Documente Academic
Documente Profesional
Documente Cultură
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
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
demo2010a The password for the Administrator account on all computers in this lab is: pass@word1
Page 1 of 11
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.
g. Name the new project BestPracticesImages and click the OK button. h. When the SharePoint Customization Wizard, enter the address of the site you
Select to deploy the solution as a Farm Solution. Click the Finish button.
Page 2 of 11
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
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
Page 3 of 11
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.
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
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
Package.package node.
s. In the visual designer, click the Manifest button (located near the bottom of the
Page 6 of 11
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
menu.
z. After you have a successful build, right click the project and select Package.
Page 8 of 11
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
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.
In the Solution Explorer, right click the project and select Deploy from the context menu. Your solution should now deploy successfully.
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
Page 11 of 11