Preventing the use of webparts in specific sites

For a recent project we had a business request to prevent the use of certain webparts in a specific site definition.

Our initial investigation showed us that there is not that many information to be found on this subject so we decided it was time to share some information on the subject.

There are a few ways of implementing this, but each one of them has its advantages and drawbacks.

You can for example – in the case of custom webparts – implement custom logic to forcefully prevent the use of webparts in certain sites. This big disadvantages here are that you have to implement this additional logic to your webparts and this is obviously an overhead we can miss. You can not use this method for OOTB webparts either.

Another way would be to put item level security on the webpart files and just let trained users have access to it. This has obviously drawbacks that those privileges users can still add those webparts

We chose for a more modular approach by using CSS in combination with a delegate control. The advantage here is that it’s modular, but it can be overridden by a savvy power user.

  Advantages Disadvantages
Custom logic
  • Bulletproof
  • Extra development
  • Overhead in code
  • Not usable for non-custom webparts
Item level security
  • No code
  • Granular
  • Privileged users can still use it
  • Not clear that they’re “not supposed to”
CSS + delegate control
  • No code
  • Able to enable or disable on a per site basis
  • Noone can see them if disabled
  • Can be overriden by a savvy power user

 

In the sample below I will create a feature that will remove the “Content Rollup” category from the insert webpart ribbon:

image

 

 

 

 

 

 

To do this I created a new user control with the following CSS inside:

div.ms-wpadder-categories > div[title="Content Rollup"] {
        display: none;
}

Afterwards you need to create a new empty module and add the following to the elements.xml file:

<Control Id=”AdditionalPageHead” Sequence=”50” ControlSrc=”~\_controltemplates\Your_project\Your_usercontrol.ascx” />

After this you add a new feature (web scoped) to the solution and add the newly created module to this feature.

When you deploy your solution and activate your feature the web parts with category “Content Rollup” will be hidden.

image

 

As you can see in the screenshot below, the “Content Rollup” category is hidden for end users.

image

 

 

 

 

 

 

If you need to just hide specific webparts you can also use the following CSS:

div.ms-wpadder-items > div[title*="Your_contains_statement"] {
           display: none;
}