SharePoint Security Helper Class
In order to circumvent recoding SharePoint security operations several times over, you might find the below class effective.
Some things to note. There are a lot of literal calls in the code, and it’s on purpose. For instance, instead of passing a specific SPWeb object as a parameter, a string value to represent the URL is instead used to hydrate the primary SPWeb.
This code can easily be adjusted, you could create a global SPWeb object (which would aid in trimming the method), you could switch the entire class so that it was tailored towards context, etc. Keeping it in this manner just keeps better medium independence since it makes it easier for client development.
-
#region Imports
-
using System.Collections.Generic;
-
using Microsoft.SharePoint;
-
#endregion
-
namespace Example
-
{
-
public class SharePointSecurityHelpers
-
{
-
#region Helper Methods
-
public static SPWeb ReturnSPWeb(string url)
-
{
-
{
-
using (SPWeb web = site.OpenWeb())
-
{
-
return web;
-
}
-
}
-
}
-
public static SPList GetList(string url, string list)
-
{
-
try
-
{
-
return ReturnSPWeb(url).Lists[list];
-
}
-
catch
-
{
-
// Exception Handling Goes Here
-
return null;
-
}
-
}
-
#endregion
-
#region Groups And Roles
-
public static void AddSharePointGroup(string url, string groupName, string groupDescription, string roleName)
-
{
-
SPWeb web = ReturnSPWeb(url);
-
-
web.SiteGroups.Add(groupName, web.AssociatedOwnerGroup, null, groupDescription);
-
web.AssociatedGroups.Add(web.SiteGroups[groupName]);
-
web.Update();
-
SPRoleDefinition roleApp = web.RoleDefinitions[roleName];
-
-
assignment.RoleDefinitionBindings.Add(roleApp);
-
}
-
-
public static List GetAllSharePointGroups(SPWeb web)
-
{
-
foreach (SPGroup group in web.SiteGroups)
-
{
-
tempCollection.Add(group);
-
}
-
return tempCollection;
-
}
-
public static List GetAllRoleDefinitions(SPWeb web)
-
{
-
foreach (SPRoleDefinition definition in web.RoleDefinitions)
-
{
-
tempCollection.Add(definition);
-
}
-
return tempCollection;
-
}
-
#endregion
-
#region Anonymous Methods
-
public static void SetAnonPermissionsOnSite(string url)
-
{
-
SPWeb web = ReturnSPWeb(url);
-
-
bool ura = web.HasUniqueRoleAssignments;
-
if (!ura)
-
{
-
web.BreakRoleInheritance(true);
-
}
-
web.AnonymousPermMask64 = SPBasePermissions.FullMask;
-
web.BreakRoleInheritance(ura);
-
}
-
-
public static void SetAnonPermissionsOnList(string url, string listName)
-
{
-
SPList list = GetList(url, listName);
-
bool ura = list.HasUniqueRoleAssignments;
-
if (!ura)
-
{
-
list.BreakRoleInheritance(true);
-
}
-
list.AnonymousPermMask64 = SPBasePermissions.ViewListItems;
-
list.BreakRoleInheritance(ura);
-
}
-
#endregion
-
#region Inheritance Methods
-
public static void BreakListInheritance(string url, string listName)
-
{
-
SPList list = GetList(url, listName);
-
list.BreakRoleInheritance(true);
-
list.Update();
-
}
-
-
public static void ResetListInheritance(string url, string listName)
-
{
-
SPList list = GetList(url, listName);
-
list.ResetRoleInheritance();
-
list.Update();
-
}
-
-
public static void BreakSiteInheritance(string url)
-
{
-
SPWeb web = ReturnSPWeb(url);
-
web.BreakRoleInheritance(true);
-
web.Update();
-
}
-
-
public static void ResetSiteInheritance(string url)
-
{
-
SPWeb web = ReturnSPWeb(url);
-
web.ResetRoleInheritance();
-
web.Update();
-
}
-
-
public static string CheckIfUniqueAssign(ISecurableObject testableObject)
-
{
-
if (testableObject.HasUniqueRoleAssignments)
-
{
-
return "This Object Is Unique From Parent!";
-
}
-
return "This Object Inherits Permission From Parent!";
-
}
-
#endregion
-
#region User Methods
-
public static void AddUserToSharePointGroup(string url, string userName, string groupName)
-
{
-
SPWeb web = ReturnSPWeb(url);
-
SPUser user = web.EnsureUser(userName);
-
SPGroup group = web.SiteGroups[groupName];
-
group.AddUser(user.LoginName, user.Email, user.Name, user.Notes);
-
group.Update();
-
}
-
-
public static void AddUserToSharePoint(string url, string userName, string role)
-
{
-
SPWeb web = ReturnSPWeb(url)
-
web.AllowUnsafeUpdates = true;
-
SPUser user = web.EnsureUser(userName);
-
SPRoleDefinitionCollection roleDef = web.RoleDefinitions;
-
SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
-
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
-
roleDefBindings.Add(roleDef[role]);
-
roleAssignments.Add(roleAssignment);
-
web.AllowUnsafeUpdates = false;
-
}
-
#endregion
-
}
-
}
Just throw your namespace at the top, and you should be ready to go!
5 Comments »
RSS feed for comments on this post. TrackBack URL





















Articles & Research
SharePoint Architecture
Personal/Off-Topic
Latest Free SharePoint Software
SecureCenter For SharePoint
SharePoint Security Assurance Program™
Free Online SharePoint Security Tools
Online SharePoint Security Health Assessment
Article Or Research Filed Under 
Great job, Adam! Wouldn’t it be even more helpful to turn these methods into Extension Methods? We could use them in a more intuitive way.
Comment by Waldek Mastykarz — September 24, 2008 @ 7:33 am
There’s a big problem with this code, you are returning a disposed SPWeb instance from the helper method ReturnSPWeb(string url).
Then you are disposing it again :
using (SPWeb web = ReturnSPWeb(url))
If you return a SPWeb from a metod it’s the caller who owns it and has to dispose of it.
IMHO a nicer approach is to use delegates:
WithWeb( string url, Action action )
{
using( SPSite site = new SPSite(url))
{
using( SPWeb web = site.OpenWeb() )
{
action(web);
}
}
}
Usage:
public static void ResetSiteInheritance(string url)
{
WithWeb(url, delegate( SPWeb web )
{
web.ResetRoleInheritance();
web.Update();
});
}
Thanks
/Jonas
Comment by Jonas — September 24, 2008 @ 7:56 am
Whoops, didn’t see that other using statement slipped in there. It’s been repaired. Thanks Jonas!
I am considering using both the extension method as well as the delegate approach, both would probably be nicer!
Comment by Adam Buenz — September 24, 2008 @ 8:00 am
Adam,
I see that you have removed the second dispose. You should keep that one and remove the dispose in the helper (ReturnSPWeb).
I know that most of the time you get away with working on a disposed SPWeb without getting any exceptions but it’s not correct. Once you dispose of an object it can’t be used safely anymore. I don’t know why MS doesn’t throw ObjectDisposedExceptions when doing this???
Modified code:
public static SPWeb ReturnSPWeb(string url)
{
using (var site = new SPSite(url))
{
return site.OpenWeb();
}
}
This requires some redundant housekeeping code and that’s why I think the delegate approach is a bit nicer.
Thanks
/Jonas
Comment by Jonas — September 26, 2008 @ 12:33 pm
checkout http://spcore.codeplex.com ( Security class ) .. let me know if you want to contribute to the project..
thanks
sandeep
Comment by Sandeep — September 28, 2009 @ 7:46 pm