[转]Working with user roles and permissions in SharePoint Object Model
Working with user roles and permissions in SharePoint Object Model
m_SharePointWeb.SiteGroups.Add(groupName1, ownerUser, ownerUser, "Test group");
m_SharePointWeb.AssociatedGroups.Add(m_SharePointWeb.SiteGroups[groupName1]);
//update groups
m_SharePointWeb.SiteGroups[groupName1].Update();
m_SharePointWeb.Update();
m_SharePointWeb.Update();
//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[@"DAENET\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 the user has permissions to add new item in the folder
SPUser userToCheck = m_SharePointWeb.SiteUsers[@"DAENET\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!!!");
}