Sunteți pe pagina 1din 13

SharePoint 2010 Best Practices

SharePoint 2010 Best Practices

Table of Contents
SharePoint 2010 Best Practices .............................................................................................. 1
Exercise 1 Creating a Feature with Common Resources ...............................................................................................2 Exercise 2 Creating a Web Part with Custom CAS Policies ............................................................................................4 Exercise 3 Creating a Dependent Web Part ...................................................................................................................9

SharePoint 2010 Best Practices

SharePoint 2010 Best Practices


Objectives
After completing this lab, you will be better able to: Create a feature with common resources Create a web part with custom CAS policies Create a dependent web part

Scenario

In this lab you will learn how to create a feature that depends on another feature and how you can create features using Code Access Security. 30 Minutes

Estimated Time to Complete This Lab Computers used in this Lab

demo2010a The password for the Administrator account on all computers in this lab is: pass@word1

Page 1 of 11

SharePoint 2010 Best Practices

Exercise 1 Creating a Feature with Common Resources


Scenario
In this exercise, you will create a feature that contains images upon which other features depend. Tasks Complete the following task on: demo2010a 1. Creating a Feature with Common Resources Detailed Steps
a. Before you begin this lab, you must run the PSI file named Setup, located here:

C:\SharePoint2010Developer\Labs\BestPractices\Source. This file creates a new SharePoint site collection at the location http://intranet.contoso.com/sites/Labxx.
b. Start Visual Studio 2010. c. Select File New Project from the main menu. d. In the New Project dialog, select Visual C# or Visual Basic SharePoint 2010 in

the Installed Templates list. Note: Visual C# code and Visual Basic code has been provided for your convenience
e. Select Empty SharePoint Project as the Project type. f.

Ensure that the target framework is set to .NET Framework 3.5.

g. Name the new project BestPracticesImages and click the OK button. h. When the SharePoint Customization Wizard, enter the address of the site you

are using for this exercise: http://intranet.contoso.com/sites/Labxx/.


i. j.

Select to deploy the solution as a Farm Solution. Click the Finish button.

Page 2 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps


k. In the Solution Explorer, right click the Features node and select Add Feature

from the context menu.


l.

In the Solution Explorer, right click the BestPracticesImages project node and select Add SharePoint Images Mapped Folder from the context menu. BestPracticesImages folder and select Add Existing Item from the context menu.

m. In the Solution Explorer, inside of the Images folder, right click the

n. Locate the file C:\Program Files\Common Files\Microsoft Shared\web server

extensions\14\Template\Images\delete.gif and add it to the folder.


o. In the Solution Explorer, expand the Package node and double-click the

Package.package node.
p. In the Properties window (lower right side), locate the SolutionId property and

copy the SolutionId property to Notepad for use later in the lab. Note: Your ID will be different from the one shown in the image below

q. Do not deploy the solution

Page 3 of 11

SharePoint 2010 Best Practices

Exercise 2 Creating a Web Part with Custom CAS Policies


Scenario
In this exercise, you will create a web part that utilizes a custom CAS policy. Tasks Complete the following task on: Demo2010a 1. Creating a Web Part with Custom CAS Policies Detailed Steps
a. Start a new instance of Visual Studio 2010. b. Select File New Project from the main menu. c. In the New Project dialog, select Visual C#/Visual Basic SharePoint 2010 in

