Thursday, October 25, 2007

Programatically Organizing Web Parts in a Gallery

The other day we were given the challenge of organizing web parts for "my site" site collections.


The interesting thing about "my sites" is that they are created on the fly, which rules out the option of manually organizing the web parts.


We could deploy a feature that adds web parts to the gallery by provisioning the *.dwp and *.webpart files. However, there doesn't seem to be an easy way to delete existing web parts that the customer wishes to hide from the list.


So what we did eventually was to manage the list through code. Using a similar technique described in the article, Customizing MOSS 2007 My Sites within the enterprise, we ran a script to organize the web parts within each "My Site" web part gallery. The script essentially performs the following steps:

a. Get an instance of the web part gallery
b. Deletes all web parts in the gallery
c. Adds only the required web parts to the gallery.


And of course, it runs only once, when the site is created.



Getting an instance of the web part gallery

The first step in process is to get an instance of the web part gallery. Simply get an instance of the list named "Web Part Gallery" from the root web site.

Dim webPartGallery As SPList = SPContext.Current.Site.RootWeb.Lists("Web Part Gallery")



Deleting all web parts in the gallery

Next, we web ahead to delete all web parts in the list. Well, we could have skipped this step. But in our case, the requirement was to completely change the way the web parts were listed, so it didn't make much sense to keep the existing structure.

Dim webPartsCount As Integer = webPartGallery.ItemCount
Dim arrWebParts As New ArrayList()
For i As Integer = 0 To webPartsCount - 1
arrWebParts.Add(webPartGallery.Items(i).ID)
Next


For Each webPartId As Integer In arrWebParts
webPartGallery.Items.DeleteItemById(webPartId)
Next

webPartGallery.Update()




Adding web parts to the gallery

Finally, we add web parts to the gallery. To do so, we create SPFile objects and upload them to the "/_catalogs/wp/" folder.



The idea is to build a string for the web part description file (*.dwp or *.webpart) and load it as a byte array to the folder. In this example, we create a method that builds up a *.dwp file.

Private Sub AddWebPartToGallery(ByVal assembly As String, ByVal typeName As String, ByVal title As String, ByVal description As String, ByVal partImageLarge As String, ByVal partImageSmall As String, ByVal web As SPWeb, ByVal webPartUrl As String, ByVal group As String, ByVal quickAddGroup As String)

Dim sb As New StringBuilder()

Dim sb As New StringBuilder()
sb.Append("")
sb.Append("<webpart xmlns="">")
sb.Append("<assembly>" + assembly + "</assembly>")
sb.Append("<typename>" + typeName + "</typename>")
sb.Append("<title>" + title + "</title>")
sb.Append("<description>" + description + "</description>")
sb.Append("<partimagelarge>" + partImageLarge + "</partimagelarge>")
sb.Append("<partimagesmall>" + partImageSmall + "</partimagesmall>")
sb.Append("<chrometype>TitleOnly</chrometype>")
sb.Append("</webpart>")


Dim file As SPFile = web.Files.Add("_catalogs/wp/" + webPartUrl, New System.Text.UTF8Encoding().GetBytes(sb.ToString()), True)
file.Properties("Group") = group
file.Properties("QuickAddGroups") = quickAddGroup
file.Update()
web.Update()

End Sub



What's nice about it is that you could also supply information about the Group and quick add groups as well. Which means you get to manage how the web parts are displayed in the web part gallery picker.


We could easily create a smiliar method that handles *.webpart files.

Here's an example that calls the AddwebPartToGallery() method to add a summary link web part to a custom group, "My Stuff".

AddWebPartToGallery("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c", "Microsoft.SharePoint.Publishing.WebControls.SummaryLinkWebPart", "Summary Link Web Part", "Use to display links on your web page that can be grouped and styled and can be organized by dragging and dropping.", "/_layouts/images/webpart.gif", "/_layouts/images/webpart.gif", rootWeb, "SummaryLink.dwp", "My Stuff", "My Stuff")

And the end result of running the script is a web part gallery picker page that contains only the summary links web part in a category named, "My Stuff".


8 Comments:

At 11:13 PM, Anonymous Anonymous said...

Dear Friend,
I read your blog to manage the Webpart Gallery in MySite ,but I need to ask : Where I can put your code to customize the webpart Gallery?

 
At 1:14 PM, Blogger Mei Ying said...

I added it to a server control that was hosted by the master template.

 
At 5:42 PM, Blogger marc said...

hello,

i get the error: "The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML."

when i tried using a web part added to gallery by the method you've given.

any ideas on this?

thanks!

 
At 5:46 PM, Anonymous Anonymous said...

hello, great article the delete part worked great for me, but i have problems with the adding part.

get the error:

The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML.

when i try to add a webpart added to the gallery by the method youve shown, any ideas on this?

thanks for your time.

 
At 6:46 AM, Anonymous Anonymous said...

Mei Ying, thnks a lot for your blog.
It really saved my burden, as we are expecting this, to be implemented in our portal too...

I need ur assistance in implementing the below scenario.

If i click "Add web part" in a zone, instead of opening the web part gallery in a pop-up and then clicking the "Advanced Web part gallery and options", which inturn opens the web pary gallery in the page itself.

we need "add web part" should take us to the web part gallery with in the page(the one you will get once you click the "advanced web part gallery and options").

your suggestions / assistance is highly valuable. pls give me an idea to achieve this.

 
At 12:52 AM, Anonymous Anonymous said...

check this out:
http://baigadil.blogspot.com/2008/06/removing-webpart-from-gallery-in.html

 
At 1:01 PM, Anonymous Anonymous said...

Hi
Me anuja ,very nice and helpful blog.
Will you please provide a full code ? you used a class or a httphandler for this?
And i just want to hide the all webparts and want to display only some to a specific group of users.
so how to do this?
will you please help me??

Thanks for your time

 
At 12:51 AM, Anonymous Keval said...

This is a very nice article! Thanks!
Keval

 

Post a Comment

<< Home