none
Can I list out all Site collection under a managed path? RRS feed

  • Question

  • I am looking for a SP client side code to iterate thru all site collection under a specific managed path and display on my web app root, the site collection name and url?

    In the loop, I also want to check if the logged/current user is exists in the Members List of Community Site collection!

    So that I can display all Community Sites where the current user has JOINED and became a member?

    Thanks,

    Wednesday, March 22, 2017 4:39 PM

Answers

  • You can use a search query like this:

    ContentClass:STS_Site AND Path:https://server/managed_path/*

    It will only return root URLs of the site collections whose URL starts with 'https://server/managed_path/'.

    In the code you may either issue this query via REST API:

    https://server/_api/search/query?querytext='ContentClass:STS_Site AND Path:https://server/managed_path/*'

    or via CSOM:

    using (ClientContext clientContext = new ClientContext("http://<serverName>/sites/<siteCollectionPath>"))
    {
        KeywordQuery keywordQuery = new KeywordQuery(clientContext);
        keywordQuery.QueryText = "ContentClass:STS_Site AND Path:https://server/managed_path/*";
        SearchExecutor searchExecutor = new SearchExecutor(clientContext);
        ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
        clientContext.ExecuteQuery();
    }

    It will return only site collections that are located at the URLs starting with 'Path' query parameter.



    v


    Saturday, March 25, 2017 12:06 PM

All replies


  • hello, check the following post: Enumerate all tenant’s site collections in Sharepoint Online via PowerShell. It shows example how to enumerate all site collections via client object model in PowerShell, but it easy to write the same code on C#. With this code you will just need to add filter on site collection url to filter site collections in needed managed path. For checking user membership in particular group check e.g. the following post: Check if User exist in security Group using Client OM.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, March 22, 2017 6:43 PM
  • http://sadomovalex.blogspot.in/2015/05/enumerate-all-tenants-site-collections.html

    # enumerate all site collections
        $web = $clientContext.Web
        $tenant = New-Object "Microsoft.Online.SharePoint.TenantAdministration.Tenant"
        -ArgumentList $clientContext
        $props = $tenant.GetSiteProperties(0, $true)
        $clientContext.Load($props)
        $clientContext.ExecuteQuery()
        
        foreach($sp in $props)
        {
           Do-Something $sp.Url
       }

    Thursday, March 23, 2017 11:17 AM
  • Thanks for the info. Understand the code but need to iterate only a specific managed path. Because there are several managed paths exists and having several 100s of site collection. Iterating them all and filtering it might make the page slow because I need to list them in web app using client object model. 

    Again, can I get the site collection under one managed path and then iterate them?

    Saturday, March 25, 2017 5:38 AM
  • You can use a search query like this:

    ContentClass:STS_Site AND Path:https://server/managed_path/*

    It will only return root URLs of the site collections whose URL starts with 'https://server/managed_path/'.

    In the code you may either issue this query via REST API:

    https://server/_api/search/query?querytext='ContentClass:STS_Site AND Path:https://server/managed_path/*'

    or via CSOM:

    using (ClientContext clientContext = new ClientContext("http://<serverName>/sites/<siteCollectionPath>"))
    {
        KeywordQuery keywordQuery = new KeywordQuery(clientContext);
        keywordQuery.QueryText = "ContentClass:STS_Site AND Path:https://server/managed_path/*";
        SearchExecutor searchExecutor = new SearchExecutor(clientContext);
        ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
        clientContext.ExecuteQuery();
    }

    It will return only site collections that are located at the URLs starting with 'Path' query parameter.



    v


    Saturday, March 25, 2017 12:06 PM
  • Hi Roberto,

    Any progress?

    Best regards,

    Patrick


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, April 5, 2017 1:30 AM
    Moderator