Moss-Batch custom list item Permissions

Source: Internet
Author: User
Moss-permission for bulk custom list items the permissions for documents or list items in Moss inherit from their document libraries by default. If you want to separately set the permissions for a document or list item, the Administrator must adjust the management permissions. Therefore, the administrator wants to generate or initialize the permissions of all documents in batches based on the organizational structure of the enterprise. To achieve this, you need to solve several problems: (1) how to improve user permissions (simulate administrator permissions) (2) how to set user permissions the final code is implemented in the following steps: (1) simulate administrator permissions (2) Discontinuing permissions (3) custom list item permissions code: # region clear list permissions (discontinuing inherited permissions)
Private void deletelistpermission (splistitem item)
{
Spsecurity. runwithelevatedprivileges (delegate () // use this method to simulate the Administrator account to run this event handler
{
Using (spsite site = new spsite (workflowproperties. siteid) // you do not need to use this method to dispose ().
{
Using (spweb = site. openweb (workflowproperties. webid) // obtain the web method !!!
{
Try
{
// Clear all Permissions
If (! Item. hasuniqueroleassignments) item. breakroleinheritance (false); // cancel permission inheritance for this entry. If it is "false", all permissions are removed and only the system account is retained. If it is "true ", copy the upper-level permissions.
}
Catch (exception ex)
{}
}}
}
);
}
# Endregion # region custom list item permission private void setlistpermission1 (splistitem item, string person, string spermit)
{
Spsecurity. runwithelevatedprivileges (delegate () // use this method to simulate the Administrator account to run this event handler
{
Using (spsite site = new spsite (workflowproperties. siteid) // you do not need to use this method to dispose ().
{
Using (spweb = site. openweb (workflowproperties. webid) // obtain the web method !!!
{
Try
{
Splist list = workflowproperties. List; // get the list of trigger events
Int ishave = 0; // identifies whether the user is included
Foreach (sproleassignment Ra in item. roleassignments)
{
Spprincipal sp = Ra. member;
If (person. Contains (sp. Name. tostring () // if the user already exists
{
// Remove all permissions of the Group
RA. roledefinitionbindings. removeall (); // obtain all permissions of the site
Sproledefinitioncollection roles = web. roledefinitions;
// Obtain the spermit permission
Sproledefinition viewroledefinition = roles [spermit];
// Determine whether the group/user has the spermit permission. If not, add the spermit permission.
If (! RA. roledefinitionbindings. Contains (viewroledefinition ))
{
RA. roledefinitionbindings. Add (viewroledefinition );
RA. Update ();
}
Ishave = 1;
}}
If (ishave = 0) // if this user is not included, create
{
Sproleassignment ra1 = new sproleassignment (Web. ensureuser (person); // generate a new role assignment
Ra1.roledefinitionbindings. Add (Web. roledefinitions [spermit]); // assign this role to an "approved" permission level
Item. roleassignments. Add (ra1); // bind the new permission to the List entry
}
}
Catch (exception ex)
{}
}}
}
);
}
# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.