locked
User profile photo - no photo from URL but can see photo in sharepoint. RRS feed

  • Question

  • Trying to figure this one out. I am tasked with writing some code to get a user's photo from Sharepoint so we can use it elsewhere. 

    My code works to get the photo using the employee's profile property for the photoURL. Problem is that when I do this, many photos come back as the no photo (the little person silhouette) but if I look at those people in SharePoint, office, and/or Skype they have a photo.

    I'm using C# and CSOM to do this. How do I get the photos? Is there another place to go get them that is more definitive over SharePoint tenant?

    Monday, June 5, 2017 7:55 PM

All replies

  • Hi,

    Are you using SharePoint online?

    If so, here is sample code for your reference:

    using (var clientContext = new ClientContext("https://xxx.sharepoint.com/sites/lee/"))
                {
                    string password = "pw";
                    SecureString sec_pass = new SecureString();
                    Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                    sec_pass.MakeReadOnly();
                    clientContext.Credentials = new SharePointOnlineCredentials("user@xxx.onmicrosoft.com", sec_pass);
    
                    // Get the people manager instance and load current properties
                    PeopleManager peopleManager = new PeopleManager(clientContext);
                    PersonProperties personProperties = peopleManager.GetMyProperties();                
    
                    clientContext.Load(personProperties, p => p.AccountName);
                    clientContext.ExecuteQuery();
    
                    //
                    var pictureURL= peopleManager.GetUserProfilePropertyFor(personProperties.AccountName, "PictureURL");
                    clientContext.ExecuteQuery();
                    Console.WriteLine(pictureURL.Value);
                    Console.ReadKey();
    
                }

    Here are some links for your reference.

    https://github.com/SharePoint/PnP/tree/master/Samples/UserProfile.Manipulation.CSOM

    https://github.com/SharePoint/PnP/tree/master/Samples/UserProfile.Manipulation.CSOM.Console

    For SharePoint Online case, I would suggest you post in online forum, so it would not cause misunderstanding.

    https://social.technet.microsoft.com/Forums/msonline/en-US/home?forum=onlineservicessharepoint

    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, June 6, 2017 3:11 AM
  • if you read my thing, I can get the photo but there isn't always a photo even if sharepoint has one..

    I've determined my problem is that MS doesn't sync the damn photos across AD, exchange, and sharepoint. so people have a photo but getting the URL from sharepoint doesn't mean sharepoint ever got it.

    Is there a way to get the photo using the Managed EWS API?

    Tuesday, June 6, 2017 12:36 PM
  • Hi,

    If you want to use EWS API to get user photo, you could check below thread.

    https://newsignature.com/articles/synchronizing-profile-pictures-exchange-online-sharepoint-online/

    private static void GetContactPhoto(ExchangeService service, string ItemId)
    {
       // Bind to an existing contact by using the ItemId passed into this function.
       Contact contact = Contact.Bind(service, ItemId);
       // Load the contact to get access to the collection of attachments.
       contact.Load(new PropertySet(ContactSchema.Attachments));
       // Loop through the attachments looking for a contact photo.
       foreach (Attachment attachment in contact.Attachments)
       {
          if ((attachment as FileAttachment).IsContactPhoto)
          {
             // Load the attachment to access the content.
             attachment.Load();
          }
       }
       FileAttachment photo = contact.GetContactPictureAttachment();
       // Create a file stream and save the contact photo to your computer.
       using (FileStream file = new FileStream(photo.Name, FileMode.Create, System.IO.FileAccess.Write))
       {
          photo.Load(file);
       }
    }

    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

    • Proposed as answer by Dennis Guo Thursday, June 15, 2017 9:31 AM
    Thursday, June 8, 2017 7:16 AM