sharepoint 2013 自定义列表eventhandle权限控制
記錄一下如何在sharepoint server 2013自定義列表中,使用eventhandle控制自定義列表custom list的條目item權限。
///<summary>
??????? ///正在添加項.
??????? ///</summary>
??????? public override void ItemAdding(SPItemEventProperties properties)
??????? {
??????????? base.ItemAdding(properties);
??????????? SPSecurity.RunWithElevatedPrivileges(delegate()
???????? ???{
??????????????? if(properties.ListTitle =="工作日報")
??????????????? {
??????????????????? string DepartmentName ="";
??????????????????? if(properties.AfterProperties["Department"] !=null)
??????????????????? {
???????????????????????DepartmentName =properties.AfterProperties["Department"].ToString();
??????????????????? }
??????????????????? if (DepartmentName =="IT部門")
??????????????????? {
???????????????????????properties.ErrorMessage="該部門暫時不能填寫日報!";
???????????????????????properties.Cancel =true;
??????????????????? }
??????????????? }
??????????? });
??????? }
?
///<summary>
??????? ///已添加項.
??????? ///</summary>
??????? public override void ItemAdded(SPItemEventProperties properties)
??????? {
??????????? base.ItemAdded(properties);
??????????? SPSecurity.RunWithElevatedPrivileges(delegate()
??????????? {
??????????????? if(properties.ListTitle =="工作日報")
??????????????? {
??????????????????? SPSite site=newSPSite(properties.SiteId);
???????? ???????????SPWeb web=site.OpenWeb();
???????????????????
??????????????????? web.AllowUnsafeUpdates = true;
??????????????????? SPList list =web.Lists[properties.ListId];
?
??????????????????? SPListItem item =list.GetItemById(properties.ListItemId);
?
? ??????????????????//權限斷開,并且移除掉其它權限
??????????????????? if(!item.HasUniqueRoleAssignments)
??????????????????? {
???????????????????????item.BreakRoleInheritance(false);
??????????????????? }
??????????????????? //添加用戶權限
??????????????????? SPUser CurrentUser =web.EnsureUser(properties.UserLoginName);
??????????????????? SPRoleAssignment MyRoleAssignment =newSPRoleAssignment(CurrentUser);
???????????????????MyRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Contributor));
???????????????????item.RoleAssignments.Add(MyRoleAssignment);
?
??????????????????? //添加上級領導權限
?
??????????????????? //1.根據當前用戶,獲取到該用戶的上級領導
??????????????????? SPList EmployRelationList= web.Lists["員工關系表"];
??????????????????? SPQuery query =newSPQuery();
??????????????????? query.Query = @"<Where><Eq><FieldRefName=""EmployeeId"" LookupId=""TRUE""/><Value Type=""User"">" +properties.CurrentUserId +"</Value></Eq></Where>";
??????????????????? SPListItemCollection sic =EmployRelationList.GetItems(query);
?
??????????????????? DataTable dt =sic.GetDataTable();
??????????????????? string LeaderUserId =string.Empty;
??????????????????? if (dt !=null &&dt.Rows.Count > 0)
??????????????????? {
???????????????????????LeaderUserId = dt.Rows[0]["Leader"].ToString();
??????????????????? }
??????????????????? //2.將當前上級領導添加到該項目權限中
??????????????????? if (LeaderUserId !=null &&LeaderUserId!="")
??????????????????? {
???????????????????????SPUser LeaderUser =web.EnsureUser(LeaderUserId);
???????????????????????MyRoleAssignment =newSPRoleAssignment(LeaderUser);
???????????????????????MyRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Reader));
???????????????????????item.RoleAssignments.Add(MyRoleAssignment);
??????????????????? }
??????????????????? item.Update();
??????????????????? web.Dispose();
??????????????? }
??????????? });
??????? }
?
轉載于:https://www.cnblogs.com/jiangu66/p/3184482.html
總結
以上是生活随笔為你收集整理的sharepoint 2013 自定义列表eventhandle权限控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery页面加载的时候加载函数
- 下一篇: ant安装过程,配置过程