SPAlert to trigger when a Page of a certain Content Type is Published

1.7k Views Asked by At

Can anyone please let me know how to trigger an alert only when a page of a certain content type is published in MOSS?

The requirement is to set up alerts in a publishing site's pages library to send off notifications when someone created a page using a certain content type and publish the page. I’ve managed to trigger the alert for pages created using the content type. And seperately when the page is published using CAML in the SPAlert.Filter property.

But when I combine the queries using CAML <And></And> alerts stop triggering any notifications.

Does anyone know why this is happening? And the correct way to use CAML <And> in SPAlert.Filter?

My Code:

SPUser user = SPContext.Current.Web.CurrentUser;
SPWeb web = SPContext.Current.Web;

SPAlert alert = user.Alerts.Add();

alert.Filter = "<Query><And><Eq><FieldRef Name='ContentType' /><Value Type='Text'>CUSTOM_PAGE_CONTENT_TYPE</Value></Eq><Eq><FieldRef Name='_ModerationStatus' /><Value Type='Integer'>0</Value></Eq></And></Query>";

alert.Title = "Alert Title";
alert.AlertType = SPAlertType.List;
alert.EventType = SPEventType.All;
alert.List = web.Lists["Pages"];
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.AlwaysNotify = true;
alert.Update(true);

Thanks in Advance, Rizi.

2

There are 2 best solutions below

0
On

Try this:

<Query>
    <Where>
        <And>
            <Eq>
                <FieldRef Name='ContentType' />
                <Value Type='Text'>CUSTOM_PAGE_CONTENT_TYPE</Value>
            </Eq>
            <Eq>
                <FieldRef Name='_ModerationStatus' />
                <Value Type='ModStat'>Approved</Value>
            </Eq>
        </And>
    </Where>
</Query>

You need a Where element between Query and And. Also, use a type of ModStat instead of Integer for ModerationStatus.

0
On

Actually, SPAlert.Filter property shouldn't have the <Where> XML element. Remove that from the filter XML fragment and you should have it working.

Check the second Note box on this page: http://msdn.microsoft.com/en-us/library/bb802961(v=office.12).aspx