Picture Upload Tool For SharePoint MySite

Few days back a friend of mine asked me about using standard photos for all mysite users.  He actually have a SharePoint Server portal at his company and is widely used but the problem is that everyone has uploaded their mysite profile pictures which are something funny and some time annoying for some of the employees, so he asked me about a utility that make sure that everyone in the company having mysite have his personal official picture so he contact me to develop it.  The good thing here that the portal has a document library on their HR web site containing pictures and all information about employee including his email and Active Directory User Name.    There were other solutions like putting the image in Active Directory but it was rejected by IT guys for some unknown reason so we have to stick with our custom Solution.  

Solution

1. Check all the User Profiles in the Portal Web Applications

2. Check If Mysite Exist

3. Check If User Picture Exist in HR Employee Details List

4. Check If Image already Exist in his Mysite Shared Picture\Profile Picture folder

5. If Picture name is same just go to next profile

6. If Profile Picture folder or Picture does not exist

7. Upload the Picture to MySite Shared Picture\Profile Picture folder

8. Update User Profile Property Picture

So the Source code to do this job is given below.

Code Details

Imports

using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
using System.Configuration;

//The method below will take Source Web site and User Account Name as parameter and return SPFile object if user Picture Exists in the site.

static SPFile GetUserPicture(string SourceWebURL, string UserName)
        {
            string PictureLibraryWebURL = GetConfigEntry("PictureLibrarySiteURL");
            string PictureLibraryName = GetConfigEntry("PictureLibraryName");
            string UserIDColumnName = GetConfigEntry("UserIDColumnName");
            SPListItem Picture = null;

SPQuery aQuery = null;

            try
            {
                using (SPSite SourceSite = new SPSite(SourceWebURL))
                {
                    using (SPWeb SourceWeb = SourceSite.AllWebs[PictureLibraryWebURL])
                    {
                        string strQuery = "<Where><Eq><FieldRef Name=" + UserIDColumnName + " /><Value Type=\"Text\">" + UserName + "</Value></Eq></Where>";
                        string viewFields = "<FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" />";

                        aQuery = new SPQuery();
                        aQuery.Query = strQuery;
                        aQuery.ViewFields = viewFields;

                        SPList aList = SourceWeb.Lists[PictureLibraryName];
                        SPListItemCollection PictureItemCol = aList.GetItems(aQuery);

                        if (PictureItemCol != null)
                        {
                            if (PictureItemCol.Count > 0)
                            {
                                Picture = PictureItemCol[0];
                            }
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return Picture.File;
        }

//This Method Checks if the Picture for the current User Exists or not.

//Sending Picture Name as parameter will make sure that the picture in mysite is same as central image.

static bool IsPictureAlreadyExists(string PictureName, SPWeb MySiteWeb)
       {
           bool Exist = false;
           SPFolder TargetFolder = null;

           string PictureLibraryName = "Shared Pictures";
           string TargetFolderName = "Profile Pictures";

           try
           {
               TargetFolder = MySiteWeb.GetFolder(PictureLibraryName);
               TargetFolder = TargetFolder.SubFolders[TargetFolderName];
               if (TargetFolder.Files.Count > 0)
               {
                   foreach (SPFile aFile in TargetFolder.Files)
                   {
                       if (aFile.Name.ToString().ToLower() == PictureName)
                       {
                           Exist = true;
                       }
                   }
               }
           }
           catch
           {
               Exist = false;
           }
           return Exist;
       }

//Copy Picture will Upload the File to User Mysite Shared Pictures Library in the Profile Picture folder. 

//Folder will be created automatically if does not exist.

//This method will return the path of the image to be updated in the user profile Picture Property

static string CopyFile(SPFile Picture, SPWeb MySiteWeb)
        {
            string ProfilePictureURL = "";
            SPFolder TargetFolder = null;
            SPFile UploadedFile = null;

           string TargetFolderName = "Profile Pictures";

           try
           {

               TargetFolder = MySiteWeb.GetFolder("Shared Pictures");

               try
               {
                   TargetFolder = TargetFolder.SubFolders[TargetFolderName];
               }
               catch
               {
                   TargetFolder = TargetFolder.SubFolders.Add(TargetFolder.ServerRelativeUrl + "/" + TargetFolderName);
               }

               byte[] binFile = null;

               binFile = Picture.OpenBinary();
               MySiteWeb.AllowUnsafeUpdates = true;

               UploadedFile = TargetFolder.Files.Add(Picture.Name.ToString(), binFile, true);

               ProfilePictureURL = UploadedFile.Url;
               MySiteWeb.AllowUnsafeUpdates = false;

               binFile = null;
               UploadedFile = null;

           }
           catch
           {
               return "";
           }

            return ProfilePictureURL;
        }

//Method to Read from Config File.

static string GetConfigEntry(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key].ToString();
        }

//The main Method will loop on all User Profiles and Check for My Site.  If mysite Exist then it will upload the photo otherwise skip the photo

static void Main(string[] args)
        {
            string RootWebApplicationURL = GetConfigEntry("RootWebApplicationURL");
            string MySiteWebApplicationURL = GetConfigEntry("MySiteWebApplicationURL");
            //string Domain = GetConfigEntry("Domain");

            string UserAccount = "";
            string PersonalPicURL = "";
            string PersonaURL = "";
            using (SPSite MySiteWebApplication = new SPSite(MySiteWebApplicationURL))
            {
                ServerContext mySitecontext = ServerContext.GetContext(MySiteWebApplication);
                UserProfileManager profileManager = new UserProfileManager(mySitecontext);

                foreach ( UserProfile profile in profileManager)
                {
                    PersonaURL = profile.PersonalUrl.ToString();

                    try
                    {
                        SPSite PSite = profile.PersonalSite;
                        if (PSite == null)
                        {
                            continue;
                        }
                    }
                    catch
                    {
                        continue;
                    }

                    SPWeb CurrentUserMySiteWeb = profile.PersonalSite.RootWeb;
                    UserAccount = CurrentUserMySiteWeb.Site.Owner.LoginName.ToString();

                    SPFile Picture = GetUserPicture(RootWebApplicationURL, UserAccount);

                    if (Picture != null)
                    {
                        if (!IsPictureAlreadyExists(Picture.Name.ToLower(), CurrentUserMySiteWeb))
                        {
                            PersonalPicURL = CopyFile(Picture, CurrentUserMySiteWeb);
                            PersonalPicURL = CurrentUserMySiteWeb.Url + "/" + PersonalPicURL;

                            profile[PropertyConstants.PictureUrl].Value = PersonalPicURL;
                            profile.Commit();
                        }
                    }
                }
                Console.WriteLine("Process Finished");
                Console.ReadLine();
            }

        }

//Configuration Entries

<appSettings>
    <add key="RootWebApplicationURL" value="http://sharepoint2007"/>
    <add key="MySiteWebApplicationURL" value="http://sharepoint2007"/>
    <add key="PictureLibrarySiteURL" value="/HR" />
    <add key="PictureLibraryName" value="User Pictures"/>
    <add key="UserIDColumnName" value="UserName"/>
</appSettings>

Complete Source Code

http://cid-a8677d5751e6b4da.skydrive.live.com/self.aspx/Public/MySiteSolutions.rar

Technorati Tags:
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s