Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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();
share|improve this answer
    
Thank you, Unnie! :) –  Celbester Jun 6 at 9:39

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))
                            {
                                Console.WriteLine(login);
                            }

                    }
                }
            }

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

http://stackoverflow.com/questions/3940917/checking-permissions-of-a-user-with-a-site-collection

share|improve this answer
    
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 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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