Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top