Share via


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.
  5. Update the code so that the web part implements the interface as follows.

    C#

    public class StringConsumer : WebPart public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) this.providerPart = ProviderPart; protected override void RenderContents(HtmlTextWriter writer) try { catch {
    {
    FakePre-daa34dfe15464cb487199b0c1ed440d0-4f23fb6285f44c588e912fe749afaa09FakePre-d0b7d335fc4a4a49a00f61fe3205e26d-8239f9972f7347ea9824512ef4ff753fFakePre-77d43a4428bd475793a00e8ed60be714-5e82b6f335fc4540b3fafd7870b7991dpublic class StringConsumer : WebPart public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) this.providerPart = ProviderPart; protected override void RenderContents(HtmlTextWriter writer) try { catch {FakePre-0cd164ccd86f44e587fa65d4d98cd963-8c8eb44901fc4957b76a0144946b4a80public class StringConsumer : WebPart public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) this.providerPart = ProviderPart; protected override void RenderContents(HtmlTextWriter writer) try { catch {FakePre-21a7418498bb414a86de3ebc2c005d7d-7c526dd33bf1463a8b8560fc39d9e07bFakePre-7ec86a19c6b0429eb6a117afb1479bce-0c500353af314a4aa7ec48c1203aba07public class StringConsumer : WebPart public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) this.providerPart = ProviderPart; protected override void RenderContents(HtmlTextWriter writer) try { catch {FakePre-d2d66c10ddc8447d934062fbdd0992b0-444e4afb170b4dd58dd663f91bd54f61public class StringConsumer : WebPart public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) this.providerPart = ProviderPart; protected override void RenderContents(HtmlTextWriter writer) try { catch {FakePre-caecd8edcf70412cb74e938547e77ebc-f60ef7315e4b46d0976b03d30f322979FakePre-f62c02554c314ceeb1a4eb76d6eb6362-1a6551413640426885ea9f8123aae924public class StringConsumer : WebPart public void GetConnectionInterface( BestPracticesParts.IStringConnection ProviderPart) this.providerPart = ProviderPart; protected override void RenderContents(HtmlTextWriter writer) try { catch {FakePre-cea4997738614e4f9f5d81d7a7e00f55-d51a462e6e174908b6415c18e8eaffb4FakePre-f4df0d75b713463ca8c0b464e59307f5-5c179fcd71184cfbab0cae3c5bafbc29FakePre-00825046469c4f17bf0f1d0557c40500-98474b3ba8734a00b704df894ba2b8c9FakePre-cd1c5c7ee4444353a13d2dc2f8838e61-2e9e8425337848c1b0b58fef8b82d34cFakePre-c23dd1e422964081af0d326c63afd461-51272dfbbd3e4a77bc2f4bae11e2fd1c

    VB.NET

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

  7. 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="https://schemas.microsoft.com/sharepoint/"> <ActivationDependencies> <ActivationDependency SolutionId="Your Solution Id here"/> </ActivationDependencies> <CodeAccessSecurity> …
  8. 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.

  9. Open the BestPracticesImages solution in Visual Studio 2010.
  10. In the Solution Explorer, right click the project and select Deploy from the context menu.
  11. After successfully deploying the BestPracticesImages project, return to the BestPracticesParts project.
  12. In the Solution Explorer, right click the project and select Deploy from the context menu. Your solution should now deploy successfully.
  13. Open your test site in Internet Explorer.
  14. In the test site, add the StringProvider and StringConsumer web parts to the page.
  15. Initially, the consumer should be displaying the delete image indicating that it has no connection.

  16. Using the web part menu, connect the consumer and provider and verify that the text is passed between them.