3

I am getting the following message when running below script :

"Access denied. You do not have permission to perform this action or access this resource."

var clientContext;
var user;

// Make sure the SharePoint script file 'sp.js' is loaded before your
// code runs.
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    user = clientContext.get_web().get_currentUser();

    clientContext.load(user);
    clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
    alert('The email address of the current user is ' + user.get_email());
}
function onQueryFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

Using sharePoint 2013, not sure what setting I need to change or which line exactly is giving the error.

Having trouble figuring out which line exactly is the problem in console.?

2

For anyone looking, I figured it out. In user permissions make sure remote API is enabled

  • Hi @M_kul , good job , and sorry for late answer for your comment , but as i told you in above comments : make sure that the user have at least a read permission this will include this role "Use Remote Interfaces - Use SOAP, Web DAV, the Client Object Model or SharePoint Designer interfaces to access the Web site" , if you do additional setting please add it in details to be useful for all, i also will update my answer with my comment above to be clear for all , thanks – Mohamed El-Qassas MVP Nov 8 '16 at 11:10
2

You should be aware of the JavaScript CSOM runs under the context of the current user , and when you get access denied that mean the current context didn't have a sufficient permission ,

So make sure that the current user has a sufficient permission to access your site.

Note : you sould assign to the user at least read as a permission level that by default have the following role

Use Remote Interfaces  -  Use SOAP, Web DAV, the Client Object Model or SharePoint Designer interfaces to access the Web site. 

I also tried the following code with a user have access to the site at console that working properly ,

var clientContext;
var user;

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

function sharePointReady() {
    clientContext = SP.ClientContext.get_current();
    user = clientContext.get_web().get_currentUser();

    clientContext.load(user);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
    alert('The email address of the current user is ' + user.get_email());
}
function onQueryFailed(sender, args) {
    alert('Error: ' + args.get_message());
}

Regarding Having trouble figuring out which line exactly is the problem in console

  • Try to add your code to script editor on your page.
  • Open your page via google chrome
  • press F12 to open developer tools
  • Navigate to your source tab
  • Set breakpoints to your code as mentioned at How to set breakpoints
  • User does have access to the site. The code runs correctly for an admin but not for normal users. I can't seem to find online what permission exactly I have to change to give users access to this. – M_kul Nov 7 '16 at 20:58
  • are the normal users have at least read access ? – Mohamed El-Qassas MVP Nov 7 '16 at 21:07
  • Assuming I am granting access to the correct thing yes. After some research I found to have settings for the group so that everyone can see. Which is set. I am in SharePoint designer but I don't see a specific "user" list if there is one – M_kul Nov 7 '16 at 21:34
  • Apologies I have been able to find the list and indeed I can get to it using _catalogs/users/detail.aspx. But i see no settings for this list. Users do not have access to system pages I do not know if that is what is causing the error. – M_kul Nov 7 '16 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.