0

I am new to the SharePoint development.

I want to write a C# code that will scan the sharepoint site and return the tree structure of all folders for specific user.

It should also show the permissions on all folders/files(who can modify/delete the files)

Is this achievable? If yes, how can I do it?

  • Do you mean all folders in a specific list ? – Ahmed Bouchaala Oct 29 '18 at 10:25
  • yes, all folders at all that exist on – Yanshof Oct 29 '18 at 13:06
3

I'm not sure if you are using SharePoint server object model or CSOM, but idea is the same. You need to crawl everything starting from the site collection level, to subsite and then to document library, list, folder and to the file level. On every object which has unique permissions you need to check RoleAssignments property and check if your specific user is inside. But, if permissions are assigned through SharePoint group, you need to check the members of this group if user is inside. Also, if permissions are assigned through Active Directory group, you need to connect to AD and check if user is inside. Here is a code snippet for the server object model.


SPSite site = new SPSite("https://urlOfTheSiteCollection");
// go through all subsites in specific site collection
foreach (var web in site.AllWebs)
{
    if (web.HasUniqueRoleAssignments || web.IsRootWeb)
    {
        // load subsite permissions
    }

    // go through all lists in current subsite
    foreach (var list in web.Lists)
    {
        if (list.HasUniqueRoleAssignments)
        {
            // load list permissions
        }

        // go through all list items in current list
        foreach (var listItem in list.Items)
        {
            if (listItem.HasUniqueRoleAssignments)
            {
                // load list item permissions
            }
        }
    }
}

// load permissions for specific object - Web, List and ListItem have RoleAssignments property from which you can extract members and their permission levels
foreach (var roleAssignment in object.RoleAssignments) // object can be Web, List, ListItem
{
    var member = roleAssignment.Member // get the user -> here you can check if this is the user you are searching for. Member property can also be group so you need to retrieve members

    // get permission levels assigned to user
    foreach (var roleDefinition in roleAssignment.RoleDefinitionBIndings) 
    {
        var permissionLevel = roleDefinition.Name;
    }
}

We have a tool SysKit Security Manager which shows the information you wanted, it has User Permission Details report which shows exactly which permissions user has and where, across all sites, lists, folders and files. Hope you'll find this helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.