Wednesday, December 5, 2007

Adding Group and Quick Add Groups to your custom webpart programmatically

I have seen that most people are doing this using the frontend, but if you have hundreds or thousands of sites, you might want to do this programmatically. I will not go in to creating the webpart itself, just listing it in the webpart gallery.

Create a new feature "MyCompanysCoolWebpart".  Paste the following in the the feature.xml file.

<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="{Insert GUID here}"
  Title="MyCompanyCoolWebpart"
  Description="This is what this feature does."
  Hidden="TRUE"
  Scope="Site"
  xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
  <ElementManifest Location="elements.xml"/>
  <ElementFile Location="dwp\myCompanyCoolWebpart.webpart" />
</ElementManifests>
</Feature>

Nothing out of the ordinary here. We are just creating a feature that will be at the site level. Next create an elements.xml file and add the following:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="WebPartPopulation" Url="_catalogs/wp" Path="dwp" RootWebOnly="TRUE">
    <File Url="myCompanyCoolWebpart.webpart" Type="GhostableInLibrary" >
      <Property Name="Group" Value="MyCompany" />
      <Property Name="QuickAddGroups" Value=";#Middle Left;#" />
    </File>
  </Module>
</Elements>

Now, lets see what we did in the elements.xml file.  First we pointed the module URL attribute to "_catalogs/wp" since that is the location of the webpart gallery and we set RootWebOnly to TRUE since we only need this feature at the Root Web level. We then set two properties "Group" and "QuickAddGroups". 

Group is going to be the name of the group your webpart will be categorized under in the "Add Web Parts" dialog. 

 grouping

QuickAddGroups will display your webpart at the top of the "Add Web Parts" dialog as "Suggested Web Parts for yourZone". The value(s) of the QuickAddGroups attribute must be separated by ";#" (semicolon hash, without the quotes, of course). If your webpart's Quick Add Group is Top and you click "Add a Web Part" in the Top zone (like follows)

topzone

You will then see your webpart is suggested for that zone.

Suggested

One thing to keep in mind is that the QuickAddGroups is not the name of the webpart zone, but the value of the QuickAdd-GroupName attribute of the WebPartZone of your page layout.

<webpartpages:webpartzone runat="server" allowpersonalization="false" frametype="TitleBarOnly"
        id="TopZone" title=""
  <% $Resources:sps,LayoutPageZone_TopZone %>
    " orientation="Vertical"
    quickadd-groupnames="Top" quickadd-showlistsandlibraries="false"><ZoneTemplate></ZoneTemplate>
</webpartpages:webpartzone>

To deploy this feature you can put it together with your webpart in a solution, install this feature using stsadm commands, call the feature from within a custom site definition or staple this to a site definition.

3 comments:

Kaj Bonfils said...

Good article.

Do you have any clues on how to change the icon next to the web part in the "Add Webparts" Dialog

Term Papers said...

I have been visiting various blogs for my term papers writing research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards

Anonymous said...

I am not sure where you are getting your information, but good topic.
I needs to spend some time learning more or understanding more.

Thanks for great info I was looking for this info for
my mission.

My blog post ... taruhan bola