Monday, October 25, 2010

Programmatically get permissions for list and listitems using client object model

Programmatically get permissions for list and listitems using client object model.

In this Post we have two code snippets, one for retrieving the Permissions on a specific list and
other for getting the Permissions on a specific Item using Client Object model.

Get Permissions for a List –

//get the Conext
ClientContext ctx = new ClientContext(“SPSiteUrl”);

//get the list
List myList = ctx.Web.Lists.GetByTitle(“My List”);

IEnumerable roles = null;
roles = ctx.LoadQuery(
myList.RoleAssignments.Include(

roleAsg => roleAsg.Member,

roleAsg => roleAsg.RoleDefinitionBindings.Include(

roleDef => roleDef.Name, // for each role def, include roleDef’s Name

roleDef => roleDef.Description)));

ctx.ExecuteQuery();

Retrieving permissions for a Specific item - The Code is similar to the above code but i am
using SecurableObject to hold the ListItem. Just to make things easy.

SecurableObject curObj = null;

ListItem curItem = ctx.Web.Lists.GetByTitle(“My List”).GetItemById(ItemId); -> Use ItemId of
the Item.

//plug it into our query object
curObj = curItem as SecurableObject;

IEnumerable roles = null;

roles = ctx.LoadQuery(

curObj.RoleAssignments.Include(

roleAsg => roleAsg.Member,

roleAsg => roleAsg.RoleDefinitionBindings.Include(

roleDef => roleDef.Name, // for each role def, include roleDef’s Name

roleDef => roleDef.Description)));

ctx.ExecuteQuery();

No comments:

Post a Comment