Exercise 2: Creating a Web Part with Custom CAS Policies
- Start a new instance of Visual Studio 2010.
- Select File » New » Project from the main menu.
- In the New Project dialog, select VisualC#/Visual Basic » SharePoint »2010 in the Installed Templates list.(Note: be sure to choose the same language here [C# or VB.NET] as before).
- Select Empty SharePoint Project as the Project type.
- Ensure that the target framework is set to .NET Framework 3.5.
- Name the new project BestPracticesParts and click the OK button.
- When the SharePoint Customization Wizard, enter the address of the site you are using for this exercise: https://intranet.contoso.com/sites/Labxx/.
- Select to deploy the solution as a Farm Solution.
Click the Finish button.
- In the Solution Explorer, right click the BestPracticesParts project node and select Add » New Item from the context menu.
- Select to add a new Web Part.
- Name the Web Part StringProvider and click the Add button.
- In the Solution Explorer, right click the BestPracticesParts project node and select Add » New Item » expand Common Items and select Code » Interface from the Add New Item dialog box.
- 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.
- Open IStringConnection.cs/vb for editing in Visual Studio and add the code to create an interface as shown.
public interface IStringConnection { string ProvidedString { get; } }
Public Interface IStringConnection ReadOnly Property ProvidedString() As String End Interface
- Open StringProvider.cs/vb for editing in Visual Studio.
- Update the code so that the web part implements the interface as follows.
[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); } }
<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
- In the Solution Explorer, expand the Package node and double click the Package.package node.
In the visual designer, click the Manifest button (located near the bottom of the Package.package window) and expand the Edit Options.
- Click the Open in XML Editor link.
- Modify the XML to add custom CAS policies as shown in the following code.
<?xml version="1.0" encoding="utf-8"?> <Solution xmlns="https://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>
In the Solution Explorer, select the BestPracticesParts project and in the Properties window, change the Assembly Deployment property to Web Application.
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)
- 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.)
[assembly: System.Security.AllowPartiallytrustedCallers()]
<Assembly: System.Security.AllowPartiallyTrustedCallers()>
- In the Solution Explorer, right click the project and select Build from the context menu.
- After you have a successful build, right click the project and select Package.
|
|