Is there a way to determine if a single SharePoint list item matches a SPQuery/SPView?

602 views Asked by At

The standard 'Alerting' feature in SharePoint allows you to only send out an alert if the item appears in a specified view.

I'm wanting to apply similar logic in a custom workflow (c#).

Given a SPListItem and SPView/SPQuery does anybody know if there is an effective* way to see if the item falls into that view / matches the query?

*effective meaning, not just running the query then looping through to find the item

2

There are 2 answers

0
Tom On BEST ANSWER

I've had an idea however, if I adapt the idea discussed in the answer to this question How to query the SPView object.

If I modify the query to execute all the filters from the view, but also add in a clause where ID equals the ID of my item, I will either get 1 result back or 0, then I can just check the count.

Performance wise I'm not sure which is more efficient between Mathieu's suggestion and this idea?

I will investigate tomorrow...

UPDATE My technique was effective. I would like to think (from a design point of view) that the ID check occurring on the DB side rather than via an XML query, would be more efficient.

Thanks all the same for your suggestion Mathieu.

0
Mathieu On

I have not come across such a method on SPListItem, SPQuery, ... that has such functionality. Checking if the current item is in the returned collection doesn't seem such a bad idea. But instead of looping through all the items I would use a LINQ query to help you out. Something like this:

itemCollection.Cast<SPListItem>().Any(i => i.ID.Equals(yourItem.ID));

Where "itemCollection" is the collection of items you got from the query or the view. and "yourItem" is the item you want to check if it's in the colleciton.