Friday, September 19, 2014

How to remove certain items from a list/library in SharePoint, and keep all other items

The scenario is as follows. You have indexed a SharePoint farm and one of your business owners tells you that in his library he keeps multiple copies of a file, where the latest is marked as ApprovalStatus=1 with a custom column. And only those files should be surfaced in the search results.

So, how do you craft search a query? At first you might write something like:

ApprovalStatus=1

This will exclude all items which don’t have that value as well, so not a good idea.

Next you know the list has a custom list definition so you try something like:

ApprovalStatus=1 AND ContentClass=STS_ListItem_10300

Now you’re excluding all other content except that list. Even worse, but still explicit to what you want. The solution is to add an OR clause to include all the other stuff you want, which is everything.

((ApprovalStatus=1 AND ContentClass=STS_ListItem_10300) OR (ContentClass<>STS_ListItem_10300))

And we have arrived at the final query. It will include anything not from the 10300 list definition + items from the 10300 definition which is approved.

Conclusion

When you want to exclude certain content, it’s much easier to generate a rule saying what you want to include. In this case one part specifying the exact rule for what to show for a particular library, and one part saying everything else except that library.

Other possible solutions

  • Content enrichment web service which excludes content from that library and which is not approved
  • Don’t keep multiple files, but use SP versioning
  • Move non-approved documents to a library which is not being indexed