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?

  • 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 Nov 7 at 16:46
8

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.