Sunday, 20 January 2013

Listing all the webparts in a particular group


Problem : Listing all the webparts that belong to a particular group

Analysis : Well, all the webparts that we have deployed to our sharepoint site can be seen in the WebPart Gallery which acts as a repository for all the webparts that we can add on a webpart page. Now to find which webpart belongs to which group this is the place we should be looking to.

Solution : SharePoint Object Model provides us the flexibility to iterate through lists and fetch results based upon our requirement.

Here in this case Web Part Gallery is nothing but a list only. Those who have even the slightest doubt , I am fully prepared with the below screenshot .

All the lists get stored in AllLists table in the content database. If you open it then you can see an entry for Web Part Gallery there.


So, once we know that Web Part Gallery is a list we are half done. :)

Object Model provides us many classes through which we can easily fetch the data from sharepoint. SPList class helps us in fetching information from a SharePoint list.

Web Part Gallery has a field called Group. So, the entire problem gets shortened up to one thing i.e. fetching data from a list where the value of one field is something.

Easy :) I know all of you must have by now anticipated the solution already but still would like to give the finishing touch by adding the code snippet... We developers see code everywhere and can't live without putting them here and there... :p

                SPSite currentSite = SPContext.Current.Site;
                SPWeb web = currentSite.OpenWeb();
                SPList list = web.Lists["Web Part Gallery"];
                SPQuery query = new SPQuery();
                query.Query = "<Where><Eq><FieldRef Name='Group' /><Value Type='Text'>Name of the group</Value></Eq></Where>";
                SPListItemCollection itemCollection= list.GetItems(query);

Great ! Now we have the list of all the webparts that belong to a particular group. Hope this might help you somewhere.

Regards,

Geetanjali Arora


No comments:

Post a Comment