Saturday, December 04, 2010

Sharepoint My Site Profile Image does not show in Discussion Board and Main Site Collection

In Sharepoint 2007, the user profile pictures sometimes do not appear or show in the main site collection such as the discussion board and the manage user permission page. This is a Sharepoint known bug which has not been fixed by any hotfix or patches. For example, the discussion board below shows that the profile picture is empty:
However, this user has already uploaded a profile image in My Site.
This issue is due to the profile picture URL is not sync to Sharepoint User Information List. The synchronization somehow has gone wrong when you have multiple site collection or when you are using HTTPS. Besides the profile picture URL, other information such as the Email, Firstname, Lastname may also go wrong sometimes.

To fix this issue, the profile picture URL, or other missing information, must be copied over from My Site to User Information List. A schedule task/job must be created to run hourly or daily to execute the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Administration;

namespace UserProfilePictureJob
{

    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                using (SPSite GlobalSite = new SPSite(args[0]))
                {
                    //ServerContext object current site
                    ServerContext context = ServerContext.GetContext(GlobalSite);

                    // get the user profile manager
                    UserProfileManager userProfileManager = new UserProfileManager(context);

                    using (SPWeb GlobalWeb = GlobalSite.OpenWeb())
                    {
                        SPList userInfo = GlobalWeb.SiteUserInfoList;

                        foreach (UserProfile profile in userProfileManager)
                        {
                            if (profile != null) // if user exists in profile database
                            {
                                if (userInfo.Fields.ContainsField("Picture"))
                                {
                                    SPListItem itemUser;

                                    SPQuery query = new SPQuery();
                                    query.Query = string.Format(@"<Where><And><Eq><FieldRef Name='Name' /><Value Type='Text'>" + profile["AccountName"] + "</Value></Eq>" +
                                        "<Eq><FieldRef Name='ContentType' /><Value Type='Text'>Person</Value></Eq></And></Where>");
                                    SPListItemCollection items = userInfo.GetItems(query);

                                    if (items.Count > 0)
                                    {
                                        itemUser = items[0];
                                    }
                                    else
                                        itemUser = null;

                                    if (itemUser != null)
                                    {
                                            GlobalWeb.AllowUnsafeUpdates = true;
                                            if (profile["PictureURL"].Value == null)
                                                itemUser["Picture"] = null;
                                            else
                                            {
                                                itemUser["Picture"] = profile["PictureURL"].Value;
                                                string thumbnailpictureurl = (string)profile["PictureURL"].Value;
                                                
                                                thumbnailpictureurl = thumbnailpictureurl.Substring(0, thumbnailpictureurl.Length - 4) + "_" + thumbnailpictureurl.Substring(thumbnailpictureurl.Length - 3, 3) + ".jpg";
                                                thumbnailpictureurl = thumbnailpictureurl.Replace("Profile%20Pictures/", "Profile%20Pictures/_t/");
                                                thumbnailpictureurl = thumbnailpictureurl.Replace("Profile Pictures/", "Profile%20Pictures/_t/");

                                                string checkhttps = thumbnailpictureurl.Substring(0, 5);
                                                checkhttps = checkhttps.ToLower();

                                                string weburl = args[0];
                                                if (weburl.Substring(0, 5) == "https:" && checkhttps != "https")
                                                    thumbnailpictureurl = thumbnailpictureurl.Replace("http", "https");

                                                if (thumbnailpictureurl.IndexOf("_t/_t/") == -1) //if picture url not modified before
                                                {
                                                    //Update Picture url in User Information List
                                                    itemUser["Picture"] = thumbnailpictureurl;

                                                    //Update Picture URL in mysite so that...
                                                    //it displays thumbnail in the My Profile page and Search result page
                                                    profile["PictureURL"].Value = thumbnailpictureurl;
                                                    profile.Commit();
                                                }
                                                else
                                                {

                                                }

                                            }

                                            itemUser.Update();
                                            GlobalWeb.AllowUnsafeUpdates = false;
                                    }
                                }
                            }
                        }
                    }//end web
                }//endsite
            }
            else
            {
                Console.WriteLine("Incorrent argument");
            }
        }
    }
}


You may download a copy of the source code and project Here

2 comments:

Robert Kirchhof said...

The download link says the file has been deleted.

Robert Kirchhof said...

The download link says the file has been deleted.