Tuesday, 20 December 2016

Client Object Model code to find List item level permissions



Client Object Model Code to find list items that has unique permissions



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;


namespace CSOMTestUserAccessItems
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext ctx = new ClientContext("http://spdev.sp2013.com/");
            ctx.AuthenticationMode = ClientAuthenticationMode.Default;
            ctx.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            Web web = ctx.Web;
            ctx.Load(web, w => w.Title);
            ctx.Load(web, w => w.CurrentUser.LoginName);
            List list = ctx.Web.Lists.GetByTitle("Products");
            CamlQuery qry = new CamlQuery();
            qry.ViewXml = "<view></view>";
            ListItemCollection Listitems = list.GetItems(qry);
            ctx.Load(Listitems);
            ctx.Load(Listitems, li => li.Include(i => i.HasUniqueRoleAssignments));
            ctx.ExecuteQuery();
            var user = web.CurrentUser.LoginName;
            foreach (ListItem item in Listitems)
            {
                Console.WriteLine();
                if ((item.HasUniqueRoleAssignments))
                {

                    ClientResult<BasePermissions> permissions = item.GetUserEffectivePermissions(@"i:0#.w|sp2013\manjunath.soreddy");
                    ctx.ExecuteQuery();
                    var hasPermissions = permissions.Value.Has(PermissionKind.ViewListItems);
                    if (hasPermissions)
                    {
                        Console.WriteLine(item["Title"] + " ...." + item["Price"] + "....." + item["Quantity"]);
                    }
                }


            }
            Console.Read();
        }
    }

}

1 comment:

  1. Hi,

    I have more than 500 items in list and its taking time to return the result because of checking the permissions for each item. Any idea how can i overcome this issue.

    ReplyDelete