none
Retrieve Picture of user SharePoint Online and Rest RRS feed

  • Question

  • i'm trying to retrieve the picture url of users from their SharePoint User Information page. But i'm receiving a 500 internal server error. The code snippet below used to work before we moved to SharePoint Online. Since then the Rest calls have been giving errors.

    looking on line i have noticed a small difference

    userDetails = i:0#.f|membership|jane.Doe@mycompany.com

    but what i see on line is : i%3A0%23.f%7Cmembership%7Cuser%40domain.onmicrosoft.com

    the Onmicrosoft.com. Not sure if that the reasonbut any help will be appreciated

    var getsiteContacts = function siteContacts () {
        
             // execute AJAX request
            $.ajax({
                     url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Site Contact')/items?$select=user/Title,user/Name&$expand=user",
                     type: "GET",
                     headers: { "ACCEPT": "application/json;odata=verbose" },
                     success: function (data) {
    
    	     	
                      
                     var DataResult = data.d.results;
                     //var user = DataResult.userId;
                     for (var i = 0; i < DataResult.length; i++) {
    
                        if(DataResult[i].user!=null){
                          
                           var userDetails = DataResult[i].user.Name;
                           
    
    
                            //Get the user profile picture for each user
                            console.log(userDetails);
                            console.log(data);
                            				
                     }
                        getProfilePicture(userDetails);	
    
                    }	              
                              
                               
    	         },
                    error: function () {
                    //alert("Failed to get details");
                }
                
            });
    
          
        }
    
        function getProfilePicture (userDetails){
    
            var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='"+encodeURIComponent(userDetails)+"'";
            console.log(requestUri);
    
             $.ajax({
                            url: requestUri,
                            type: "GET",
                            headers: { "ACCEPT": "application/json;odata=verbose" },
                            success: function (data) {
    
                                var itemsPictureUrl = [];
                                var itemsPicturehome = [];
    
                                itemsPictureUrl.push('<img src="' +data.d.PictureUrl+ '" height="50" width="50">');
                                $("#contactprofilepicture").append(itemsPictureUrl);
                                itemsPicturehome.push('<a href="' +data.d.UserUrl+ '">'+data.d.DisplayName+'</a><p>Please Contact me for any issues about the site</p>');
                                $("#contactname").append(itemsPicturehome);
    
                            
                             console.log(data);
    
                                    
                        },
                            error: function () {
                            //alert("Failed to get details");
                        }
                    });
    
        }


    Monday, March 27, 2017 8:30 AM

Answers

  • Hi,

    the sintax it's correct "i%3A0%23.f%7Cmembership%7Cuser%40domain.onmicrosoft.com" maybe the problem is the value of your variable userDetails, because if you try with postman or in your browser authenticated on SharePoint Oline you are able to get data:

    https://<tenat>.sharepoint.com/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i%3A0%23.f%7Cmembership%7C<user>%40<tenant>.onmicrosoft.com'

    I tried on my tenant and works well.


    Giuliano De Luca - MCSD SharePoint, MCSD Web Applications, MCPS, MS. www.delucagiuliano.com Twitter: @giuleon Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    • Marked as answer by dongotti Tuesday, April 4, 2017 7:11 AM
    Monday, March 27, 2017 10:06 AM
  • thanks De Luca

    apparently it was an update that caused it to stop working, seems to have started working again

    thank you so much for your help

    • Marked as answer by dongotti Tuesday, April 4, 2017 7:11 AM
    Tuesday, March 28, 2017 10:49 AM

All replies

  • Hi,

    the sintax it's correct "i%3A0%23.f%7Cmembership%7Cuser%40domain.onmicrosoft.com" maybe the problem is the value of your variable userDetails, because if you try with postman or in your browser authenticated on SharePoint Oline you are able to get data:

    https://<tenat>.sharepoint.com/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i%3A0%23.f%7Cmembership%7C<user>%40<tenant>.onmicrosoft.com'

    I tried on my tenant and works well.


    Giuliano De Luca - MCSD SharePoint, MCSD Web Applications, MCPS, MS. www.delucagiuliano.com Twitter: @giuleon Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    • Marked as answer by dongotti Tuesday, April 4, 2017 7:11 AM
    Monday, March 27, 2017 10:06 AM
  • thanks De Luca

    apparently it was an update that caused it to stop working, seems to have started working again

    thank you so much for your help

    • Marked as answer by dongotti Tuesday, April 4, 2017 7:11 AM
    Tuesday, March 28, 2017 10:49 AM
  • Hi,

    Thanks for your feedback, you could mark your solution as answer, so it may help others who faced similar issue.

    Thanks for your understanding.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 28, 2017 11:54 AM