Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Last active October 13, 2021 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beckettkev/6f3389963294aa1b566a59fde48dc245 to your computer and use it in GitHub Desktop.
Save beckettkev/6f3389963294aa1b566a59fde48dc245 to your computer and use it in GitHub Desktop.
JavaScript for Adding a Document Library Email Alert for the loggged in User
var ctx = SP.ClientContext.get_current();
this.user = ctx.get_web().get_currentUser();
this.alerts = this.user.get_alerts();
this.list = ctx.get_web().get_lists().getByTitle('Documents');
ctx.load(this.user);
ctx.load(this.alerts);
ctx.load(this.list);
ctx.executeQueryAsync(()=> {
let alertTime = new Date();
alertTime.setHours(alertTime.getHours() + 24);
let notify = new SP.AlertCreationInformation;
notify.set_title('Documents - Movers and Shakers');
notify.set_alertFrequency(SP.AlertFrequency.daily);
notify.set_alertType(SP.AlertType.list);
notify.set_list(this.list);
notify.set_deliveryChannels(SP.AlertDeliveryChannel.email);
notify.set_alwaysNotify(true);
notify.set_status(SP.AlertStatus.on);
notify.set_alertTime(alertTime);
//notify.set_user(this.user.get_loginName());
notify.set_user(this.user);
notify.set_eventType(SP.AlertEventType.all);
// 0 = Anything Changes
// 1 = Someone else changes a document
// 2 = Someone else changes a document created by me
// 3 = Someone else changes a document modified by me
notify.Filter = '0';
debugger;
this.alerts.add(notify);
this.user.update();
ctx.executeQueryAsync(()=> { console.log('CHEESE ON TOAST!'); debugger; }, ()=> { console.log('EGGS!'); })
}, ()=> {
console.log('WORSE THAN EGGS!');
});
@rsreeraj
Copy link

rsreeraj commented May 7, 2021

Similarly How do we delete the user alerts created for the library for a specific user. Can uou suggest JSOM code for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment