Enjoy free articles about SharePoint 2010 and InfoPath 2010 at www.walisystemsinc.com
In the second article Connectable Web Parts In InfoPath 2010 we created and published an InfoPath form and then used it to create connectable web parts. In this article, we will create connectable web parts from scratch using .NET. In the previous article, the focus was on creating and configuring InfoPath 2010 form in such a way that it can be used in InfoPath 2010 Web Part. InfoPath 2010 web part comes with SharePoint 2010 and is used to host InfoPath 2010 forms. If InfoPath form is configured correctly or in other words, if its fields have been exposed as parameters, it can be used in InfoPath 2010 web part. In this article we will create .NET web parts from scratch and then deploy them in SharePoint. We will create two web parts, one provider and one consumer. We will use same fields that we used in InfoPath form we created in previous article.
è You can download complete application/code from the following link:
è Web part deployment package (WSP) is in the following folder:
So, let’s begin. Here are the steps:
1. Open Visual Studio 2010. Click New Project.
2. Under Installed Templates, expand SharePoint node and select 2010. Select Empty SharePoint Project and enter ConnectableWebParts in the Name box and click OK. You can change the Location if you want otherwise keep the default location selected.
3. Select a site in What local site do you want to use for debugging. Select Deploy as a farm solution option and click Finish. This will create a new project for you.
4. Right-click project name and select Add. Select New Item. In Installed Templates, Under Visual C#, select Code and then select Interface. Enter IParams.cs in the Name box and click Add.
Figure 1: Add new
Figure 1: Add new item
5. In IParams.cs code, add public before interface IParams and then add a string parameters. Code will look like the following:
Figure 2: Interface
Figure 2: Interface
6. Now we will create a provider web part. Right-click project name. Select Add. Select New Item. In Installed Templates, select Visual C#. Select SharePoint. Select 2010. Select Web Part. Enter ProviderWebPart in Name box and click Add.
7. ProviderWebPart class inherits from WebPart. Add IParams after WebPart.
Figure 3: Provider
Web Part Class
Figure 3: Provider Web Part Class
8. Add following code to create fields and Submit button.
Figure 4: Properties
9. Add get and set methods.
10. Modify CreateChildControls() method.
Figure 5: CreateChildControls()
Figure 5: CreateChildControls()
11. Handle Click event.
12. Create a method to return the iParams object.
Figure 6: Connection Provier
13. Now let’s
create a Consumer web part. Just like you added a provider
web part above, add a new web part and name it Consumer.
14. Open Consumer.cs and add code to create objects for the provider.
Figure 7: Consumer web part properties
15. Add a function that will build rows in a table. We will use a table to show the data retrieved from the provider web part.
Figure 8: BuildTableRow() Function
16. Add values to the labels.
Figure 9: OnPreRender() Function
17. Add code in CreateChildControls() method.
18. Create a method to assign the IParams object from the provider web part to the private IParams object in the consumer web part.
Figure 10: Connection Consumer
19. We have completed the code. Now it’s time to test the web parts. One way is to use F5 in Visual Studio if you want to debug and test the functionality. Second is to deploy the web part in SharePoint and then test it. I would recommend that you use F5 to deploy the web parts in SharePoint. This will automatically create the WSP for the web parts. Once the WSP has been created, you can deploy it to SharePoint using PowerShell. After the WSP has been created, open PowerShell. Go to Start > All Programs > Microsoft SharePoint 2010 Products and select SharePoint 2010 Management Shell.
20. For convenience copy the WSP from the debug folder to a folder that is easily accessible. Go to project folder and then access bin\debug. Copy ConnectableWebParts.wsp to C:\Temp folder (or any other conveniently located folder).
21. From Management Shell command prompt, run following command:
Add-spsolution –literalpath “C:\Temp\ConnectableWebParts.wsp”
Figure 11: Deploy web part
You will immediately see the result of the deployed. Deployed column will show False.
22. Now run following command from the Management Shell command prompt:
Install-spsolution –identity connectablewebparts.wsp –webapplication http://yourserver –gacdeployment
Replace http://yourserver with your SharePoint Server (site) URL.
23. Open SharePoint site. Select Site Actions and then View All Site Content.
24. Click Create.
25. Select Page from Filter (under Installed Items) and then select Web Part from the Type and click Create.
26. Enter ConnectableWebParts in the Name box and click Create. Leave Site Assets selected in the Document Library drop down.
27. Click Add a web part in the Header zone. Select Custom from Categories. Select ProviderWebPart from Web Parts and click Add.
28. Click Add a Web Part in the Footer zone. Select Custom from Categories. Select Consumer from Web Parts and click Add.
29. Hover mouse over Consumer web part heading. A small arrow will appear at the end of the row that contains the heading. Click the arrow, a menu will appear. Select Connections > Get Consumer From > ProviderWebPart.
Figure 12: Get Consumer From
30. Click Stop Editing button in the ribbon.
31. Add data in the provider web part and hit Submit. Before submitting data, this is how the web parts will look:
Figure 13: Provider and Consumer Web Parts ready for use
After you submit data, Consumer web part will show values from the provider web part. This is how it will look:
Figure 14: Provider and Consumer Web Parts after Submission
So, this wraps up our demo of connectable web parts created in .NET and deployed in SharePoint 2010.