2

I am working on office 365 SharePoint site.

I want to fetch all lists/libraries/items where permissions are given to Everyone group across SharePoint site using CSOM.

    Web web = context.Web;
var Lists = context.Web.Lists;
context.Load(Lists);
context.ExecuteQuery();
foreach (var List in Lists)
{
    context.Load(List.RoleAssignments);
    context.ExecuteQuery();
    foreach (var RoleAssignment in List.RoleAssignments)
    {
        context.Load(RoleAssignment.Member);
        context.ExecuteQuery();
        var EveryoneexceptexternalUsers = " c:0-.f|rolemanager|spo-grid-all-users/526bf739-e2f7-4c87-a8ce-0737198cc796";
        PrincipalType SharePointGroup = default(PrincipalType);
        if (RoleAssignment.Member.PrincipalType == SharePointGroup && RoleAssignment.Member.LoginName == EveryoneexceptexternalUsers) {
            Console.WriteLine(List.Title, "has Everyone Group");

        }
        else
        {
            Console.WriteLine("Everyone group is not present in the List");
        }
        Console.ReadLine();
    }
}
1
  • I didn't run the PowerShell script. Instead of PowerShell converted that into csom. I have posted my code here. I have added everyone group in a list. when I run the code it is not fetching the list name instead of that it is executing else condition.
    – Liz
    Jul 26, 2019 at 8:07

3 Answers 3

0

To achieve your requirement, there is a PowerShell script for your reference.

    #Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables for Processing
$SiteUrl = "https://tenantname.sharepoint.com/sites/sitename"
$UserName="username"
$Password ="password"

#Setup Credentials to connect

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))


    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 

    $Context.Credentials = $credentials


    #sharepoint online powershell get all lists

    $Lists = $Context.web.Lists

    $Context.Load($Lists)

    $Context.ExecuteQuery()



    #Iterate through each list in a site   

    ForEach($List in $Lists)
    {

        $Context.Load($List.RoleAssignments)
        $Context.ExecuteQuery()
        Foreach($RoleAssignment in $List.RoleAssignments)
        { 
               $Context.Load($RoleAssignment.Member)

               $Context.executeQuery()

               If($RoleAssignment.Member.PrincipalType -eq "SharePointGroup" -And $RoleAssignment.Member.LoginName -eq "Everyone"){

                    Write-host $List.Title "has Everyone Group"

                } 

        }




    }   

More reference:

SharePoint Online: Site Collection Permissions Report using PowerShell.

3
  • But still not able to fetch.
    – Liz
    Jul 24, 2019 at 11:31
  • did you encounter the error when running the powershell script?
    – Amy_MSFT
    Jul 25, 2019 at 1:49
  • I didn't run the PowerShell script. Instead of PowerShell converted that into csom. I have posted my code here. I have added everyone group in a list. when I run the code it is not fetching the list name instead of that it is executing else condition.
    – Liz
    Jul 25, 2019 at 6:10
0
 Web web = context.Web;
                var Lists = context.Web.Lists;
                context.Load(Lists);
                context.ExecuteQuery();
                foreach (var List in Lists)
                {
                    context.Load(List.RoleAssignments);
                    context.ExecuteQuery();
                    Console.WriteLine(List.Title);
                    if (List.Title.StartsWith("Employee"))
                    {
                        foreach (var RoleAssignment in List.RoleAssignments)
                        {
                            context.Load(RoleAssignment.Member, r => r.LoginName, r => r.PrincipalType);
                            context.ExecuteQuery();
                            var EveryoneexceptexternalUsers = "c:0-.f|rolemanager|spo-grid-all-users/526bf739-e2f7-4c87-a8ce-0737198cc796";
                            //PrincipalType SharePointGroup = default(PrincipalType);
                            if (RoleAssignment.Member.LoginName == EveryoneexceptexternalUsers)//RoleAssignment.Member.PrincipalType == SharePointGroup && 
                            {
                                Console.WriteLine(List.Title, "has Everyone Group");
                            }
                            else
                            {
                                Console.WriteLine("Everyone group is not present in the List");
                            }
                        }
                    }
                    Console.ReadLine();
                }
2
  • I have done small changes now it is working
    – Liz
    Jul 26, 2019 at 10:18
  • Note that neither this nor the other answers here address your requirement to check permissions on list items that may have unique permissions
    – sasfrog
    Jul 26, 2019 at 21:55
0

I prefer different approach, especially in large environments. Instead of iterating all the content, which might take days and weeks for a good mature tenant, I use search:

  1. You'd need to register an account and do not provide to this account any permissions to SharePoint sites
  2. You'd search content on behalf of this account (I'd recommend [search with Graph API][1]). Everything you'd find would be content shared with everyone.
# Search
# MS Graph Search API url (beta or v1.0):
$apiUrl = "https://graph.microsoft.com/beta/search/query"
$apiUrl = "https://graph.microsoft.com/v1.0/search/query"

# specify where to search - entity types
$entityTypes = "['driveItem','listItem','list','drive','site']"

# query all tenant content 
$query = "*"

# query specific site 
$query = "* site:https://contoso.sharepoint.com/sites/mysite"

# build a simple request body
$body = @"
{ 
  "requests": [
    {
      "entityTypes": $entityTypes,
      "query": {
        "queryString": "$query"
      }
    }
  ]
}
"@

# call Graph API:
$res = Invoke-RestMethod -Headers $Headers -Uri $apiUrl -Body $Body -Method Post -ContentType 'application/json'

# explore returned object
$res.value[0].hitsContainers[0].hits

Here is the article on "[How to Find Content Shared with Everyone in SharePoint and Teams][2]" and video "[Discover SharePoint content shared with everyone in Microsoft 365][3]".
 


  [1]: https://vladilen.com/microsoft-365/search-m-365-content-programmatically/
  [2]: https://vladilen.com/software/microsoft-365/find-sites-shared-with-everyone-in-spo/
  [3]: https://youtu.be/3v_TxXd6ZWc?si=FDy7NIK5yoPz6ttz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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