Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Assign users Permission to a list item in workflow sharepoint 2010

Assign users Permission to a list item in workflow sharepoint 2010

Code behind will assign “Full access” and “Design” permissions for a given list item to users XXY and XXZ (we get via People and group fields) in workflow code.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;

namespace Mynamespace_UpdateItemPermissions
{
public sealed partial class Wrk_UpdateItemPermissions : SequentialWorkflowActivity
{
public Guid webID;
public Guid siteID;
public Guid listID;
public int itemID;
public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

public Comm_UpdateReisItemPermissions()
{
InitializeComponent();
}

private void AddUserRoleToItem (string AUserNameField, string ARoleName, SPListItem @AListItem, SPWeb @AWeb)
{
String UserName = AListItem[AUserNameField].ToString();

if (UserName == “”)
return;

SPUser User = AWeb.SiteUsers.GetByID (int.Parse(UserName.Substring(0, UserName.IndexOf(‘;’))));

SPRoleAssignment roleAssignment = new SPRoleAssignment (User.LoginName, “”, “”, “”);

SPRoleDefinition roleDefinition = AWeb.RoleDefinitions[ARoleName];

roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
AListItem.RoleAssignments.Add(roleAssignment);
}

private void SetReisItemPermissionsEx(object sender, EventArgs e)
{
SPSite site = new SPSite(siteID);
SPWeb web = site.OpenWeb(webID);
SPList list = web.Lists[listID];
SPListItem listItem = list.GetItemById(itemID);

listItem.BreakRoleInheritance(false);

foreach (SPRoleAssignment role in listItem.RoleAssignments)
{
role.RoleDefinitionBindings.RemoveAll();
role.Update();
}

AddUserRoleToItem(“XXZ”, “Full Control”, listItem, web);
AddUserRoleToItem(“XXY”, “Design”, listItem, web);

listItem.Update();

site.Close();
}

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{
webID = workflowProperties.WebId;
siteID = workflowProperties.SiteId;
listID = workflowProperties.ListId;
itemID = workflowProperties.ItemId;
}
}
}

No comments:

Post a Comment