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 setup with two farms. Farm A is the front-end farm where web applications are hosted and farm B is the service farm where the search service + others are hosted. I have set up a custom content source and a custom scope (named "Teamsites") pointing to that content source. If I use the standard search center page I can query the scope without any issues, but when I use custom code (i.e. from within a web-part) to do a KeywordQuery with:

QueryText = "Scope=\"Teamsites\""

I get results even from outside the scope. As soon as I visit the viewscopes.aspx page on the site collection I'm querying from, however my custom query works fine. Is there some initialization I'm missing?

I've tried newing the KeywordQuery object both with SPContext.Current.Site and using the method described at the top of this article: http://extreme-sharepoint.com/2011/10/28/sharepoint-keywordquery/ Neither works unless I first visit the viewscopes.aspx page.

Edit: After nightly recycle the problem is back.

Here is how I'm initializing the KeywordQuery class:

var proxy = SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site);
var keywordQuery = new KeywordQuery((SearchServiceApplicationProxy) proxy);
keywordQuery.ResultTypes = ResultType.RelevantResults;
keywordQuery.QueryText = "Scope=\"Teamsites\" AND contentclass=\"STS_Site\"";
keywordQuery.RowLimit = 50;
keywordQuery.Execute();

I've been wrestling with this for over a week now. Any help is greatly appreciated!

Edit2: I tried using FullTextSqlQuery with the same result.

Edit3: For completeness these are the methods I used to create scope and scope rule:

http://www.c-sharpcorner.com/uploadfile/anavijai/create-a-content-source-scope-rule-in-sharepoint-2010-using-powershell/

http://www.c-sharpcorner.com/uploadfile/anavijai/create-scopes-in-sharepoint-2010-using-powershell/

Edit4: I've now tried using the soap service search.asmx with the same result. It will not limit the search to the scope I specify.

share|improve this question
add comment

1 Answer

You need to specify the scope using HiddenConstraints property on the query, something like this

keywordQuery.HiddenConstraints = "scope:" + "\"" + SearchScope + "\"";
share|improve this answer
add comment

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.