4

I'm developing a custom webpart that displays all site collections the current user has access to. I'm using search with a KeywordQuery (contentclass:STS_Site) to bring up the results.

Now I want to exclude some site collections from the results for this webpart, e.g. all MySites. But these sites should not be excluded from the search index in general.

I've been testing a QueryString like this successfully:

contentclass:STS_Site \
  NOT Site:http://UrlOfMySiteWebApp \
  NOT Site:http://sharepoint/sites/SpecialPage \
  NOT Site:http://sharepoint/sites/AnotherSpecialPage

But that may end up in a huge QueryString difficult to maintain. Altough I'm probably not going to hit MaxKeywordQueryTextLength.

Is there another (somewhat "more elegant") way to exclude URLs or entire web applications from a certain query / search result?

3

I would create a Result Source and have my custom web part use that. Though I don't think you need the custom web part as you can probably use the OOB Search web part attached to your new Result Source.

You could omit this part all together and just have users use the out of the box "Follow" capability so you don't need to do the search at all.

If you are looking for a RegEx approach, you can't do that, you need to manage the query either through KQL or FQL.

  • So I'll do some KQL fiddling :) ... Regarding the "Follow" technique (I actually want to use it in my webpart, too): When a user is authorized for a site for the first time and I don't send the "Welcome to ..."-e-mail, he/she will not know, that the new site exists just by the "Follow" capabilities of SharePoint. Right? I'm going to combine the search results with the "followed sites" marking them as some sort of the user's "favorite sites" within all the sites he/she has access to. – jcp Dec 2 '13 at 8:53
  • Follow is opt-in for everyone except the person who creates the site. The creator is automatically set to follow the site. Follow is part of Social and you would use the social API to determine what sites a user follows. – Matthew McDermott Dec 3 '13 at 12:42
3

Here's what you need to do:

1.Go to your search site. Navigate to Site Settings | Query Rules

2.Within the Query Rules page, select 'Local SharePoint Results' for the context and then click 'New Query Rule'

3.Within the New Query Rule dialogue, provide a title for the rule, e.g. 'Remove Site X from results'

4.Ignore the query conditions - you want it to fire for all queries

5.In the Actions section, click the link 'Change ranked results by changing the query'

6.Within the 'Build your query' dialogue, in the Property Filter dropdown box, select 'Path'. For the parameter, change it from the default 'contains' to 'Not starts with'. For the value, select 'Manual value' and then type the URL for the site you want to exclude. Then click the button 'Add property filter'

7.You should see the Query text display something like {searchTerms} -Path=https://site/sub-site* displaying your site hierarchy up to the point that you want excluded. This is telling to the search engine to filter out all results that begin with that path.

Note: this will also exclude any further sites beneath that URL It's a bit long-winded compared to the old search scopes where a simple Exclude rule was all you needed, but the granularity is much much richer for tweaking what you want.

Have a look at the below link too,

SharePoint 2013 - Search Administration

Good luck! :-)

Source

  • I should have been more specific: I'm developing a custom webpart and not using an OTB search webpart. Referring to your answer: The query text -Path=https://... is the same like NOT Site:https://.. just in FQL instead of KQL, isn't it? So excluding more sites would lead to multiple -Path-entries in the query, wouldn't it? – jcp Nov 29 '13 at 10:53
  • yes, you are correct. – Karthik Jaganathan Nov 29 '13 at 10:59
2

I know this is a really old ticket, but I was just looking in to how to do this myself using the OOTB - Search Results web part.

Using this blog from The Grumpy Guru - getting a list of sites that a user has access to in sharepoint I then came across the same issue you did in that personal sites were being displayed.

I fixed this with by playing around with the in query generator for this web part, which resulted in the below query;

contentclass:"STS_Web" contentclass:"STS_Site" -SPSiteURL:personal

Would love to know if this is what you also came up with - or if you had a better solution.

  • Now that's a nice and clean OOTB solution! I ended up in adding an indexed property called ShowSite to the root web of the sites that should be included within the results. My query reads something like this: contentclass:STS_Site AND ShowSite:1. I think I'll refactor my code using your solution to exclude the personal sites yourway, although I have to use the indexed property also, to exclude some other special sites. – jcp Sep 7 '16 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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