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

%3CLINGO-SUB%20id%3D%22lingo-sub-1513294%22%20slang%3D%22en-US%22%3EAdd%20alert%20using%20Jason%20sharepoint%20online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1513294%22%20slang%3D%22en-US%22%3EHi%20%2C%3CBR%20%2F%3E%3CBR%20%2F%3EI%20have%20created%20spfx%20which%20uses%20Jsom%20code%20to%20create%20a%20alert%20on%20a%20list%20for%20a%20current%20logged%20in%20user.%20The%20alert%20creation%20is%20working%20fine%20for%20full%20control%20user%20%2Cbut%20jsom%20code%20is%20failing%20every%20time%20a%20user%20with%20read%20contribute%20or%20edit%20permission%20tries%20to%20create%20an%20alert.%3CBR%20%2F%3EIt%20shows%20access%20denied.%20You%20do%20not%20have%20permission%20to%20access%20this%20resource.%3CBR%20%2F%3E%3CBR%20%2F%3EAny%20pointers%20will%20be%20really%20helpful.%3CBR%20%2F%3E%3CBR%20%2F%3ERegards%2C%3CBR%20%2F%3EB%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1513294%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1517822%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%3AAdd%20alert%20using%20JSOM%20sharepoint%20online%20failing%20for%20edit%20contribute%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1517822%22%20slang%3D%22en-US%22%3EHi%2C%20we%20can%20provide%20you%20a%20better%20solution%20if%20you%20could%20share%20the%20code.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1524271%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%3AAdd%20alert%20using%20JSOM%20sharepoint%20online%20failing%20for%20edit%20contribute%20users%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1524271%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F144422%22%20target%3D%22_blank%22%3E%40Sudharsan%20K%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20find%20the%20code%20below%3A-%3C%2FP%3E%3CP%3Evar%20spContext%20%3D%20new%20SP.ClientContext(context.pageContext.web.absoluteUrl)%3B%3CBR%20%2F%3Evar%20user%20%3D%20spContext.get_web().get_currentUser()%3B%3CBR%20%2F%3Evar%20alerts%20%3D%20user.get_alerts()%3B%3CBR%20%2F%3Evar%20list%20%3D%20spContext.get_web().get_lists().getByTitle(%22Announcements%22)%3B%3CBR%20%2F%3EspContext.load(user)%3B%3CBR%20%2F%3EspContext.load(alerts)%3B%3CBR%20%2F%3EspContext.load(list)%3B%3CBR%20%2F%3Ereturn%20await%20new%20Promise((resolve%2C%20reject)%20%3D%26gt%3B%20%7B%3CBR%20%2F%3EspContext.executeQueryAsync(async%20()%20%3D%26gt%3B%20%7B%3CBR%20%2F%3E%2F%2FCreate%20new%20alert%20Object%20and%20Set%20its%20Properties%3CBR%20%2F%3Elet%20notify%20%3D%20new%20SP.AlertCreationInformation%3B%3CBR%20%2F%3Enotify.set_title(sharepointDetails.announcementDetails.alertTitle)%3B%3CBR%20%2F%3Enotify.set_alertFrequency(SP.AlertFrequency.immediate)%3B%3CBR%20%2F%3Enotify.set_alertType(SP.AlertType.list)%3B%3CBR%20%2F%3Enotify.set_list(list)%3B%3CBR%20%2F%3Enotify.set_deliveryChannels(SP.AlertDeliveryChannel.email)%3B%3CBR%20%2F%3Enotify.set_alwaysNotify(true)%3B%3CBR%20%2F%3Enotify.set_status(SP.AlertStatus.on)%3B%3CBR%20%2F%3Enotify.set_user(user)%3B%3CBR%20%2F%3Enotify.set_eventType(SP.AlertEventType.all)%3B%3CBR%20%2F%3E%3CBR%20%2F%3Enotify.Filter%20%3D%20'0'%3B%3CBR%20%2F%3E%3CBR%20%2F%3Edebugger%3B%3CBR%20%2F%3Ealerts.add(notify)%3B%3CBR%20%2F%3Euser.update()%3B%3CBR%20%2F%3Evar%20result%3A%20number%20%3D%20await%20new%20Promise((resolve%2C%20reject)%20%3D%26gt%3B%20%7B%3CBR%20%2F%3EspContext.executeQueryAsync(function%20()%20%7B%20%2F%2F%20onSuccess%3CBR%20%2F%3Econsole.log(%22Alert%20added%20success%20fully%22)%3CBR%20%2F%3Eresolve(1)%3B%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3Efunction%20(sender%2C%20args)%20%7B%20%2F%2F%20onError%3CBR%20%2F%3Ereject(args.get_message())%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%2C%3CBR%20%2F%3E)%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3Eresolve(result)%3B%3CBR%20%2F%3E%7D%2C%20(error)%20%3D%26gt%3B%20%7B%3CBR%20%2F%3Econsole.error(%60Error%20while%20fetching%20alerts%20for%20user.%20%24%7Berror%7D%60)%3B%3CBR%20%2F%3Ereject(new%20Error(error))%3B%3CBR%20%2F%3E%7D)%3B%3CBR%20%2F%3E%7D)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%20%3A%20This%20works%20fine%20for%20full%20control%20user%20but%20giving%20%22access%20denied%20.Unauthorized%20to%20access%20this%20resource%22%20for%20read%20and%20contribute%20users.%20I%20have%20already%20check%20read%20and%20contribute%20user%20have%20create%20alerts%20rights.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New 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
Highlighted
Hi, we can provide you a better solution if you could share the code.
Highlighted

@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.

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