[SP2013] Getting a list of site collections using search

Getting a list of site collections from SharePoint 2013 sounds like a pretty trivial task of which you would say the client side object model is capable of handling. Well when you’re chatting with Office365 it is. And even when you’re on-premises, there’s some hacking you can do in order to utilize the same tenant-based solution. Vesa Juvonen detailed here what you need to do first (the part about setting a site collections AdministrationSiteType property).

But what if you’re chatting against an on-prem environment, but are not allowed to make these kind of (Powershell) changes? Well in that case your only option left is to query search for the list of site collections it knows about. Be aware that this is not bullet proof! It’s depending on the search index and it will security trim any sites you do not have access to (so use an account that has access on webapplication level). Also, search is limited to giving back a maximum of 500 results per query. Fortunately, there’s some paging logic you can use to get the entire collection back in batches. If you’re interested, check out the following code snippet.

public IEnumerable<string> GetSiteCollections(string webAppUrl)
{
    int pageNumber = 1;
    int pageSize = 200;
    HashSet<string> uniqueUrls = new HashSet<string>();

    Trace.TraceInformation("Retrieving the site collections using SharePoint search API.");

    try
    {
        using (ClientContext context = ConnectionHelper.GetClientContext(webAppUrl))
        {
            int resultCount;
            int totalRows;
            ClientResult<ResultTableCollection> searchResults;

            do
            {
                KeywordQuery query = new KeywordQuery(context);
                query.QueryText = "contentclass:\"STS_Site\"";
                query.EnableStemming = true;
                query.TrimDuplicates = false;
                SearchExecutor searchExecutor = new SearchExecutor(context);

                int startRow = (pageNumber++ - 1) * pageSize;
                query.RowLimit = pageSize;
                query.StartRow = startRow;
                searchResults = searchExecutor.ExecuteQuery(query);
                context.ExecuteQueryRetry();

                var data = searchResults.Value.SelectMany(rs => rs.ResultRows.Select(r => r["Path"] as string)).ToList();
                resultCount = searchResults.Value.Sum(rs => rs.ResultRows.Count());

                totalRows = searchResults.Value.Sum(rs => rs.TotalRows);

                foreach (string url in data)
                    uniqueUrls.Add(url);        // HashSet.Add will only add items which are not already in the set, so removing duplicates

                Trace.TraceInformation("Wrote {0} items to the result set, which now has a total of {1} items. TotalRows = {2}", resultCount, uniqueUrls.Count(), totalRows);
            } while (resultCount > 0);
        }
    }
    catch (Exception ex)
    {
        Trace.TraceWarning("Error occurred whilst using the search API to query for site collections: " + ex.ToString());
    }

    return uniqueUrls.ToArray();
}

 

 

Related posts

[Azure] Setting custom domain with indirect verification via ARM

Getting a list of site collections from SharePoint 2013 sounds like a pretty trivial task of which you would say the client side object model is capable of handling. Well when you're chatting with Office365 it is. And even when you're on-premises, there's some hacking you can do in order to utilize the same tenant-based solution. Vesa Juvonen detailed here what you need to do first (the part about setting a site collections AdministrationSiteType property).

But what if you're chatting against an on-prem environment, but are not allowed to make these kind of (Powershell) changes? Well in that case your only option left is to query search for the list of site collections it knows about. Be aware that this is not bullet proof! It's depending on the search index and it will security trim any sites you do not have access to (so use an account that has access on webapplication level). Also, search is limited to giving back a maximum of 500 results per query. Fortunately, there's some paging logic you can use to get the entire collection back in batches. If you're interested, check out the following code snippet.

[Azure] Regain admin access to your Azure AD tenant custom domain

Getting a list of site collections from SharePoint 2013 sounds like a pretty trivial task of which you would say the client side object model is capable of handling. Well when you're chatting with Office365 it is. And even when you're on-premises, there's some hacking you can do in order to utilize the same tenant-based solution. Vesa Juvonen detailed here what you need to do first (the part about setting a site collections AdministrationSiteType property).

But what if you're chatting against an on-prem environment, but are not allowed to make these kind of (Powershell) changes? Well in that case your only option left is to query search for the list of site collections it knows about. Be aware that this is not bullet proof! It's depending on the search index and it will security trim any sites you do not have access to (so use an account that has access on webapplication level). Also, search is limited to giving back a maximum of 500 results per query. Fortunately, there's some paging logic you can use to get the entire collection back in batches. If you're interested, check out the following code snippet.

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *