Select Page

Managing user roles and permissions through SharePoint Object Model in SharePoint 2010

This post explains how to govern user roles and permissions from the SharePoint Object Model for SharePoint 2010. 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

//add some more users and AD groups to this SP Group
m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\user1", "user1<AT>", "User 1", "User 1 from Management");
m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\user2", user2<AT>, "User 2", "User 2 from Sales");
m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\user3", user3<AT>, "User 3", "User 3 from backoffice");

m_SharePointWeb.SiteGroups[groupName1].AddUser("PLAN-B\development", "devgroup<AT>", "Development", "The whole development AD Group");

//update groups

//update web


To delete the group:




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

//folder update

//now, give FULL PERMISSIONS permissions to User1
SPRoleDefinition role = m_SharePointWeb.RoleDefinitions["Full Control"];
SPRoleAssignment roleAssignment;
SPUser oneUser = m_SharePointWeb.SiteUsers[@"PLAN-Buser1"];
roleAssignment = new SPRoleAssignment(oneUser);

//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"];

//folder update

//web 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-Buser1"]

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