There are 20+ site collections in my web application. I have a client requirement wherein for the logged in user I need to retrieve list of all the list and document libraries across subsites and site collections across the web application to which the user has access permission. I do not want to loop through all the site collections, all the sites and all the lists within them to retrieve the data as this would be performance intensive.

I need a better approach to achieve the desired result.


  • Sharepoint supports unique permissions at different levels : Site, List and Listitem level. I don't think there is any way around where you can get user's permissions without looping through the site collections and site.
    – Aanchal
    Commented May 19, 2014 at 8:30

2 Answers 2


You can leverage Search functionality to get the list of all sites the user has access to since search will by default give you security trimmed results.This way you do not need to loop through each site collection,site and check whether user has permission or not. Below is the code to query all sites which user has access to:

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                    KeywordQuery keywordQuery = new KeywordQuery(site);
                    keywordQuery.QueryText = "contentclass:STS_SITE OR contentclass:STS_Web";//change the query to suit your requirement
                    keywordQuery.TrimDuplicates = false;
                    SearchExecutor searchExecutor = new SearchExecutor();
                    ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);

                    var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
                    int totalRows = resultTables.Count();
                    var resultTable = resultTables.FirstOrDefault();

                    dtResults = resultTable.Table;

This approach has a direct dependency on Search crawling, make sure crawling is done in proper intervals, but it provides good performance gain.

See this Link to get the KQL syntax for Lists,Libraries etc and use it in your query.

  • Image I want to do this for 1k users and more. Would an approach like this still be more feasible then using powershell to iterate over every webapp, sc, site and list?
    – Marco
    Commented May 19, 2014 at 9:04
  • @Serv This approach is best if you want to generate list for logged in user as this code needs to be run on the user's context. For large number of users like in thousands in this method you will have to get user token of each user and run in their context.the good thing about this approach is that it returns you a datatable of the lists/libraries the user has access , you don't have to individually check the list/library permission and populate a datatable.
    – Unnie
    Commented May 19, 2014 at 9:18

The search-keyword query approach mentioned by Unnie in my view is the best appraoach. The code will run for the current user and wll give you the appropriate record. The only thing is that you need to build the query properly. In case there are large number of records, you can also limit the number of records using rowlimit/ apply paging and show only a small number of records at a time.

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.