1

I'm trying these two ways to get the user id.

   $(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(loadUser, "sp.js"); });

    function loadUser() {

        var ctx = new SP.ClientContext.get_current();
        this.web = ctx.get_web();
        this.currentUser = this.web.get_currentUser();
        ctx.load(this.web);
        ctx.executeQueryAsync(Function.createDelegate(this, this.asdasd), Function.createDelegate(this, this.qweqwe));
    }

    function asdasd(sender, args) {
        console.log(this.currentUser.get_id());
    }
    function qweqwe(sender, args) {
        console.log(args.get_message());
    }

And replacing the top line with this.

             $(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', loadUser);
    });

Sometimes it brings the id and sometimes not, can someone help me to make this work constantly?

1
  • Just stumbled across this in my quest to add some validation at the column level in my SPO List. Ideally, everyone needs to have the view to the column but only certain users should have the rights to change the content. Wondering if it would work to restrict edit rights by adding logic in the validation to check the .userid against a permission group and if not found then set to read only on that column id. Anyone tried that?
    – user87433
    Commented Nov 7, 2019 at 16:46

1 Answer 1

10

Can you not use _spPageContextInfo? It has the current user ID built in:

_spPageContextInfo.userId

https://social.technet.microsoft.com/wiki/contents/articles/29766.sharepoint-understanding-the-sppagecontextinfo-object.aspx

2
  • thanks a lot! I didn't know you could do that! I found a lot of people that did the other way haha
    – uhcnamuf
    Commented Mar 27, 2017 at 20:16
  • Yeah, _spPageContextInfo is very very useful! Commented Mar 27, 2017 at 20:19

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.