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'm trying to get files from a Site... When i launch my query the crawler returns me data from all application on the SP installation..

The thing I'd like to do is to "restrict" this scope to a single site or site collection..

How could i do?

My code is something like this:

fullsite = URL;
myToken = UserToken;

                using (SPSite scSite = new SPSite(fullsite, myToken))
                    if ((searchAllObj.tt_group_array == null) || (searchAllObj.tt_group_array.Length == 0))
                        using (SPWeb webRoot = scSite.OpenWeb())
                            webRoot.AllowUnsafeUpdates = true;
                            scSite.AllowUnsafeUpdates = true;

                            queryComposta = "keyword + Tag:Tag1 Network:NT"; //Tag and Network are working props                                  

                            KeywordQuery keywordQuery = new KeywordQuery(scSite);
                            keywordQuery.ResultTypes = ResultType.RelevantResults;
                            keywordQuery.QueryText = queryComposta.ToString();

                            ResultTableCollection searchResultTables = keywordQuery.Execute();

EDIT: I'm on SP Foundation with search server 2010 express installed!

share|improve this question
up vote 3 down vote accepted

I suppose KeywordQuery.HiddenConstraints property can help. It appears that you can define scope restrictions with it. And there's a scope for limiting query to particular site: This Site: (

share|improve this answer
Whenever I add " keywordQuery.HiddenConstraints = "This Site: ReplyCorp"; " (where ReplyCorp IS the effective name of the site) it returns to me the list which contains item but not the item itself (and before it works).. Is something else to set? – Ziba Leah Feb 14 '12 at 16:50
Here's(…) the suggestion: For a contextual scope like "This Site" use: instance.HiddenConstraints = "site:\"[url to your site]\""; – Alex Boev Feb 14 '12 at 16:55
With site:\"ReplyCorp\" doesn't find any result.. – Ziba Leah Feb 14 '12 at 18:00

Simply append the following to your search query:

share|improve this answer
This way doesn't find any result.. I tried with relative and absolute url.. – Ziba Leah Feb 14 '12 at 18:04

This worked for me:

    string siteName = SPContext.Current.Site.RootWeb.Title;
        SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy
        KeywordQuery query = new KeywordQuery(proxy);
        query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
        query.QueryText = queryText;
        query.HiddenConstraints = "This Site: " + siteName + "";
        query.ResultTypes |= ResultType.RelevantResults;
        ResultTableCollection searchResults = query.Execute();
share|improve this answer
Unfortunately this only searches in the SPWeb, I want to search on the whole site collection (including the sub webs created within Root web) – Arsalan Adam Khatri Mar 23 '14 at 7:56

Check this article : See the section Using Scopes in KeywordQuery

share|improve this answer
The link above does not show how to add scopes to the KeywordQuery Class. – Nadeem Yousuf Sep 26 '12 at 9:30
See the 5th example : Finance Scope:”Local SharePoint Sites” Scope:”Internet Sites” NOT Scope:”Shared Folder”. – Amit Kumawat Sep 27 '12 at 15:09
Appologies, I had missed that. – Nadeem Yousuf Sep 28 '12 at 9:56

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.