Tuesday, December 11, 2012

Programatically get permissions for list using client object model sharepoint 2010

I was getting lot of queries for retrieving the library permissions of each user so i thought its better to put this information on blog. Below code snippet brings all the permission of list/library. Once permission information is loaded  then iterate for permission kind,group name,description. Put it in dictionary with member as key and all other as values. After filling the dictionary loop through the dictionary and for getting the user name from dictionary group names.Once all the information is received then bind to related control on user interface

 IEnumerable roles = null;
        private void GetSPPermissions(ClientContext ctx)
        {
            //get the list
            List myList = ctx.Web.Lists.GetByTitle("DocSite2_First");

            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,
            roleDef => roleDef.RoleTypeKind,
            roleDef => roleDef.BasePermissions)));
            ctx.ExecuteQuery();

            Dictionary dPermision = new Dictionary();

            foreach (RoleAssignment ra in roles)
            {
                var rdc = ra.RoleDefinitionBindings;
                string permission = string.Empty;
                foreach (var rdbc in rdc)
                {
                    permission += rdbc.Name.ToString() +","+ rdbc.Description.ToString() +","+ rdbc.RoleTypeKind.ToString()+","+ rdbc.BasePermissions.ToString();                  
                }
                dPermision.Add(ra.Member.Title, permission);
            }

No comments: