SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 programatically search a sharepoint site. The problem is, the results returned are from all site collections. How can I limit my search to only one site collection.

Here is how my code looks like

using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;

using (var siteColl = new SPSite(siteCollUrl))
            using (SPWeb site = siteColl.RootWeb)
                KeywordQuery qRequest = new KeywordQuery(siteColl);

                qRequest.QueryText = strKeyword;
                qRequest.ResultTypes = ResultType.RelevantResults;

                ResultTableCollection queryResults = qRequest.Execute();
                ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];
                DataTable queryDataTable = new DataTable();
                queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);


                var results = from queryResult in queryDataTable.AsEnumerable()
                              select new
                                  Title = queryResult.Field<string>("Title"),
                foreach (var v in results)
share|improve this question
up vote 2 down vote accepted

You can append the site collection url to your query text:

qRequest.QueryText = strKeyword + " site:\"{your-site-collection-url}\"";
share|improve this answer
Why it doesn't work for Root site collection? Even if I give web application Url in site:"http://root-site-collection, it still shows results from all site collections – Arsalan Adam Khatri Mar 21 '14 at 14:58

If you need to search a single site collection only, use SPSiteDataQuery and use CAML to build your search Query. You are now searching using the Farm wide Enterprise Search KeyWordQuery class, which will indeed include results from all site collections included in the Seach app's content sources.

SPSiteDataQuery qry = new SPSiteDataQuery();
qry.Query = 
  "<Where>" +
  "  <Eq>" + 
  "    <FieldRef Name='Title' />" +
  "    <Value Type='Text'>{WHATYOUARESEARCHFOR</Value>" +
  "  </Eq>" + 

share|improve this answer

You can use SPSiteDataQuery and you can use SPSiteDataQuery.Webs property to specify where the data will be retrieved from.

"<Webs Scope='SiteCollection' />" : will search on all webs in the site collection

"<Webs Scope='Recursive' />" : will search the SPWeb on which you run GetSiteData and all subwebs

You can restrict the search using Lists and Query properties:

public void MySiteDataQuery()
  using (SPSite site = new SPSite("http://YourSitecollectionURL"))
    using (SPWeb web = site.OpenWeb("/"))
      SPSiteDataQuery query = new SPSiteDataQuery();

       //query contacts list.
       query.Lists = "<Lists ServerTemplate='105' />";    
       query.ViewFields = "<FieldRef Name='Title' />";

       query.Query = "<Where><Contains>" +
       "<FieldRef Name='Title' /><Value Type='text'>John</Value>"+
       query.Webs = "<Webs Scope='SiteCollection' />";

       DataTable dt = web.GetSiteData(query);

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.