Automating SharePoint Site Creation: The Icon

You can do this relatively simple task--ensuring that the site icon is not the out-of-box SharePoint icon on each and every new site--by using Windows PowerShell.

It is 9 AM on Wednesday and somehow I’ve managed to put in well over 60 hours so far this week, with two all-nighters, working on the SharePoint farm for NBC Olympics. It's going to be an amazing race to Opening Ceremonies on July 27th.

Scripting to Save Time

As part of the deployment, I’ve had to squeeze a project and development lifecycle into a matter of hours, working to create an attractive and functional standard for team sites. Due to a variety of requirements and constraints, it is critical that I script the deployment and configuration of team sites—at least as much as I know how to do.

This week, I’m going to share with you the steps I use to achieve a relatively simple task: ensuring that the site icon is not the out-of-box SharePoint icon on each and every new site. While the task is simple, it entails using Windows PowerShell to touch and automate a variety of settings, components, and actions. So it ends up being a pretty interesting example.

(And speaking of PowerShell, don’t miss the chance to learn from me and SharePoint PowerShell überguru Gary Lapointe next week, as we deliver “Administering SharePoint with Windows PowerShell: Zero to Sixty in Three,”an online webinar, on Thursday May 31 at 11am Eastern Time.)

The PowerShell Script

I use a script similar to my site provisioning script (see my SharePoint Pro article "Create a SharePoint Site Collection with Windows PowerShell UI Style") to create each site. But when it’s finished, I’m stuck with the out-of-box Team Site provided by SharePoint.

Now, I know that the “best practice” would be to create a custom site definition, but time, money, and development cycles don’t allow that in this case, so we’re going to do as much as we can with scripts, which make it easy to test, deploy, and update over time.

At the end of the site collection script, we have two variables: $web and $url. These variables allow us to continue on with the tweaks we’re just about to discuss. But if you want to work on an existing site, you must first define the variables:

$url = "http://webapp.contoso.com/sites/SiteCollection"
$web = Get-SPWeb –Identity $url

Now my very simple goal is to configure the site icon, and along the way to share a couple of tips.

The first tip, for those of you that will be doing branding without the help of a knowledgeable branding expert and the ability to package changes into SharePoint solution packages (wsps)—create a document library to hold branding assets only. You can then keep that library up-to-date and synchronized with a master copy.

So we begin by creating a library that I like to call SITE BRANDING.

# Create SITE BRANDING document library
$listTemplate=$web.ListTemplates["Asset Library"] 
$web.Lists.Add("Site Branding","Library to store branding elements",$listTemplate) 

First, we create a reference to the list template for SharePoint 2010’s Asset Library, which is optimized for hosting media. Then we create a document library by using the Add method of the web’s Lists collection.

Next, we ensure that our new SITE BRANDING library doesn’t appear on the Quick Launch—there’s no need for users to be poking around in it.

# Modify SITE BRANDING document library
$spDocumentLibrary = $web.Lists["Site Branding"]
$spDocumentLibrary.OnQuickLaunch = $false
$spDocumentLibrary.Update()

We create a reference to our new library, then we set its OnQuickLaunch property to false, and update the object to save our changes.

Now we get to upload branding files to the document library.

What? You’ve never used PowerShell to upload documents to SharePoint? It’s easy!

# Upload branding files
$spFolder = $web.GetFolder("Site Branding")
$spFileCollection = $spFolder.Files

We create a reference to the root folder of the SITE BRANDING library—every library has a root folder at the … root. We then create a reference to the Files collection of that folder.

Now comes the tricky part of the upload. We grab ahold of the master branding folder and—just for sake of example—we filter so that we’re only going to upload JPGs.

# Upload multiple files
Get-ChildItem "C:\Users\Dan\Documents\sharepoint\branding" -filter "*.jpg” | 
ForEach { 
     $spFileCollection.Add(“Site Branding/$($_.Name)”,$_.OpenRead(),$true) 
} 

We then pipe each of the pictures to a ForEach cmdlet, which adds the file to the collection.

Finally, we use one of these pictures as the site icon.

This is just a matter of setting the correct property of the SPWeb object, but it took several tries to figure out that in order for the script to work in all scenarios, you really should use a fully-qualified URL as the path to the icon.

# Set website icon
$web.SiteLogoUrl = $web.Url + "/Site Branding/SiteImageIcon.jpg"
$web.Update()


Generic Name

One small tip: Notice I use a picture that has a very generic name. The name is not tied to any specific time, place, or company. For example, it’s not called “NBC Olympics London 2012 Icon.”

Instead, it’s generic, which allows me to easily swap it out or update it across my environment. Just update the SiteImageIcon.jpg file in all Site Branding libraries and voila!

So there’s a little PowerShell in action. I’ll be sharing the rest of the script that I use to deploy highly customized team sites over coming weeks. Happy scripting!

Discuss this Blog Entry 1

on May 24, 2012
This is great. I have done a few things similar to this before, but never the site icon. I am borrowing this.

Please or Register to post comments.

What's Dan Holme's Viewpoint on SharePoint Blog?

SharePoint expert Dan Holme shares tips, how-to's, ideas, and news about all things SharePoint, and more.

Contributors

Dan Holme

Dan Holme's 18 years of experience and his impact on hundreds of thousands of IT professionals and business decision makers have earned him a reputation as one of the world's most respected...
IdeaXchange

Come join the IdeaXchange conversation!

Read the latest from our Xperts, and make your voice heard.

Want to learn more? Check out the FAQs.

eBooks For You
Join the Conversation
Blog Archive

Sponsored Introduction Continue on to (or wait seconds) ×