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:
namespace Permissions.Workflow1
{
public sealed partial class Workflow1 : SequentialWorkflowActivity
{
public Workflow1()
{
InitializeComponent();
}
public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = newSPWorkflowActivationProperties();
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";
SPUser spuser;
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;
Litem.update();
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.
Litem.BreakRoleInheritance(true);
SPRoleAssignmentCollection SPRoleAssColn = Litem.RoleAssignments;
for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
{
SPRoleAssColn.Remove(i);
}
Now here we will assign rights
SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);
SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(spuser);
oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);
Litem.RoleAssignments.Add(oSPRoleAssignment);
Litem.Update();
No comments:
Post a Comment