Monday, November 11, 2013

SharePoint 2013 - Download MySite Profile Picture

Scenario:
There was a requirement to download all the profile pictures from SharePoint 2013 MySite and save them in different folders (based on user's domain name). After downloading images need to be resized to 30x30 pixels.


Solution:
I wrote following command line utility to achieve the goal.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
using System.Net;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;

namespace MySiteDownloader
{
    class Program
    {
        static string strURL = ConfigurationManager.AppSettings["MySiteURL"];
        static string strDomain = ConfigurationManager.AppSettings["Domain"];
        static string strLogin = ConfigurationManager.AppSettings["Login"];
        static string strPassword = ConfigurationManager.AppSettings["Password"];
        

        static void Main(string[] args)
        {
            Console.WriteLine("MySite Downloader");
            Console.WriteLine("=================");

            GetProfilePictures();

            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }

        static void GetProfilePictures()
        {
            try
            {
                string strAccount = null;
                string strProfilePicURL = null;
                string strFileName = null;
                Uri uri = null;

                WriteLog("Getting user profiles from " + strURL);

                using (SPSite site = new SPSite(strURL))
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager upm = new UserProfileManager(context);
                    WriteLog("Total user profiles found: " + upm.Count);

                    foreach (UserProfile profile in upm)
                    {
                        strAccount = profile.GetProfileValueCollection("AccountName").ToString();

                        try
                        {
                            strProfilePicURL = profile.GetProfileValueCollection("PictureURL").ToString();
                        }
                        catch(System.Exception ex)
                        {
                            strProfilePicURL = "";
                        }

                        WriteLog(strAccount + " - " + strProfilePicURL);

                        if (strProfilePicURL != "")
                        {
                            uri = new Uri(strProfilePicURL);
                            strFileName = System.IO.Path.GetFileName(uri.LocalPath);

                            DownloadPicture(strProfilePicURL, strAccount.Split('\\')[0], strFileName);
                        }
                    }

                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        static  void DownloadPicture(string strPicURL, string strFolder, string strFile)
        {
            string FilePath = AppDomain.CurrentDomain.BaseDirectory + "\\" + strFolder + "\\" + strFile.Replace("_MThumb", "");

            //File Download

            try
            {
                using (WebClient Client = new WebClient())
                {
                    if (!System.IO.Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\" + strFolder))
                    {
                        System.IO.Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\" + strFolder);
                    }

                    Client.Credentials = new NetworkCredential(strLogin, strPassword, strDomain);
                    Client.DownloadFile(strPicURL, FilePath);
                }

            }
            catch(System.Exception ex)
            {
                WriteLog(ex.ToString());
            }

            

            //Image Processing

            try
            {
                var image = Image.FromFile(FilePath);
                var newImage = ScaleImage(image, 30, 30);
                newImage.Save(FilePath.Replace("_MThumb", ""), ImageFormat.Png);
                //System.IO.File.Delete(FilePath);
            }
            catch (System.Exception ex)
            {
                WriteLog(ex.ToString());
            }

        }

        public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }

        static void WriteLog(string strMessage)
        {
            Console.WriteLine(strMessage);

            string strLogMessage = string.Empty;
            string strLogFile = AppDomain.CurrentDomain.BaseDirectory;
            strLogFile += "MySiteDownloader.txt";
            StreamWriter swLog;

            strLogMessage = string.Format("{0}: {1}", DateTime.Now, strMessage);

            if (!File.Exists(strLogFile))
            {
                swLog = new StreamWriter(strLogFile);
            }
            else
            {
                swLog = File.AppendText(strLogFile);
            }

            swLog.WriteLine(strLogMessage);
            swLog.Close();

        }
    }
}
Note: Image re-sizing code has been copied from internet.

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="MySiteURL" value="http://mysite" />
    <add key="Domain" value="US" />
    <add key="Login" value="Login" />
    <add key="Password" value="Password" />
  </appSettings>
</configuration>

Publiahing Sites vs Communication Sites

It was announced in SharePoint Virtual Summit in May 2017, a more modern way of content publishing is released called Communication Sites. ...