Frage

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?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top