Monday, September 10, 2018

An approach to search for a URL within a page or document in SharePoint

image

The Search Explained Yammer network is a great place to ask the weird and quirky search questions. Unfortunately it’s not indexed by Google, so answers there will not benefit everyone – which is why I’m writing this post.

A couple of days ago someone posted a question if it was possible to find pages in SharePoint which contain a specific link. The need was to identify broken links. Use-case can be if you rename a file, and want to find all pages linking to that file before renaming – to make sure they still point correctly.

This post will show you one approach which works, and if you have a better suggestion, please let me know.

Question is; how can you go about finding a page with the link https://contoso.sharepoint.com/sites/collection/site/library/document.docx as part of the content?

Both KQL and FQL has a bunch of operators you can use, and turns out if you employ the use of the ONEAR operator with a proximity of 0, it works. Let me explain.

The ONEAR operator is used to say that a specific term has to follow after another term in the right order, and within n number of terms.

cat ONEAR(n=2) dog, would match cat hey yo dog, as you allow two terms to go in between. You can read the official KQL and FQL syntaxes for ONEAR at:

The KQL syntax in this case is very quirky as you need to nest the ONEAR operators for each term you want to follow after each other – and you and up with this:

(((((((((https ONEAR(n=0) contoso) ONEAR(n=0) sharepoint) ONEAR(n=0) com) ONEAR(n=0) sites) ONEAR(n=0) collection) ONEAR(n=0) site) ONEAR(n=0) library) ONEAR(n=0) document) ONEAR(n=0) docx)

If you search programmatically you can take advantage of the fact that the refinement filter query parameter is actually an FQL statement. By passing in the below FQL as a refinement filter you will achieve the same as the above KQL:

onear(https, contoso, sharepoint, com, sites, collection, site, library, document, docx, N=0)

Refinement filters are also not limited to the query length of the search field with KQL.

I would advise against having too many URL’s in one query as performance will probably degrade a lot – but this solution might just help someone who really really need it.