SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've did some research on Internet and found some code examples how to retrieve search scopes, but when I try to execute it fails, what do I do wrong?

Here is the code:

  SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
  SearchServiceApplicationProxy searchServiceAppProxy = serviceContext.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) as SearchServiceApplicationProxy;
  SearchServiceApplicationInfo searchSerivceAppInfo = searchServiceAppProxy.GetSearchServiceApplicationInfo();
  SearchServiceApplication searchServiceApplication = SearchService.Service.SearchApplications.GetValue<SearchServiceApplication>(searchSerivceAppInfo.SearchServiceApplicationId);
  Scopes scopes = new Scopes(searchServiceApplication);
  foreach (Scope scope in scopes.GetSharedScopes())

Ok in this case I try to get SharedScopes but in general I just need to get all scopes, if i do scopes.AllScopes it doesnt work.

I checked the Log files and it seems I get an error there, some error that says, fail to connect to database and so on and so on. And it happens exactly on the like where i do scopes.AllScopes (or scopes.GetSharedScopes()) and so on). Do I miss something? I even tried to run this code with elevated privileges, but that doesn't help. Still the same error.

So what do I miss?

Update: I don't know, I suppose that that account has enought rights as it is systemaccount, here is the error:

Cannot open database "Search_Service_Application_DB_71f6d22eda5e4eecbc4e225360b2d5a6" requested by the login. The login failed. Login failed for user 'sp2010appweb'. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)....

share|improve this question
What kind of account are you using? Does the account have enough access to work with the service application? Can you post the entire error? – Mike Oryszak Aug 15 '12 at 22:36
Updated teh question with error. – Shkipper Aug 16 '12 at 5:39
up vote 3 down vote accepted

I found the solution instead of using the SearchServiceApplication which causes the issues with permissions, I use this piece of code:

RemoteScopes remoteScopes = new RemoteScopes(SPServiceContext.GetContext(site));

And it works like a charm.

share|improve this answer

Your Answer


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.