Home > SharePoint > Creating custom group for SharePoint web parts programmatically

Creating custom group for SharePoint web parts programmatically

In last blog i discussed how to create new group / custom group or category for SharePoint web part(s).
Into this will discuss how to create the group / category programmatically.

For that purpose i have created a feature and in feature reciever (.cs) code is being applied when feature is activated.

//Getting the url of home site of site current site collection

using (SPSite aSite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb aWeb = aSite.OpenWeb())
                    {

// Getting the web part gallery into galleryList object of SPList

                        SPList galleryList = aWeb.Lists[“Web Part Gallery”];

                        foreach (SPListItem anItem in galleryList.Items)
                        {
// There could have been many items into that gallery with properties null or no properties – check for them , as custom web part would have property [“WebPartTypeName”]

                            if (anItem.Properties != null && anItem.Properties.Count > 0)
                            {
// Check whether an item contains right properties , i am using name space to locate the right property as web part name can be changed by administrator but not the name space name as that would have been deployed by the developer.
                                if (anItem.Properties[“WebPartTypeName”].ToString().ToLower().Contains(“mycustomwebpart[name space]”))
                                {
                                       anItem[“Group”] = “My Custom Group”;
                                       anItem[“Description”] = anItem.Title + ” This is description of my custom web part”;
                                        anItem.Update();
                                  }
                            }
                        }
                    }

 

After that all the web parts comprising that name space would be added to “My Custom Group” with description.

Happy Coding.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: