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)

3 Answers 3


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

qRequest.QueryText = strKeyword + " site:\"{your-site-collection-url}\"";
  • 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 Commented Mar 21, 2014 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>" + 


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);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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