Monday, June 24, 2013

Programmatically changing the logo to Sharepoint all sites and subsites

I have found one of the very useful code below to change logo to Sharepoint all sites and subsites
using Console application. Happy coding. Cheers.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
namespace UpdateLogo
{
    class Program
    {
        static void Main(string[] args)
        {
            string site;
            string siteLogo;
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Web Application URL:");
                    site = Console.ReadLine();
                    Console.WriteLine("Enter the Site Logo URL:");
                    siteLogo = Console.ReadLine();
                }
                else
                {
                    site = args[0];
                    siteLogo = args[1];
                }
                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;
                //Enumerate through each site
                foreach (SPSite tmpSite in tmpRootColl)
                {
                    //Enumerate through each web for the site
                    foreach (SPWeb tmpWeb in tmpSite.AllWebs)
                    {
                        //Update the logo for the current Web
                        tmpWeb.AllowUnsafeUpdates = true;
                        tmpWeb.SiteLogoUrl = siteLogo;
                        tmpWeb.Update();                                         
                        tmpWeb.AllowUnsafeUpdates = false;
                        //Log to a file, where the logo is applied!
                        StreamWriter SW;
                        SW = File.AppendText("C:\\LogoLog.txt");
                        SW.WriteLine(tmpWeb.Url);
                        SW.Close();
                        //Dispose of the Web Object
                        tmpWeb.Dispose();
                    }
                    //Dispose of the Site Object
                    tmpSite.Dispose();
                }
                //Dispose of the Root Site Object
                tmpRoot.Dispose();
                //Confirmation Message
                Console.WriteLine("The operation completed successfully");
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Logo Updater", ex.Message);
                //Failure Message
                Console.WriteLine("The operation failed");
            }
        }
    }
}

1 comment: