3

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;

Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                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!

5 Answers 5

3

I suppose KeywordQuery.HiddenConstraints property can help. http://www.mstechblogs.com/paul/sharepoint-2010-keywordquery-and-the-hiddenconstraints-property. It appears that you can define scope restrictions with it. And there's a scope for limiting query to particular site: This Site: (http://technet.microsoft.com/en-us/library/gg185660.aspx).

3
  • 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, 2012 at 16:50
  • 2
    Here's(msdn.microsoft.com/en-us/library/…) the suggestion: For a contextual scope like "This Site" use: instance.HiddenConstraints = "site:\"[url to your site]\"";
    – Alex Boev
    Feb 14, 2012 at 16:55
  • With site:\"ReplyCorp\" doesn't find any result..
    – Ziba Leah
    Feb 14, 2012 at 18:00
2

Simply append the following to your search query:

site:"your-site-url"
1
  • This way doesn't find any result.. I tried with relative and absolute url..
    – Ziba Leah
    Feb 14, 2012 at 18:04
1

This worked for me:

    string siteName = SPContext.Current.Site.RootWeb.Title;
        SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy
   (SPServiceContext.GetContext(SPContext.Current.Site));
        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();
1
  • Unfortunately this only searches in the SPWeb, I want to search on the whole site collection (including the sub webs created within Root web) Mar 23, 2014 at 7:56
0

You can use the Managed Property SiteID (or WebId for Webs) for searching only in one site collection:

contentclass:STS_ListItem_DocumentLibrary -FileExtension:aspx IsDocument:true SiteId:75bf0515-5108-4d65-afb1-dbdfa358f5af

-1

Check this article : http://extreme-sharepoint.com/2011/10/28/sharepoint-keywordquery/. See the section Using Scopes in KeywordQuery

3
  • The link above does not show how to add scopes to the KeywordQuery Class. Sep 26, 2012 at 9:30
  • See the 5th example : Finance Scope:”Local SharePoint Sites” Scope:”Internet Sites” NOT Scope:”Shared Folder”. Sep 27, 2012 at 15:09
  • Appologies, I had missed that. Sep 28, 2012 at 9:56

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.