[转]Working with user roles and permissions in SharePoint Object Model

Working with user roles and permissions in SharePoint Object Model

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. On the end, I'll check permissions for any given user if (s)he has rights to do the ceratain operations on the folder items (read, add, edit...). 
 
string groupName1 = "TestGroup1";
SPUser ownerUser = m_SharePointWeb.SiteUsers["DAENET\\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("DAENET\\user1", " user1@daenet.eu ", "User 1", "User 1 from Management");
m_SharePointWeb.SiteGroups[groupName1].AddUser("DAENET\\user2", user2@daenet.eu , "User 2", "User 2 from Sales");
m_SharePointWeb.SiteGroups[groupName1].AddUser("DAENET\\user3", user3@daenet.eu , "User 3", "User 3 from backoffice");

 
m_SharePointWeb.SiteGroups[groupName1].AddUser("DAENET\\development", " devgroup@daenet.de ", "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[@"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 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[@"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!!!"
);
}

 

转载于:https://www.cnblogs.com/zhangyi85/archive/2009/09/12/1565512.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值