List out user profiles that does not have profile picture

This post is a contribution from Charls Tom Jacob, an engineer with the SharePoint Developer Support team.

Recently, one of my customers came up with an issue related to user profiles.  The problem was for certain users, profile picture wasn’t showing up.  Instead the typical red X sign indicating a missing image file was displayed.

image

On investigating this, we found that user profile images are missing from the picture library that stores profile photos.  We verified IIS logs to confirm that HTTP error code 404 (File Not Found) was recorded for requests to load image files.

The solution was to place those missing images back to the library.  Fortunately, customer had a repository of all user images and they were ready to restore them back.  But the site contained more than 10,000 users.  The challenge was to find out how many of those user’s do not have their profile images.

We used the following object model code to list our users that does not have images available in picture library.  It uses UserProfileManager class to iterate through all the user profiles and if the profile has PictureUrl attribute set, an HttpRequest is sent to fetch the image.  Looking at the HTTP response we get back, the code decides if the image is actually available or not.

Here’s the sample code:

 private static void ListProfileWithoutPhotos()
 {
     string mySiteUrl = "https://intranet.contoso.com/my/";
     
     using (SPSite site = new SPSite(mySiteUrl))
     {
         try
         {
             SPServiceContext context = SPServiceContext.GetContext(site);
             UserProfileManager profileManager = null;
                         
             if (context!=null)
                 profileManager = new UserProfileManager(context);
             else
             {
                 throw new Exception("Server Context Object is NULL!");
             }
  
             if (profileManager != null)
             {
                 foreach (UserProfile profile in profileManager)
                 {
                     oCount++;
                     if (profile[PropertyConstants.PictureUrl].Value == null)
                     {
                         profilesWithoutImage++;
                     }
                     else
                     {
                         FetchImage(profile[PropertyConstants.PictureUrl].Value.ToString());
                     }
                 }
             }
             else
             {
                 throw new Exception("User Profile Manager Object is NULL!");
             }
  
             Console.WriteLine("Found total {0} profiles, {1} without photos, {2} with photo, but missing image", oCount, profilesWithoutImage, profilesWithInvalidImageUrl);
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message + ex.StackTrace);
         }
     }
 }
  
 private static void FetchImage(string url)
 {
     HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(url);
     request.Timeout = 5000;
     request.UseDefaultCredentials = true;
     try
     {
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     }
     catch (System.Net.WebException ex)
     {
         if (ex.Message.Contains("404"))
         {
             profilesWithInvalidImageUrl++;
             Console.WriteLine("Image not found at " + url);
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message + ex.StackTrace);
     }
 }

We hope this code sample is of help to you!