Top Menu

Showing posts with label MySite. Show all posts
Showing posts with label MySite. Show all posts

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>

Tuesday, May 7, 2013

PowerShell - Saving Site and Sub-site in XML file

Scenario:
I had a requirement today to get all personal sites (site collections) and the blog sites (sub-sites) under MySite Host Site and save in an XML file. 

Following powershell script does the trick for a web application: 

PowerShell Script:
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$webs =  Get-SPSite -webapplication "http://mysite.domain.com"
$FilePath = "C:\Temp\MySites.xml"

Write-Host Getting all MySites...

$xml = "<PersonalSite>"
Foreach ($oneweb in $webs)
{
    $web = $oneweb.RootWeb

    if ($web.WebTemplate -eq "SPSMSITEHOST" -Or $web.WebTemplate -eq "SPSPERS")
    {
        $xml = $xml + "<Site>"
        $xml = $xml + "<MySite>" + $oneweb.URL + "</MySite>"
        Write-Host "Processing:" $oneweb.URL
            
        $spWeb = Get-SPweb $oneweb.URL
        foreach($subSite in $spWeb.Webs)
        {
            if ($subSite.WebTemplate -eq "BLOG")
            {
                $xml = $xml + "<Blog>" + $subSite.URL + "</Blog>"
            }
        }
        $spWeb.Dispose()

        $xml = $xml + "</Site>"
    }    
}
$xml = $xml + "</PersonalSite>"
#Write-Host $xml

$xml | out-File -FilePath $FilePath

Write-Host "XML file has been generate."
Write-Host "Press anyke to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Result (MySites.xml):
<PersonalSite>
  <Site>
    <MySite>http://mysite.domain.com/my</MySite>
  </Site>
  <Site>
    <MySite>http://mysite.domain.com/my/personal/mjackson</MySite>
    <Blog>http://mysite.domain.com/my/personal/mjackson/Blog</Blog>
  </Site>
  <Site>
    <MySite>http://mysite.domain.com/my/personal/jdoe</MySite>
    <Blog>http://mysite.domain.com/my/personal/jdoe/Blog</Blog>
  </Site>
  <Site>
    <MySite>http://mysite.domain.com/my/personal/sman</MySite>
    <Blog>http://mysite.domain.com/my/personal/sman/Blog</Blog>
  </Site>
</PersonalSite>

Tuesday, January 29, 2013

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...