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;
}