Issue:Add alert using JSOM sharepoint online failing for edit contribute users

Copper Contributor
Hi ,

I have created spfx which uses Jsom code to create a alert on a list for a current logged in user. The alert creation is working fine for full control user ,but jsom code is failing every time a user with read contribute or edit permission tries to create an alert.
It shows access denied. You do not have permission to access this resource.

Any pointers will be really helpful.

Regards,
B
3 Replies
Hi, we can provide you a better solution if you could share the code.

@Sudharsan K 

 

Please find the code below:-

var spContext = new SP.ClientContext(context.pageContext.web.absoluteUrl);
var user = spContext.get_web().get_currentUser();
var alerts = user.get_alerts();
var list = spContext.get_web().get_lists().getByTitle("Announcements");
spContext.load(user);
spContext.load(alerts);
spContext.load(list);
return await new Promise((resolve, reject) => {
spContext.executeQueryAsync(async () => {
//Create new alert Object and Set its Properties
let notify = new SP.AlertCreationInformation;
notify.set_title(sharepointDetails.announcementDetails.alertTitle);
notify.set_alertFrequency(SP.AlertFrequency.immediate);
notify.set_alertType(SP.AlertType.list);
notify.set_list(list);
notify.set_deliveryChannels(SP.AlertDeliveryChannel.email);
notify.set_alwaysNotify(true);
notify.set_status(SP.AlertStatus.on);
notify.set_user(user);
notify.set_eventType(SP.AlertEventType.all);

notify.Filter = '0';

debugger;
alerts.add(notify);
user.update();
var result: number = await new Promise((resolve, reject) => {
spContext.executeQueryAsync(function () { // onSuccess
console.log("Alert added success fully")
resolve(1);
},
function (sender, args) { // onError
reject(args.get_message());

},
);
});
resolve(result);
}, (error) => {
console.error(`Error while fetching alerts for user. ${error}`);
reject(new Error(error));
});
});

 

 

Note : This works fine for full control user but giving "access denied .Unauthorized to access this resource" for read and contribute users. I have already check read and contribute user have create alerts rights.

Hi,
There are 2 coding line that I have doubt regarding the 'Read and Contribute' permission.
spContext.load(list);
user.update();