I have a web part that needs to identify the site collections the logged-in user has access to. Can Search help with this? Are there better options for me aside from:

  1. The web part looping through each site collection to check.
  2. Creating a timer job that loops through each site collection to check and stores the results in a list. Then the web part queries from the aforementioned list.

Thank you for your time!

2 Answers 2


You can very well use Search API for getting the list of site collection current user has access to, provided that you have a good crawl system maintained , probably continuous crawl enabled(only in SP 2013). If you run the KeywordQuery search on current logged in user's context , it will give security trimmed results and it will give only site user have access to.

Below code can be used as a starting point:

KeywordQuery query = new KeywordQuery(site);
                    query.QueryText = "ContentClass:STS_Site";
                    query.RowLimit = 500;//max row limit is 500 for KeywordQuery
                    query.ResultsProvider = SearchProvider.Default;
                    query.EnableStemming = true;
                    query.TrimDuplicates = false;
                    query.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
                    query.KeywordInclusion = KeywordInclusion.AllKeywords;
                    SearchExecutor executor = new SearchExecutor();
                    ResultTableCollection resultTableCollection = executor.ExecuteQuery(query);
                    var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
                    var resultTable = resultTables.FirstOrDefault();

You can use the following code to check the permission:

using (SPSite site = new SPSite("http://localhost"))
                using (SPWeb web = site.OpenWeb())
                    // Make sure the current user can enumerate permissions.
                    if (web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
                        // Specify the permission to check.
                        SPBasePermissions permissionToCheck = SPBasePermissions.ManageLists;
                        Console.WriteLine("The following users have {0} permission:", permissionToCheck);

                        // Check the permissions of users who are explicitly assigned permissions.                   SPUSer user = web.EnsureUser(loginName);

                            string login = user.LoginName;
                            if (web.DoesUserHavePermissions(login, permissionToCheck))


Also you can refer : http://msdn.microsoft.com/en-us/library/ms441848.aspx


  • Hi Aanchal, thanks for the help! This code can also help but I'm refraining by one-by-one checking each site collection. :)
    – Celbester
    Jun 6, 2014 at 9:40

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.