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

  • 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. Oct 30 '15 at 10:45
  • ClientWebPart is a Web Part Page – Melad Batta Oct 30 '15 at 10:50
  • can i use something other than _spPageContextInfo?? – Melad Batta Oct 30 '15 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. Oct 30 '15 at 10:59
  • you can test run my code and see if you get error – Melad Batta Oct 30 '15 at 11:06
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");
| improve this answer | |
  • I get same error massage (_spPageContextInfo is undefined) – Melad Batta Oct 30 '15 at 11:48

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.