title: Code to list all permissions for SharePoint Folders
body: <p>I'm after some C# code to recursively enumerate all the folders in a SharePoint web site and list the permissions applying to them to be run from a Sharepoint client machine. Can anyone provide or point me to an example?</p>
  
  <p>The following code can perform this function on a server using SPSite object   ( from  <a href="https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8c7c5735-039e-4cb9-a2b5-58d70a10793f/get-permissions-group-from-folders-tree-view-on-a-doc-library?forum=sharepointdevelopmentprevious" rel="nofollow">https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8c7c5735-039e-4cb9-a2b5-58d70a10793f/get-permissions-group-from-folders-tree-view-on-a-doc-library?forum=sharepointdevelopmentprevious</a>) but I need to run it using SharePoint Client code</p>
  
  <pre><code>public static void getPermissionsOfFolders()
          {
              using (SPSite site = new SPSite("http://sp"))
              {
                  using (SPWeb web = site.RootWeb)
                  {
                      SPList list = web.GetList("/Lists/List2");
                      foreach (SPListItem item in list.Folders)
                      {
                          Console.WriteLine("ID: "+item["ID"]+"--"+item.SortType);
                          if (SPFileSystemObjectType.Folder == item.SortType)
                          {
                              SPRoleAssignmentCollection roles = item.RoleAssignments;
                              foreach (SPRoleAssignment role in roles)
                              {
                                  Console.WriteLine("~");
                                  Console.WriteLine("Name: "+role.Member.Name);
  
                                  SPRoleDefinitionBindingCollection bindings = role.RoleDefinitionBindings;
  
                                  XmlDocument doc = new XmlDocument();
                                  doc.LoadXml(bindings.Xml);
                                  XmlNodeList itemList = doc.DocumentElement.SelectNodes("Role");
                                  foreach (XmlNode currNode in itemList)
                                  {
                                      string s = currNode.Attributes["Name"].Value.ToString();
                                      Console.WriteLine("Permission Level: "+s);
                                  }
                              }
                              Console.WriteLine("--------------------------------------");
                          }  
                      }                                  
                  }
              }
          }
  </code></pre>
  
  <p>Code below fails with exception "Property ListItemAllFields not found"  as shown below on <code>clientContext.ExecuteQuery()</code></p>
  
  <pre><code>   private void ListSPPermissions3()
      {
              string sSite = "http://server2012a/sites/TestDocs/";
              using (var clientContext = new ClientContext(sSite))
              {
                  Site site = clientContext.Site;
                  Web web = clientContext.Web;
                  List list = web.Lists.GetByTitle("Shared Documents");
  
                  clientContext.Load(list.RootFolder.Folders); //load the client object list.RootFolder.Folders
                  clientContext.ExecuteQuery();
                  int FolderCount = list.RootFolder.Folders.Count;
  
                  foreach  (Microsoft.SharePoint.Client.Folder folder in list.RootFolder.Folders)
                  {
  
                          RoleAssignmentCollection roleAssCol = folder.ListItemAllFields.RoleAssignments;
  
                          clientContext.Load(roleAssCol); 
                          clientContext.ExecuteQuery(); // Exception property ListItemAllFields not found
  
                          foreach (RoleAssignment roleAss in roleAssCol)
                          {
                              Console.WriteLine(roleAss.Member.Title);
                          }
                  }
              }
      }
  </code></pre>