惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 张谊

[转]ubuntu下 手动安装 LAMP 和 JAVA环境 [转]ViewState的使用 [原]SharePoint文档库上传文档 [原]SharePoint列表与文档库EventHandeler [转]ASP.NET缓存概念及其应用浅析 window.showModalDialog以及window.open用法简介 [原]欢迎加入QQ群 [原]如何在Silverlight中使用WebService绑定DataGrid [转]Silverlight中调用远程Web Service的权限问题 [转]BeanUtils接口和类 [原]Commons- BeanUtils学习笔记 [转]Apache+Tomcat负载均衡及Session绑定的实现 [原]基于Caché多维数据库的SSH实现 [原]关于支付宝API开发的一点心得 [原]Oracle中列自增的方法 [原]Java反射示例 [原]JavaSocket实现广播聊天室 [转]翻译 一些很酷的.Net技巧 《生命如一泓清水》
[转]Working with user roles and permissions in SharePoint Object Model
张谊 · 2009-09-12 · via 博客园 - 张谊

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!!!"
);
}