0

I have created a custom HTML page and moved it to a Sandbox Site on my SharePoint site.

I have loaded the following script in my <head></head> tag, ttps://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices.min.js.

I am trying to get the current user Title and ID with the following jQuery code:

  var thisUserTitle = $().SPServices.SPGetCurrentUser({ //get the current user Title field 
      fieldName: "Title",
      debug: false
  });
  var thisUserID = $().SPServices.SPGetCurrentUser({ //get the current user ID field
      fieldName: "ID",
      debug: false
  });

When I console.log() both variables thisUserTitle and thisUserID, I am able to successfully get and log the thisUserTitle but thisUserID does not work and no errors appear in the console or network tab.

When I put the two in a script editor on a normal page, I am able to get both succesfully. Why is this?

1
  • It's not an answer, but neither custom HTML pages nor SPServices is a good practice in SharePoint Online in 2021. It's best to stay away from these kinds of customizations. As an alternative, you can create Power Apps or SPFx webparts. Nov 18, 2021 at 6:19

1 Answer 1

1

You don't need SPServices for most basic operations like these. For example, this is how you can get user ID:

fetch('/_api/web/currentuser/id', {  
    headers: {
        'Accept': 'application/json;odata=verbose'
    }})
.then(response => response.json())
.then(data => console.log(data.d.Id));

Or

(async () => {
 const response = await fetch('/_api/web/currentuser/id', {  
    headers: {
        'Accept': 'application/json;odata=verbose'
    }})       
    const user = await response.json();
    console.log(user.d.Id)
   
})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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