Managing tasks permissions programmatically within SharePoint using event reciever or using special

Source: Internet
Author: User

I was writing a workflow using SharePoint designer wherein at certain steps tasks were getting created and assigned to different user. but the problem with that was that any user having appropriate rights on the tasks list was able to edit the task.

Below are the two methods using which we can have only the assigned to user having the rights on that task.

It can be done using event explorer or within the SharePoint workflow using special permissions property.

Public override void itemadded (spitemeventproperties properties)

{

// Name of the List

If (properties. listtitle = "tasks ")

{

// Get the spsite object

Spsite objsite = new spsite ("http: // SERVERNAME: portname ");

// Point to the top level web site within it

Spweb objweb = objsite. openweb ();

// Get the task list item getting created

Splistitem mylistitem = properties. listitem;

 

// Get the ID of the assigned to user

// We want that only assigned to user shocould have full rights on that task

String userassignedto = mylistitem ["assigned to"]. tostring ();

Int Index = userassignedto. indexof (';');

Int id = int32.parse (userassignedto. substring (0, index ));

// Get the spuser from the ID

Spuser user = objweb. siteusers. getbyid (ID );

 

// Break the role inheritance

Mylistitem. breakroleinheritance (false );

// Webroledefinitions-full right, design, contribute and read

Sproledefinitioncollection webroledefinitions = objweb. roledefinitions;

Sproleassignment roleassignment = new sproleassignment (User );

Roleassignment. roledefinitionbindings. Add (webroledefinitions ["Full Control"]);

Mylistitem. roleassignments. Add (roleassignment );

// Give full control right to the assigned to user

Roleassignment. Update ();

 

 

}

}

Or within workflow

 

// Handler for create task activity

Private void createtask1_methodinvoking (Object sender, eventargs E)

{

// Specify properties for the task

Createtask1.taskproperties. assignedto = @ "domainusername ";

Createtask1.taskproperties. Title = @ "Please complete the task ";

Createtask1.taskproperties. Description = "this is sample SharePoint task ";

Createtask1.taskproperties. duedate = datetime. Now. adddays (7 );

Createtask1.taskproperties. emailbody = "this is the sample <B> <I> email body </B> </I> ";

Createtask1.taskproperties. sendemailnotification = true;

 

// Define a hybriddictionary object

Hybriddictionary permscollection = new hybriddictionary ();

// Give administrator rights to the user to whom the task has been assigned

Permscollection. Add (createtask1.taskproperties. assignedto, sproletype. Administrator );

// Specialpermissions-The specialpermissions property in your code will strip out all existing permissions inherited from

// The parent list (workflow task list) and only adds permissions for each pair you added to the hashtable

Createtask1.specialpermissions = permscollection;

 

}

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.