So let’s say you’re building a web part or a custom SharePoint (server/services) page and you need to know what roles are there on a particular list or even object so that you control what is being showed to the user, hide or show relevant information etc (example: http://blogs.msdn.com/modonovan/archive/2005/07/07/436394.aspx).
If you’re a seasoned SPS veteran you’ll first go for the SPRole (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sprole.aspx) and SPRoleCollection (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sprolecollection.aspx) which you’ll soon learn, as your trusty VS 2008 will inform you, has become obsolete, but it will let you use it.
So you probably would’ve used it like this:
SPWeb currentWeb = SPControl.GetContextWeb(Context);
foreach (SPRole role in currentWeb.Roles)
{
//I guess you would put your code here
Console.WriteLine(role.Name);
}
Then you remember how limiting is was and you wonder, well if this is obsolete, what has replaced it?
Well this is what:
SPRoleAssignment (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sproleassignment.aspx )
SPRoleAssignmentCollection (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sproleassignmentcollection.aspx)
Examples of RoleAssignments:
using (SPSite site = new SPSite("http://yourspsite/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList yourList = web.Lists["Your List Name"];
foreach (SPRoleAssignment role in yourList.RoleAssignments)
{
//role.Member gives you the SPPrincipal type object, which is a user or a group, .Name gives you the Name of the User/Group
Console.WriteLine(role.Member.Name);
//here you can add your code which might include Add() and Update() of the RoleAssignments Collection (yourList.RoleAssignments)
}
}
}
using (SPSite site = new SPSite("http://yourspsite/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList yourList = web.Lists["Your List Name"];
//the output will look something like this :
//<permissions><permission memberid='33' mask='9223372036854775807' />
//<permission memberid='34' mask='1856436900591' />
//<permission memberid='35' mask='756052856929' />
//</permissions>
// this gives you users and/or groups and their security masks
Console.WriteLine(yourList.RoleAssignments.Xml);
//you can use LINQ to parse and iterate through the xml
XElement listGroupIDs = XElement.Parse(yourList.RoleAssignments.Xml);
foreach (XElement x in listGroupIDs.Elements("permission"))
{
//extract the values and use them to find and/or instantiate the values of groups and objects and even manipulate them
Console.WriteLine(x.Attribute("memberid").Value);
}
}
}
Although the following two classes are considered to be obsolete:
SPPermission (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sppermission.aspx)
SPPermissionCollection (http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.sppermissioncollection.aspx)
There is one really cool thing about SPPermissionCollection in particular which is its XML property which is a bit more descriptive then the same property from the SPRoleAssignmentCollection class.
Example:
using (SPSite site = new SPSite("http://yourspsite/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList yourList = web.Lists["Your List Name"];
//the output will look something like this :
//<Permissions>
//<Permission MemberID="33" Mask="-1" MemberIsUser="False" MemberGlobal="True" GroupName="Processes Owners" />
//<Permission MemberID="34" Mask="1011028719" MemberIsUser="False" MemberGlobal="True" GroupName="Processes Members" />
//<Permission MemberID="35" Mask="138612833" MemberIsUser="False" MemberGlobal="True" GroupName="Processes Visitors" />
//</Permissions>
//which gives you Group/UserNames and not just MemberIDs,
Console.WriteLine(yourList.Permissions.Xml);
//you can use LINQ to parse and iterate through the xml
XElement listGroupIDs = XElement.Parse(yourList.Permissions.Xml);
foreach (XElement x in listGroupIDs.Elements("Permission"))
{
//extract the values and use them to find and/or instantiate the values of groups and objects and even manipulate them
Console.WriteLine(x.Attribute("GroupName").Value);
}
}
}