Saturday, August 31, 2013

Exclude AllItems.aspx from search results in Sharepoint 2013

Search in Sharepoint 2013 plays one of the key roles. Many new features introduced in the new SP version rely on search service application and search index (e.g. new content by search web part, cross-site publishing and catalog connections) and it seems like that there will be more posts about search in my blog in future. In this post I will show how to solve one practical issue: exclude standard all items list view (allitems.aspx from search result). I should warn you that if you want to restrict users from accessing the list view, then solution described in this post is not what you want, because hiding of the page from search results won’t help when user will access the list by direct url. In this case you should use Sharepoint security model for proper authorization.

By default allitems.aspx list view is included into the search results, and if you won’t exclude it visitors of your site may see something like this in the search results page:

image

And when they will click on it, they will be redirected to the list view with items. It is not security hole, because as you probably know search results in Sharepoint are trimmed, so user will see only those content which he has access to. So if user sees allitems.aspx in the search results, then it means that user has at least read access to the list. However standard all items view may contain information, which you should not like to show to anonymous users, e.g. it may show values of managed metadata fields with full path rendered, which will show how taxonomy is organized. There also can be other fields presented in not user friendly form. So how to hide this view from search results using standard configuration mechanisms (I won’t consider options with using javascript for hiding the content from search result)?

The first way is to use Crawl Rules. Go to Central administration > Manage service applications > Search service application > Crawl rules:

image

In the opened window enter the following parameters:

Path = ://*/AllItems.aspx
Crawl configuration = Exclude all items in this path (check “Exclude complex URLs (URLs that contain question marks – ?)”)
Specify authentication = Use the default content access account (or use other suitable option for your project)

image

(it is also possible to create crawl rules using powershell)

After that perform crawl (full or incremental in CA > Manage service applications > Search service application > Content sources > Choose the correct content source and click crawl). After that all items view will disappear from search results. However this approach has one important drawback: individual list items from this list may also disappear from search result. I didn’t test it deeply and can’t say for sure, but it can be so that if there are no other links to the list item, except links from alltems.aspx, then this list item won’t be crawled (which is logical if crawler works like regular search engine robot for indexing list items. However as I said I didn’t test it by myself and can’t say for sure).

If you faced with this problem, then you can use another solution. Go to Site settings > Search results sources and copy your default result source (e.g. Local SharePoint Results) to the new one. Then make the copied result source default one (open context menu and select Set as default), so it will be used by the search results web part on the search results page. Then modify copied result source and edit Query transform rule. If you copied Local SharePoint Results then initially it will look like this:

{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}

which means use original search terms entered by user in search box and exclude people results. You need to add one more exclude rule for allitems.aspx “-filename:allitems.aspx”, so the query transform will look like this:

{?{searchTerms} -ContentClass=urn:content-class:SPSPeople -filename:allitems.aspx}

Advantage of this solution is that you don’t need to recrawl your sites again. Search in Sharepoint is heavily optimized for performance, so it still may show allitems.aspx in the search results some time. In this case click Ctrl-F5 several times and make iisreset, it will refresh cache both on client and server sides. After this allitems.aspx should disappear from the results.

Hope that this information will help you in your work.

6 comments:

  1. I have noticed as well that when using crawl rules to exclude allitems.aspx, all the content in that list just disappears from the search results. Tried to find for a specific solution to do this, since these items being removed from the index would optimize your search performance already. I guess the Query transformation rules on the content sources is the best option for now...

    ReplyDelete
  2. Jonathan,
    did you mean query transformation for the result sources (there are too many sources :) )? If yes, then we decided to use this option for now as well.

    ReplyDelete
  3. Alexey, I was indeed referring to the result sources.

    ReplyDelete
  4. I tried the "result sources" approach but it still show "AllItems.aspx" in search results. I have cleared browser cache numerous times as well as reset IIS. I even started full crawl several times but no luck. I have defined this result source in not only subsite but also parent site.

    ReplyDelete
  5. Admin hi,
    the fact that IIS reset and clearing browser cache didn't help tells that these pages are still in your search index. For most problems related with search instead of IIS reset you should try the following:
    - reset search index in CA > Manage service applications > Search service application
    - restart 2 windows services from Administrative tools: Sharepoint Server Search 15 and Sharepoint Search Host Controller
    - make full crawl
    Note that after reset of search index all search request will give empty results until next full crawl will be done.

    ReplyDelete
  6. I used the Query Transformation method, that worked for me. Thanks

    ReplyDelete