I'd like to expose a web part to some users, but not all of them. How can I show or hide a web part in the Add Web Parts pop up window? I'd like to do this through code, and I'm hoping to use SharePoint Roles to make this happen.
+2
A:
I know you can manage which web parts show up in the Add Web Parts window in the Web Part Gallery.
While I haven't done it... since it's just another SharePoint List, you should be able to programmatically assign roles to Groups/Users?
Update - Since you want to see some code. Nothing special, just a quick hack. You'll definitely want to do your standard error checking, etc. HTH :-)
using (SPSite site = new SPSite("YOUR SP URL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Web Part Gallery"];
// Your code for choosing which web part(s) to modify perms on
// will undoubtedly be more complex than this...
SPListItem listItem = list.GetItemById(19);
SPPrincipal groupToAdd = web.SiteGroups["YOUR GROUP NAME"] as SPPrincipal;
SPRoleAssignment newRoleAssignment = new SPRoleAssignment(groupToAdd);
SPRoleDefinition newRoleDefinition = web.RoleDefinitions["Read"];
newRoleAssignment.RoleDefinitionBindings.Add(newRoleDefinition);
listItem.RoleAssignments.Add(newRoleAssignment);
}
}
brianng
2009-07-27 20:16:35
this is encouraging, i know it can be done now. but i need to do it with code.
Dana
2009-07-28 17:12:57
A:
You can do this with SharePoint Groups.
Go to the Web Part Gallery, click "Edit" on the web part you wish to scope, then click Manage Permissions. Here you can specify which users or groups can use the web part.
Ajay Nayak
2009-07-28 18:14:01