Adis Jugo

The Southern Side – SharePoint thoughts and bytes

Working with user roles and permissions in SharePoint Object Model


Warning: pack() [function.pack]: Type H: illegal hex digit L in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit x in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit Q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit M in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit v in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit l in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit O in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit T in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit L in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit x in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit Q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit M in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit v in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit l in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit O in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit T in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit L in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit x in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit Q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit M in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit v in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit l in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit O in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit T in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit L in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit x in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit Q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit M in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit v in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit g in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit l in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit O in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit W in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit T in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit S in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit j in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit q in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

Warning: pack() [function.pack]: Type H: illegal hex digit I in /home/nohufycu/public_html/blog.sharedove.com/adisjugo/wp-content/plugins/wp-recaptcha/recaptchalib.php on line 237

This post explains how to govern user roles and permissions from the SharePoint Object Model. Creating SharePoint groups, adding users to the group, setting permissions for the group and check the existing permissions are explained.

In this example, I’ll create a SharePoint group using the Object Model, add few users in that group – which will be single users as well as the whole AD groups, create a folder inside the existing SharePoint Document library, break it’s permissions inheritance to the parent Document Library, and create new permissions model adding to a single user full rights and to newly created SharePoint group read only rights. At the end, I’ll check permissions for any given user if (s)he has rights to do the certain operations on the folder items (read, add, edit…).

string groupName1 = "TestGroup1";
SPUser ownerUser = m_SharePointWeb.SiteUsers["PLAN-B\\ajugo"];

//Add the group to the SPWeb web
m_SharePointWeb.SiteGroups.Add(groupName1, ownerUser, ownerUser, "Test group");

//Associate the group to the SPWeb
m_SharePointWeb.AssociatedGroups.Add(m_SharePointWeb.SiteGroups[groupName1]);

//add some more users and AD groups to this SP Group
m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\\user1", "use...@plan-b-gmbh.com", "User 1", "User 1 from Management");
m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\\user2", <a href="mailto:use...@planb.eu">user2@</a>plan-b-gmbh.com, "User 2", "User 2 from Sales");
m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\\user3", use...@plan-b-gmbh.com, "User 3", "User 3 from backoffice");

m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\\development", "devg...@plan-b-gmbh.com", "Development", "The whole development AD Group");

//update groups
m_SharePointWeb.SiteGroups[groupName1].Update();

//update web
m_SharePointWeb.Update();

To delete the group:

m_SharePointWeb.SiteGroups.Remove(groupName1);
m_SharePointWeb.Update();

 

Give permissions for groups and users to a SharePoint entity (SPWeb, SPList, SPListItem…)

In this example, I’ll create a folder inside the existing SharePoint library, break permissions inheritance on the folder level and give rights to one user and one SPGroup to this folder:

//get the existing document library
SPListCollection docLibs = m_SharePointWeb.GetListsOfType(SPBaseType.DocumentLibrary);
SPDocumentLibrary DocLib = (SPDocumentLibrary)(docLibs["DocLibraryName"]);

//create folder
SPFolder folderTest2 = createDocumentLibraryFolder(DocLib.RootFolder, "TestFolder");

//break role inheritance
folderTest2.Item.BreakRoleInheritance(false);

//folder update
folderTest2.Update();

//now, give FULL PERMISSIONS permissions to User1
SPRoleDefinition role = m_SharePointWeb.RoleDefinitions["Full Control"];
SPRoleAssignment roleAssignment;
SPUser oneUser = m_SharePointWeb.SiteUsers[@"PLAN-B\user1"];
roleAssignment = new SPRoleAssignment(oneUser);
roleAssignment.RoleDefinitionBindings.Add(role);
folderTest2.Item.RoleAssignments.Add(roleAssignment);

//and the readonly rights to the existibg SP Group
SPGroup group2 = m_SharePointWeb.SiteGroups["Test group"];
SPRoleAssignment group2RoleAssigment = new SPRoleAssignment(group2);
SPRoleDefinition groupRoleDefinition = m_SharePointWeb.RoleDefinitions["Read"];
group2RoleAssigment.RoleDefinitionBindings.Add(groupRoleDefinition);
folderTest2.Item.RoleAssignments.Add(group2RoleAssigment);

//folder update
folderTest2.Update();

//web update
m_SharePointWeb.Update();
Check if a specific user has a certain permissions on SPItem, SPList or SPWeb objects
//check if the user has permissions to add new item in the folder
SPUser userToCheck = m_SharePointWeb.SiteUsers[@"PLAN-B\user1"]

if (folderItem.DoesUserHavePermissions(userToCheck, SPBasePermissions.AddListItems))
{
   Trace.WriteLine("User has permissions to add list items!!!");
}
else
{
   Trace.WriteLine("User DOES NOT HAVE permissions to add list items!!!");
}

Sun, May 18 2008 » Development

Share 'Working with user roles and permissions in SharePoint Object Model' on Facebook Share 'Working with user roles and permissions in SharePoint Object Model' on LinkedIn Share 'Working with user roles and permissions in SharePoint Object Model' on Twitter Share 'Working with user roles and permissions in SharePoint Object Model' on XING

11 Responses

  1. slim May 23 2008 @ 15:58

    Fantastic article – really helped me out of a tight corner, thank you!!

  2. yagya datt June 30 2008 @ 08:13

    Really nice. it helped me a lot.

    thank you

  3. greate article October 1 2008 @ 21:26

    Really helped a lot.

  4. whew! November 6 2008 @ 22:35

    Yes, this helped me, too. Thanks very much.

  5. saurabh November 7 2008 @ 06:12

    Hi Adis…….thanx for a very nice post…

    but i have a question for u…..

    Can we create any Custom Roles….??

    if yes then can you plz explain us…..?

    thanx n Regards

    Saurabh

  6. chris November 13 2008 @ 03:12

    if the “userToCheck” is a member of a group, and the group is included in RoleAssignments, DoesUserHavePermissions is not ok.

  7. Bander Alsharfi November 25 2008 @ 13:08

    Love it =)

    Thanks a billion!!!

  8. Jessica February 3 2009 @ 12:26

    The same as the other comments, thank you very much

  9. Chandrakanth February 17 2009 @ 14:41

    wow this is a very good Article to create Groups and add Users using sharepoint object model …. Chandakanth

  10. hodza April 1 2009 @ 15:04

    Thanks a lot you ve just save my life ..!!!!

  11. rajkumar July 22 2009 @ 08:46

    Is this solution will work for all the Sharepoint user or only for site admin users.

One Ping

  1. Working with user roles and permissions in SharePoint Object Model – part 2 @ Adis Jugo May 13 2008 @ 16:27

Leave a Reply