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.
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 = "http://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++;
FetchImage(profile[PropertyConstants.PictureUrl].Value.ToString());
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;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
catch (System.Net.WebException ex)
if (ex.Message.Contains("404"))
profilesWithInvalidImageUrl++;
Console.WriteLine("Image not found at " + url);
We hope this code sample is of help to you!
This post is a contribution from Nishand Vasudevan, an escalation engineer with the SharePoint Developer Support team.
Disabling the event receivers can stop the workflow events. For example, disabling the event can stop workflow from starting and could stops firing events like OnTaskChanged.
The below code will not trigger OnTaskChanged event that otherwise would. The code snippet is to depict what you shouldn’t be doing in your code and it’s an illustrative example.
1: public class EventFiringHandler : SPItemEventReceiver
2: {
3: public void DisableHandleEventFiring()
4: {
5: this.EventFiringEnabled = false;
6: }
7:
8: public void EnableHandleEventFiring()
9: {
10: this.EventFiringEnabled = true;
11: }
12: }
1: EventFiringHandler handler = new EventFiringHandler();
2: SPSite mysite = new SPSite("http://localhost/");
3: SPWeb web = mysite.OpenWeb();
4: SPList list = web.Lists["Tasks"];
5: SPListItem item = list.GetItemById(<taskItemID>);
6: Hashtable hashT = new Hashtable();
7: hashT.Add("Title","MyNewTitle");
8: handler.DisableHandleEventFiring();
9: SPWorkflowTask.AlterTask(item, hashT, true);
The property bag is intended for small properties, like configuration or other settings. Accessing the property bag will require its own query so it comes with performance penalty. The property bag won’t get initialized when SPWeb object instantiates when calling SPSite.OpenWeb().