Share via


Exercise 2: Creating a Web Part with Custom CAS Policies

  1. Start a new instance of Visual Studio 2010.
  2. Select File » New » Project from the main menu.
  3. 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).
  4. Select Empty SharePoint Project as the Project type.
  5. Ensure that the target framework is set to .NET Framework 3.5.
  6. Name the new project BestPracticesParts and click the OK button.
  7. When the SharePoint Customization Wizard, enter the address of the site you are using for this exercise: https://intranet.contoso.com/sites/Labxx/.
  8. Select to deploy the solution as a Farm Solution.
  9. Click the Finish button.

  10. In the Solution Explorer, right click the BestPracticesParts project node and select Add » New Item from the context menu.
  11. Select to add a new Web Part.
  12. Name the Web Part StringProvider and click the Add button.
  13. 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.
  14. 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.
  15. 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; } }

    C#

    Public Interface IStringConnection ReadOnly Property ProvidedString() As String End Interface
  16. Open StringProvider.cs/vb for editing in Visual Studio.
  17. Update 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); } }

    Visual Basic

    <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
  18. In the Solution Explorer, expand the Package node and double click the Package.package node.
  19. In the visual designer, click the Manifest button (located near the bottom of the Package.package window) and expand the Edit Options.

  20. Click the Open in XML Editor link.
  21. Modify the XML to add custom CAS policies as shown in the following code.

    XAML

    <?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>
  22. In the Solution Explorer, select the BestPracticesParts project and in the Properties window, change the Assembly Deployment property to Web Application.

  23. 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)

  24. 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: System.Security.AllowPartiallytrustedCallers()]

    Visual Basic

    <Assembly: System.Security.AllowPartiallyTrustedCallers()>
  25. In the Solution Explorer, right click the project and select Build from the context menu.
  26. After you have a successful build, right click the project and select Package.