the Installed Templates list. (Note: be sure to choose the same language here [C# or VB.NET] as before).
d. Select Empty SharePoint Project as the Project type. e. Ensure that the target framework is set to .NET Framework 3.5. f.

Name the new project BestPracticesParts and click the OK button. are using for this exercise: http://intranet.contoso.com/sites/Labxx/.

g. When the SharePoint Customization Wizard, enter the address of the site you h. Select to deploy the solution as a Farm Solution. i.

Click the Finish button.

j.

In the Solution Explorer, right click the BestPracticesParts project node and select Add New Item from the context menu. Name the Web Part StringProvider and click the Add button.

k. Select to add a new Web Part. l. m. In the Solution Explorer, right click the BestPracticesParts project node and select

Page 4 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps Add New Item and select Code Interface from the Add New Item dialog box.
n. Name the interface IStringConnection.cs or IStringConnection.vb (Note: the first

character here is a capital i that is used to denote an interface class) and click the Add button.
o. Open IStringConnection.cs/vb for editing in Visual Studio and add the code to

create an interface as shown. C#


public interface IStringConnection { string ProvidedString { get; } }

VB.NET
Public Interface IStringConnection ReadOnly Property ProvidedString() As String End Interface

p. Open StringProvider.cs/vb for editing in Visual Studio. q. Replace the code so that the web part implements the interface as follows.

C#
[ToolboxItemAttribute(false)] public class StringProvider : WebPart, IStringConnection { private TextBox theString; [ConnectionProvider("String Provider")] public IStringConnection ConnectionInterface() { return this; } public string ProvidedString { get { return theString.Text; } } protected override void CreateChildControls() { theString = new TextBox(); if (!Page.IsPostBack) theString.Text = "Web Part connections are good"; this.Controls.Add(theString); } protected override void RenderContents(HtmlTextWriter writer) { writer.Write("<p>Enter a string</p>"); theString.RenderControl(writer); } }

VB.NET

Page 5 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps


<ToolboxItemAttribute(False)> _ Public Class StringProvider Inherits WebPart Implements IStringConnection Private theString As TextBox <ConnectionProvider("String Provider")> _ Public Function ConnectionInterface() As IStringConnection Return Me End Function Public ReadOnly Property ProvidedString() As String _ Implements IStringConnection.ProvidedString Get Return theString.Text End Get End Property Protected Overrides Sub CreateChildControls() theString = New TextBox() If Not Page.IsPostBack Then theString.Text = "Web Part connections are good" End If Me.Controls.Add(theString) End Sub Protected Overrides Sub RenderContents(ByVal writer _ As HtmlTextWriter) writer.Write("<p>Enter a string</p>") theString.RenderControl(writer) End Sub End Class r. In the Solution Explorer, expand the Package node and double click the

Package.package node.
s. In the visual designer, click the Manifest button (located near the bottom of the

Package.package window) and expand the Edit Options.

Page 6 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps


t. Click the Open in XML Editor link. u. Modify the XML to add custom CAS policies as shown in the following code. <?xml version="1.0" encoding="utf-8"?> <Solution xmlns="http://schemas.microsoft.com/sharepoint/"> <CodeAccessSecurity> <PolicyItem> <PermissionSet class="NamedPermissionSet" version="1" Description="Connection Permissions"> <IPermission class="AspNetHostingPermission" version="1" Level="Minimal" /> <IPermission class="SecurityPermission" version="1" Flags="Execution" /> </PermissionSet> <Assemblies> <Assembly Name="BestPracticesParts"/> </Assemblies> </PolicyItem> </CodeAccessSecurity> </Solution> v. In the Solution Explorer, select the BestPracticesParts project and in the

Properties window, change the Assembly Deployment property to Web Application.

w. In the Solution Explorer, expand the(if using C#) Properties node / (if using

VB.NET) MyProject node, and open the AssemblyInfo.cs/vb file for editing. Note: You may have to click the Show all files button in the solution explorer to see the items in these nodes

x. Verify that the following to assembly attribute exists somewhere in this file. This

allows the web parts to run outside of the GAC. Note: Both C# and VB.NET should automatically add this. C#
[assembly: AllowPartiallytrustedCallers()]

VB.NET
<Assembly: AllowPartiallyTrustedCallers()>

Page 7 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps


y. In the Solution Explorer, right click the project and select Build from the context

menu.
z. After you have a successful build, right click the project and select Package.

Page 8 of 11

SharePoint 2010 Best Practices

Exercise 3 Creating a Dependent Web Part


Scenario
In this exercise, you will create a web part that depends upon the Solution packaged in Exercise 2 Tasks Complete the following task on: Demo2010a 1. Creating a Dependent Web Part Detailed Steps
a. In the Solution Explorer for BestPracticesParts, right click the project node and

select Add New Item from the context menu.


b. Select to add a new Web Part. c. Name the Web Part StringConsumer and click the Add button. d. Open StringConsumer.cs/vb for editing in Visual Studio. e. Update the code so that the web part implements the interface as follows.

C#
public class StringConsumer : WebPart { BestPracticesParts.IStringConnection providerPart = null; [ConnectionConsumer("String Consumer")] public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) { this.providerPart = ProviderPart; } protected override void RenderContents(HtmlTextWriter writer) { try { writer.Write("<p>" + providerPart.ProvidedString + "</p>"); } catch { writer.Write( @"<img src='/_layouts/images/BestPracticesImages/delete.gif'/>" ); } } }

VB.NET
<ToolboxItemAttribute(false)> _ Public Class StringConsumer Inherits WebPart Private providerPart As BestPracticesParts.IStringConnection = Nothing <ConnectionConsumer("String Consumer")> _ Public Sub GetConnectionInterface(ByVal ProviderPart _

Page 9 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps


As BestPracticesParts.IStringConnection) Me.providerPart = ProviderPart End Sub Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter) Try writer.Write("<p>" _ & Convert.ToString(providerPart.ProvidedString) & "</p>") Catch writer.Write( _ "<img src='/_layouts/images/BestPracticesImages/delete.gif'/>") End Try End Sub End Class

In the Solution Explorer, expand the Package node, expand the Package.package node, and double click the Package.template.xml node. Note: You may have to click the Show all files button in the solution explorer to see the items in these nodes
f.

g. Edit the XML to apply a solution dependency to the solution contained in Exercise

1. Place the solution dependency code above the CAS policies as shown below. Be sure to use the SolutionId you saved to notepad in Exercise 1.
<Solution xmlns="http://schemas.microsoft.com/sharepoint/"> <ActivationDependencies> <ActivationDependency SolutionId="Your Solution Id here"/> </ActivationDependencies> <CodeAccessSecurity> h. In the Solution Explorer, right click the project and select Deploy from the

context menu. Your solution deployment should fail because the BestPracticesImages solution has not been deployed.

i. j.

Open the BestPracticesImages solution in Visual Studio 2010. In the Solution Explorer, right click the project and select Deploy from the context menu. BestPracticesParts project.

k. After successfully deploying the BestPracticesImages project, return to the l.

In the Solution Explorer, right click the project and select Deploy from the context menu. Your solution should now deploy successfully.

m. Open your test site in Internet Explorer.

Page 10 of 11

SharePoint 2010 Best Practices Tasks Detailed Steps Note: The following steps are optional
n. In the test site, add the StringProvider and StringConsumer web parts to the

page.
o. Initially, the consumer should be displaying the delete image indicating that it has

no connection.

p. Using the web part menu, connect the consumer and provider and verify that the

text is passed between them.

Page 11 of 11

S-ar putea să vă placă și