Friends
cud u tell me the best practice to achieve my requirement..
my Req. is..
I have a list with a my custom drop down control that having some users name (i get them by some custom logic),
when Item is being created I want this item to be accessed by only users
1) who is Created by
2) and those are being selected by this Dropdown.
Thanks Michael..
I have a Dropdown User with multiple select option and I dont know how will i get all selected user from dropdown in WorkFlow. That’s why I am preferring Code.
pls tell me if I cud do that in workflow…
here mentioning the code what I am using..
public void SetItemLevelPermission(SPItemEventProperties properties) { SPWeb web = null;
try
{
SPUser currentUser = null;
web = properties.OpenWeb();
currentUser = web.CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite elevatedSite = new SPSite(properties.SiteId))
{
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(properties.ListItem.Web.ID))
{
SPList elevatedList = elevatedWeb.Lists[properties.ListId];
SPListItem elevatedListItem = elevatedList.Items.GetItemById(properties.ListItem.ID);
elevatedListItem.Web.AllowUnsafeUpdates = true;
// Break inheritance and clear permissions on list item
if (!properties.ListItem.HasUniqueRoleAssignments)
{
elevatedListItem.BreakRoleInheritance(false);
}
// Add Add and Edit permissions for current user
SPUser Usr = GetUser(web);
this.SetPermissionsForUser(elevatedWeb, elevatedListItem, Usr);
this.EventFiringEnabled = false;
elevatedListItem.SystemUpdate();
this.EventFiringEnabled = true;
elevatedListItem.Web.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception Ex)
{
throw;
}
}
public SPUser GetUser(SPWeb web) {
SPUser userName = null;
foreach (SPUser usr in web.SiteUsers)
{
if (usr.Name == “A1”)
{
userName = usr;
return userName;
}
}
return null;
}
private void SetPermissionsForUser(SPWeb elevatedWeb, SPListItem elevatedListItem, SPUser Usr)
{
SPRoleDefinition roleDefinition = elevatedWeb.RoleDefinitions[“Only Edit Custom”];
SPRoleAssignment roleAssignment = new SPRoleAssignment(Usr.LoginName, Usr.Email, Usr.Name, string.Empty);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
elevatedListItem.RoleAssignments.Add(roleAssignment);
}
and problem with this code is..its working well with the SP-2010 but not in SP-2013.
You could use a workflow with an impersonation step. First remove unwanted permissions and then grant permissions based on your dropdown column. I have done this before on a task list to only grant access to the person/people in the assigned to column.
Â