0

im trying to get user profile picture in my Cilent Web Part but I get a error massage (_spPageContextInfo is undefined).

I'm useing REST API

jQuery(document).ready(function () {
            GetCurrentUser();

            function GetCurrentUser() {
                $.ajax({
                    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl",
                    type: "GET",
                    headers: {
                        "accept": "application/json;odata=verbose",
                    },
                    success: function (data) {
                        var userName = data.d.DisplayName;
                        var picUrl = data.d.PictureUrl;
                        $('#bild').attr('src', picUrl);
                    },
                    error: function (error) {
                        alert(JSON.stringify(error));
                    }
                });
            }
        });

are there other ways to solve it ? would be appreciated if you could help me solve it

7
  • Looks like you are executing the code not in an SP Dom Context. _spPageContextInfo is directly written on the page via SSOM so you can use it nearly immediately. What do you mean by ClientWebPart? An SP Hosted APP?
    – Mx.
    Commented Oct 30, 2015 at 10:45
  • ClientWebPart is a Web Part Page Commented Oct 30, 2015 at 10:50
  • can i use something other than _spPageContextInfo?? Commented Oct 30, 2015 at 10:51
  • WebPart-Page is the layout you are using for your page.The _spPageContextInfo is the only suitable object to get the siteAbsoluteUrl. If this is not working for you you simply do something wrong.
    – Mx.
    Commented Oct 30, 2015 at 10:59
  • you can test run my code and see if you get error Commented Oct 30, 2015 at 11:06

1 Answer 1

0

Surround GetCurrentUser call with ExecuteOrDelayUntilScriptLoaded to ensure _spPageContextInfo exists before you use it. You should be able to have the actual function code outside ExecuteOrDelayUntilScriptLoaded for clarity.

ExecuteOrDelayUntilScriptLoaded(function ()
{
    GetCurrentUser();
}, "sp.js");
1
  • I get same error massage (_spPageContextInfo is undefined) Commented Oct 30, 2015 at 11:48

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.