Wednesday, August 26, 2009

WSPBuilder – Adding multiple web parts to a single feature

One small limitation I found with WSPBuilder is that there is not a default way of adding new web parts to existing features. Fortunately, there is an easy way to do this.

1. Create a WSPBuilder project in Visual Studio.

2. Add a new WSPBuilder Web Part Feature to the project. (Ctrl+Shift+A)

image 

3. Now add another Web Part Feature. At this point, your project should look something like this:

image

4. Drag the web part you want combined into the parent folder of the other feature and then delete the feature folder from where you dragged it.

image

5. Open the elements.xml file and add another File node with the information from the second web part.

<?xml version="1.0" encoding="utf-8" ?>
<
Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<
Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE">
<
File Url="WebPartFeature1.webpart" Type="GhostableInLibrary">
<
Property Name="Group" Value="MyGroup"></Property>
<
Property Name="QuickAddGroups" Value="MyGroup" />
</
File>
<
File Url="WebPartFeature2.webpart" Type="GhostableInLibrary">
<
Property Name="Group" Value="MyGroup"></Property
>
<
Property Name="QuickAddGroups" Value="MyGroup"
/>
</
File
>
</
Module>
</
Elements>







6. Open the feature.xml file and add an ElementFile entry for the second web part.



<?xml version="1.0" encoding="utf-8"?>
<
Feature Id="a942cf13-5e3b-48e6-9862-6f98dc931375"
Title="WebPartFeature1"
Description="Description for WebPartFeature1"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/">
<
ElementManifests>
<
ElementManifest Location="elements.xml"/>
<
ElementFile Location="WebPartFeature1.webpart" />
<
ElementFile Location="WebPartFeature2.webpart" />
</
ElementManifests>
</
Feature>

7. You can now build the project (Right-click Project –> Build), build the WSP (Right-click Project –> WSPBuilder –> Build WSP) and deploy the feature (Right-click Project –> WSPBuilder –> Deploy). If you look in hive under TEMPLATE\Features\YourFeature, you will see that both web parts are there.

7 comments:

  1. HiJohn,
    I have developed an event handler,called TaskListEventHandler, for the Task list ((ItemAdding, ItemDeleting, ItemUpdating) as feasture using the WSP Builder project template, and I have also added a web part, called RegisterEventHandler, which will be used to bind(register) the events to a list. The webpart is WSP Builder template "Webpart without feature".

    Now as per your post, i moved the .webpart file from the 80/wpcatalog to the Feature folder and updated the elements.xml and the feature.xml with the reference to the .webpart file


    Now i do build->WSPBuild ->CreateDeploymentfolder using the WSPBuilder.

    After deployment, the feature and webpart are getting deployed sucessfully, But I had set the grouping for the webpart but it is not grouping,and even it is not getting populated automatically i need to populate it.

    Could you please give me a solution.

    ReplyDelete
  2. This is one thing I have noticed as well and I'm not sure if it is because of the way I am hacking WSPBuilder or a bug with WSPBuilder. I end up having to manually go to the site's Web Part Gallery and do it manually. Let me know if you find a work-around. I haven't had a chance to investigate.

    By the way, here is the URL for the Web Part Gallery page.
    http://[yourportalname]/_layouts/newdwp.aspx

    ReplyDelete
  3. Hi John,
    Thanks for your reply.

    I tried using the .wsp file (generated through WSPBuilder Deployment)and deployed it on the server using the stsadm command, and the grouping of the webpart works fine.

    It is strange that the WSPBuilder is not able to do it, I 'm still investigating it.


    With regards
    Biju

    ReplyDelete
  4. hi John,

    I didnn't understand about the link which you have mentioned in your post

    "By the way, here is the URL for the Web Part Gallery page.
    http://[yourportalname]/_layouts/newdwp.aspx "


    How can i make use of it?

    With regards
    Biju

    ReplyDelete
  5. That's just a shortcut to getting to the Web Part Gallery. Sometimes I find the navigation a little trying in SharePoint.

    http://office.microsoft.com/en-us/winsharepointadmin/HA011608541033.aspx

    ReplyDelete
  6. I'm experiencing the same thing; webparts aren't automatically populated in the gallery. It worked in the beginning, but now it doesn't anymore and I cannot seem to figure out what's happening :(

    ReplyDelete
  7. The 2 web parts only refer to 1 of the 2 .cs files in the WebPartCode section. Anyone has the solution to to webpart 2 to .cs file #2?

    ReplyDelete