0

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?

7

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

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

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.