3,244
社区成员




SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.OpenWeb(properties.ListItem.ParentList.ParentWeb.ID))
{
web.AllowUnsafeUpdates = true;
// Make sure referring to the new objec created under the evelvated security context
// there seems to be some bug in web.Lists[properties.ListId].Items[properties.ListItemId] // IndexOutOfRange
SPListItem item = web.Lists[properties.ListId].Items[properties.ListItem.UniqueId];
item.BreakRoleInheritance(false);
SPRoleDefinition contributeRoleDef = web.RoleDefinitions["参与讨论"];
SPRoleDefinition readRoleDef = web.RoleDefinitions["读取"];
// the user creating this item have the Contribute permisioin level
SPRoleAssignment roleAssOfCurrentUser = new SPRoleAssignment(web.AllUsers[properties.UserLoginName]);
roleAssOfCurrentUser.RoleDefinitionBindings.Add(contributeRoleDef);
// all the authenticated user can read
SPRoleAssignment roleAssOfAllUser = new SPRoleAssignment(web.AllUsers["NT AUTHORITY\\Authenticated Users"]);
roleAssOfAllUser.RoleDefinitionBindings.Add(readRoleDef);
item.RoleAssignments.Add(roleAssOfCurrentUser);
item.RoleAssignments.Add(roleAssOfAllUser);
//获取当前登录人的部门(前提:在AD里已经设置好部门,或者在moss管理中心设置好),并且对当前日历记录赋值部门
SPUser user = web.CurrentUser;
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile profile = profileManager.GetUserProfile(properties.UserLoginName.ToString());
try
{
properties.ListItem["dept"] = profile["Department"].Value.ToString();
}
catch { }
properties.ListItem.SystemUpdate(); // NO NEED
}
}
});
web.Lists[item.ParentList.ID].Items[item.ID].BreakRoleInheritance(false);
properties.ListItem.BreakRoleInheritance(false);
SPRoleDefinition role = web.RoleDefinitions["读取"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(properties.UserLoginName, "", properties.UserDisplayName, "");
roleAssignment.RoleDefinitionBindings.Add(role);
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
web.Lists[item.ParentList.ID].Items[item.ID].RoleAssignments.Add(roleAssignment);
web.Lists[item.ParentList.ID].Items[item.ID].Update();
SPListItem item = properties.ListItem;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite(item.ParentList.ParentWeb.Site.ID))
{
using (SPWeb web = site.OpenWeb(item.ParentList.ParentWeb.ID))
{
web.AllowUnsafeUpdates = true;
SPUser user = web.CurrentUser;
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile profile = profileManager.GetUserProfile(properties.UserLoginName.ToString());
try
{
properties.ListItem["dept"] = profile["Department"].Value.ToString();
}
catch { }
//properties.ListItem.BreakRoleInheritance(false);
//});
//if (properties.ListItem.HasUniqueRoleAssignments)
//{
//properties.ListItem.BreakRoleInheritance(false);
//}
web.Lists[item.ParentList.ID].BreakRoleInheritance(false);
//这句现在好了
SPRoleDefinition role = web.RoleDefinitions["读取"];
SPRoleAssignment roleAssignment = new SPRoleAssignment(properties.UserLoginName, "", properties.UserDisplayName, "");
roleAssignment.RoleDefinitionBindings.Add(role);
SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
properties.ListItem.RoleAssignments.Add(roleAssignment);
//到这句就完了
properties.ListItem.SystemUpdate();
}
}
});