Sharepoint - Create list alert with filter on managed metadata field

The problem you are running into is that Alert.filter accepts a very limited subset of CAML and what that subset allows is anybody's guess (i.e. completely undocumented, of course you already know that by now).

A while back I read an answer on a forum to a similar question that provide a novel approach to figure out what worked. I would reference it here, but I can't seem to find it now. Anyway, the approach was:

  1. Create a view that filters the way you want the alert to filter.
  2. Setup an alert on that view.
  3. Programmatically, look at that alert and look at the filter.

The reason this works is because the alert isn't actually tied to the view, it copies the filter. So if the view changes, the alert will be out of sync with it.

So I setup a quick test with a multi-select metadata site column, creatively named ManagedMetadata, containing some fruits and vegetables, created a list that used the field, and a view that only showed items with Broccoli as a value. Finally, I created an alert on that view. Then I created some items with and without Broccoli and checked that the alert works, which it does.

Now, I launch the debug console and run this code:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var alerts = web.get_alerts();

ctx.load(web);
ctx.load(alerts);

ctx.executeQueryAsync(
    function() {
        var en = alerts.getEnumerator();
        while (en.moveNext()) {
            console.log(en.get_current());
        }
    },
    function(sender, args) {
        console.log(args.get_message());
    }
);

When I run that code, this what gets dumped for that alert:

enter image description here

The actual filter is:

<Query><Eq><FieldRef Name=\"ManagedMetadata\"/><Value type=\"string\">brocoli</Value></Eq></Query>

The only difference I see is that the value type is set to string instead of text like you have. Why? I cannot say, but it seems to work.

Note that I'm doing this in SharePoint online. If you mentioned what version of SharePoint you're on, I missed it. Anyway, hope this helps.

Tags:

Caml

Alert