Wednesday, 6 February 2013

Creating a Feature with Common Resources

 Creating a Feature with Common Resources

Excercise 1:-
  1. If you haven’t already done so, run the batch file named SetupLabxx.bat, found in the c:\Student\Labs\xx_BestPractices\ folder, to create the new site collection that will be used to test and debug the code you will be writing in this lab. This batch file creates a new site collection at an URL of http://intranet.contoso.com/sites/Labxx.
  2. Start Visual Studio 2010.
  3. Select File » New » Project from the main menu.
  4. In the New Project dialog, select VisualC#/Visual Basic » SharePoint »2010 in the Installed Templates list.
  5. Select Empty SharePoint Project as the Project type.
  6. Ensure that the target framework is set to .NET Framework 3.5.
  7. Name the new project BestPracticesImages and click the OK button.
  8. When the SharePoint Customization Wizard, enter the address of the site you are using for this exercise: http://intranet.contoso.com/sites/Labxx/.
  9. Select to deploy the solution as a Farm Solution.
  10. Click the Finish button.
  11. In the Solution Explorer, right click the Features node and select Add Feature from the context menu.
  12. In the Solution Explorer, right click the BestPracticesImages project node and select Add » SharePoint “Images” Mapped Folder from the context menu.
  13. In the Solution Explorer, inside of the Images folder, right click the BestPracticesImages folder and select Add » Existing Item from the context menu.
  14. Locate the file C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\Template\Images\delete.gif and add it to the folder.
  15. In the Solution Explorer, expand the Package node and double-click the Package.package node.
  16. 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)
  17. DO NOT DEPLOY THE SOLUTION YET!
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: http://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.

    1. Open IStringConnection.cs/vb for editing in Visual Studio and add the code to create an interface as shown.
    2. C#

    1. public interface IStringConnection
    2. {
    3.     string ProvidedString { get; }
    4. }
    1. C#

    1. Public Interface IStringConnection
    2.     ReadOnly Property ProvidedString() As String
    3. End Interface

    1. Open StringProvider.cs/vb for editing in Visual Studio.

    1. Update the code so that the web part implements the interface as follows.
    2. C#

    1.  [ToolboxItemAttribute(false)]
    2. public class StringProvider : WebPart, IStringConnection
    3. {
    4.     private TextBox theString;
    5.  
    6.     [ConnectionProvider("String Provider")]
    7.     public IStringConnection ConnectionInterface()
    8.     {
    9.         return this;
    10.     }
    11.  
    12.     public string ProvidedString
    13.     {
    14.         get { return theString.Text; }
    15.     }
    16.  
    17.     protected override void CreateChildControls()
    18.     {
    19.         theString = new TextBox();
    20.         if (!Page.IsPostBack)
    21.             theString.Text = "Web Part connections are good";
    22.         this.Controls.Add(theString);
    23.     }
    24.  
    25.     protected override void RenderContents(HtmlTextWriter writer)
    26.     {
    27.         writer.Write("<p>Enter a string</p>");
    28.         theString.RenderControl(writer);
    29.  
    30.     }
    31. }

    1. Visual Basic
    2. <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
    3. In the Solution Explorer, expand the Package node and double click the Package.package node.
    4. In the visual designer, click the Manifest button (located near the bottom of the Package.package window) and expand the Edit Options.
    5. Click the Open in XML Editor link.
    6. Modify the XML to add custom CAS policies as shown in the following code.
      XAML
      <?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>
    7. In the Solution Explorer, select the BestPracticesParts project and in the Properties window, change the Assembly Deployment property to Web Application.
    8. 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)
    9. 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()> 
    10. In the Solution Explorer, right click the project and select Build from the context menu.
    11. After you have a successful build, right click the project and select Package.
    Exercise 3: Creating a Dependent Web Part
    1. In the Solution Explorer for BestPracticesParts, right click the project node and select Add » New Item from the context menu.
    2. Select to add a new Web Part.
    3. Name the Web Part StringConsumer and click the Add button.
    4. Open StringConsumer.cs/vb for editing in Visual Studio.

    1. Update the code so that the web part implements the interface as follows.
    2. C#

    1. public class StringConsumer : WebPart
    2. public void GetConnectionInterface(
    3. BestPracticesParts.IStringConnection ProviderPart)
    4. this.providerPart = ProviderPart;
    5. protected override void RenderContents(HtmlTextWriter writer)
    6. try
    7. {
    8. catch
    9. {
    1. {
    2.     BestPracticesParts.IStringConnection providerPart = null;
    1.     [ConnectionConsumer("String Consumer")]

     





     
      1. public class StringConsumer : WebPart
      2. public void GetConnectionInterface(
      3. BestPracticesParts.IStringConnection ProviderPart)
      4. this.providerPart = ProviderPart;
      5. protected override void RenderContents(HtmlTextWriter writer)
      6. try
      7. {
      8. catch
      9. {


        {

     





     
      1. public class StringConsumer : WebPart
      2. public void GetConnectionInterface(
      3. BestPracticesParts.IStringConnection ProviderPart)
      4. this.providerPart = ProviderPart;
      5. protected override void RenderContents(HtmlTextWriter writer)
      6. try
      7. {
      8. catch
      9. {


        }
      
    
    
    
    
    
    public class StringConsumer : WebPart
    public void GetConnectionInterface(
    BestPracticesParts.IStringConnection ProviderPart)
    this.providerPart = ProviderPart;
    protected override void RenderContents(HtmlTextWriter writer)
    try
    {
    catch
    {
        {
    public class StringConsumer : WebPart
    public void GetConnectionInterface(
    BestPracticesParts.IStringConnection ProviderPart)
    this.providerPart = ProviderPart;
    protected override void RenderContents(HtmlTextWriter writer)
    try
    {
    catch
    {
                writer.Write("<p>" + providerPart.ProvidedString + "</p>");
            }

      1. public class StringConsumer : WebPart
      2. public void GetConnectionInterface(
      3. BestPracticesParts.IStringConnection ProviderPart)
      4. this.providerPart = ProviderPart;
      5. protected override void RenderContents(HtmlTextWriter writer)
      6. try
      7. {
      8. catch
      9. {
                writer.Write(             @"<img src='/_layouts/images/BestPracticesImages/delete.gif'/>"
                            );
            }
        }
    }

    1. VB.NET
    2. <ToolboxItemAttribute(false)> _
      Public Class StringConsumer
          Inherits WebPart
      
          Private providerPart As BestPracticesParts.IStringConnection = Nothing
      
          <ConnectionConsumer("String Consumer")> _
          Public Sub GetConnectionInterface(ByVal ProviderPart _
                                            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
    3. 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)
    4. 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.
      XAML
      …
      <Solution xmlns="http://schemas.microsoft.com/sharepoint/">
      <ActivationDependencies>
        <ActivationDependency SolutionId="Your Solution Id here"/>
      </ActivationDependencies>
      <CodeAccessSecurity> …
    5. 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.
    6. Open the BestPracticesImages solution in Visual Studio 2010.
    7. In the Solution Explorer, right click the project and select Deploy from the context menu.
    8. After successfully deploying the BestPracticesImages project, return to the BestPracticesParts project.
    9. In the Solution Explorer, right click the project and select Deploy from the context menu. Your solution should now deploy successfully.
    10. Open your test site in Internet Explorer.
    11. In the test site, add the StringProvider and StringConsumer web parts to the page.
    12. Initially, the consumer should be displaying the delete image indicating that it has no connection.

    1. Using the web part menu, connect the consumer and provider and verify that the text is passed between them.
  • No comments:

    Post a Comment