This topic is about removing all CONTRIBUTOR permissions from an item created in a list and give the rights to the person who is in Assigned To field. To achieve this, simply create a SharePoint Sequential WorkFlow in VS 2010.
Open Your VS 2010, Select SharePoint template in Visual C#. From the right Pane select Sequential WorkFlow. Than give a name to this WorkFlow and location to save this solution
Enter the Site Name and Farm Solution if you are farm admin or Sandbox if you are site collection admin.
Select the type of WorkFlow type.
Select The List for in that site for which you want to create a workflow. In our case it is the Task List.
Finally select the when and how this workflow will trigger.
By doing the above steps we have created a WorkFlow in Tasks Lists and it will trigger automatically whenever a new item is created or it is modified.
To Start with VS 2010 will provide us the following Code:
public sealed partial class Workflow1 : SequentialWorkflowActivity
public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
Now first we will create an item and assign it to a user.
string listUrl = "/Lists/Tasks";
string User = "domain\\username";
SPList list = web.GetList(listUrl);
SPListItem Litem = list.Items.Add("http://it-35/lists/Tasks, SPFileSystemObjectType.File, null);
spuser = web.EnsureUser(user);
Litem["Assigned To"] = spuser;
Now we have created an item in our Task List. The next step is to remove all the rights of Contributor and then only give Contributor rights to the person whose name is in Assigned To field.
SPRoleAssignmentCollection SPRoleAssColn = Litem.RoleAssignments;
for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
Now here we will assign rights
SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(spuser);
This code will do the job for you. Please do leave comments if you like this post or if you have any questions.