Tuesday, December 20, 2011

Replace the logo on your SharePoint site via a Feature

The following C# class is the code you can associate with a feature that will replace the standard logo of your SharePoint site with a custom logo once that feature is activated.  When the feature is deactivated, it will reset the logo back to the default SharePoint logo.  Naturally, you'll want to update the reference to the logo with the proper location in which your custom logo is stored under the 12 (SharePoint 2007) or 14 (SharePoint 2010) hive.  In this particalur instance, I've got mine located under "_layouts/IMAGES/CustomLogoFolder/Custom_logo.png" (which translates to 14/TEMPLATE/IMAGES/CustomLogoFolder/Custom_logo.png on a SharePoint 2010 farm).

    public class UpdateLogoFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            base.FeatureActivated(properties);
            SPWeb currentWeb;
            using (currentWeb = properties.Feature.Parent as SPWeb)
            {
                try
                {
                    currentWeb.AllowUnsafeUpdates = true;
                    StringBuilder sbServerRelativeUrl = new StringBuilder(currentWeb.ServerRelativeUrl);
                    if (!currentWeb.ServerRelativeUrl.EndsWith(@"/"))
                    {
                        sbServerRelativeUrl.Append(@"/");
                    }
                    StringBuilder sbSiteLogoUrl = new StringBuilder(sbServerRelativeUrl.ToString());
                    sbSiteLogoUrl.Append(@"_layouts/IMAGES/CustomLogoFolder/Custom_logo.png");
                    currentWeb.SiteLogoUrl = sbSiteLogoUrl.ToString();
                    currentWeb.Update();
                }
                catch (Exception ex)
                {
                    WriteToEventLog(ex.Message, EventLogEntryType.Error);
                }
                finally
                {
                    currentWeb.AllowUnsafeUpdates = false;
                }
            }
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            base.FeatureDeactivating(properties);
            SPWeb currentWeb;
            using (currentWeb = properties.Feature.Parent as SPWeb)
            {
                try
                {
                    currentWeb.AllowUnsafeUpdates = true;
                    StringBuilder sbServerRelativeUrl = new StringBuilder(currentWeb.ServerRelativeUrl);
                    if (!currentWeb.ServerRelativeUrl.EndsWith(@"/"))
                    {
                        sbServerRelativeUrl.Append(@"/");
                    }
                    StringBuilder sbSiteLogoUrl = new StringBuilder(sbServerRelativeUrl.ToString());
                    sbSiteLogoUrl.Append(@"_layouts/IMAGES/siteicon.png");
                    currentWeb.SiteLogoUrl = sbSiteLogoUrl.ToString();
                    currentWeb.Update();
                }
                catch (Exception ex)
                {
                    WriteToEventLog(ex.Message, EventLogEntryType.Error);
                }
                finally
                {
                    currentWeb.AllowUnsafeUpdates = false;
                }
            }
        }
    }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.