Here is the scenario: You deployed your SharePoint solution and applied your specific themes for all of the webparts and components to look exactly how you wanted them to look. Few weeks later, your users report some pages look different. You discover that out of the box webparts you haven`t styled have been added to pages. How do you limit users to be able to select only webparts particular to your site?
In most cases when you deploy your custom solution - you already have all of the necessary webparts and components in place and it`s very unlikely for your users to change design of the site; most of the time they need a content editor webpart here and there and maybe one of your custom webparts. However, because there are many other webparts available when users access webpart gallery - you end up with some that your theme hasn`t anticipated and now parts of the pages look differently.
One of the solutions would be to hide unwanted webparts from the gallery as a part of your solution deployment. To do that you will need to create a site of web scoped feature and activate it after you have deployed your pages and created a site structure.
Here is the definition of my feature that will operate on a web scope:
<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”
Id=”587C3A55-BF2A-4bda-8F10-55FFBAF03E72″
Title=”Hide unused webparts”
Description=”Hide unused webparts”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”False”
ActivateOnDefault=”FALSE”
AlwaysForceInstall=”TRUE”
ReceiverAssembly=”MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bcd2fea03ff3d79″
ReceiverClass=”MyProject.RemoveWebpartsFromGal.RemoveWebpartsFromGal”>
</Feature>
<?xml version=”1.0″ encoding=”utf-8″?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/”
Id=”587C3A55-BF2A-4bda-8F10-55FFBAF03E72″
Title=”Hide unused webparts”
Description=”Hide unused webparts”
Version=”1.0.0.0″
Scope=”Web”
Hidden=”False”
ActivateOnDefault=”FALSE”
AlwaysForceInstall=”TRUE”
ReceiverAssembly=”MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bcd2fea03ff3d79″
ReceiverClass=”MyProject.RemoveWebpartsFromGal.RemoveWebpartsFromGal”>
</Feature>
Next, you need to create a receiver class that will perform the logic to remove your unused webparts, in my case called
RemoveWebpartsFromGal.cs, will have
FeatureActivated method that will look like this:
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
SPList list = web.Lists["Web Part Gallery"];
for (int i = 0; i < list.Items.Count; i++)
{
string webpartName = list.Items[i].Name;
webpartName = webpartName.Substring(0, webpartName.IndexOf(’.'));
if (!webpartName.Equals(”MyCustomWebPart”)
&& !webpartName.Equals(”MSContentEditor”))
{
list.Items[i].Delete();
}
}
list.Update();
}
In here, we`re accessing Web Part Gallery that holds all of the webpart definitions; loop through them to ensure we don`t remove the ones we want to keep (in my case MSContentEditor - a content editor webpart) and remove the definitions.
If your solution is deployed by an automated script – your webpart definitions will be re-created each time you re-deploy the solution. In case you need to add one of the definitions later on – just create copy existing
webpart or
dwp file to the gallery.
Yaroslav Pentsarskyy, Microsoft MVP
Blog:
www.sharemuch.com