Tuesday, 29 June 2010

Create a new Managed Metadata term with synonym in SharePoint 2010 using PowerShell

Use this script if you want to create a new term in the SharePoint 2010 Managed Metadata service with PowerShell. Incidentally, the Managed Metadata CSV import format provided out-of-the-box in SharePoint 2010 does not support synonyms, so you could easily use this code along with the Import-Csv PowerShell cmdlet to come up with a replacement format if you needed to support synonyms (will get around to posting a script at some point):

#Connect to Central Admin
$taxonomySite = get-SPSite http://centraladminsite:port

#Connect to Term Store in the Managed Metadata Service Application
$taxonomySession = Get-SPTaxonomySession -site $taxonomySite
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]
write-host "Connection made with term store -"$termStore.Name

#Connect to the Group and Term Set
$termStoreGroup = $termStore.Groups["Group Name"]
$termSet = $termStoreGroup.TermSets["Term Set Name"]

#Create term, term description, and a synonym
$term = $termSet.CreateTerm("Test Term", 1033)
$term.SetDescription("This is a test", 1033)
$term.CreateLabel("This is a test synonym", 1033, $false)

#Update the Term Store
$termStore.CommitAll()

#Dispose of taxonomy site object
$taxonomySite.Dispose()

Monday, 28 June 2010

Add SharePoint farm solution and bulk activate feature using PowerShell

There are two parts to this PowerShell script – the first part is fairly straightforward as it simply adds and installs a solution file into the farm. I have added a line to the end of the script to wait until the solution has fully deployed before continuing:

#Set up Web Application variable
#Only needed if solution contains Web Application scoped resources
$webApp = Get-SPWebApplication -Identity http://portal

#Add solution file to the farm
$solution = Add-SPSolution -LiteralPath "C:\Install\Solution.wsp"

#Deploy solution
#Add -WebApplication $webApp to the line below if solution contains Web Application scoped resources
Install-SPSolution $solution -Force -GACDeployment

#Wait for solution to be deployed
do {Start-Sleep -s 1} while ($solution.Deployed -eq $false)

If you have multiple solution files to deploy, then it is worth checking out this script from Gary Lapointe, which enables you to install and deploy solution files in bulk using an XML configuration file.

The second part of the script activates a site or site collection feature, which is also pretty standard, except that I have provided four scenarios:

  • Activate site scoped feature in one site collection

$site = Get-SPSite http://portal
Enable-SPFeature -Identity "FeatureName" -Url $site.Url
$site.Dispose()

  • Activate site scoped feature for all site collections in a Web Application

$webApp = Get-SPWebApplication -Identity http://portal
$webApp | Get-SPSite -limit all | ForEach-Object {Enable-SPFeature -Identity "FeatureName" -Url $_.Url}

  • Activate web scoped feature in one site

$web = Get-SPWeb http://portal
Enable-SPFeature -Identity "FeatureName" -Url $web.Url
$web.Dispose()

  • Activate web scoped feature for all sites in a site collection

$site = Get-SPSite http://portal
$site | Get-SPWeb -limit all | ForEach-Object {Enable-SPFeature -Identity "FeatureName" -Url $_.Url}
$site.Dispose()

C# code in a PowerShell script

Found this excellent article today from Stefan Go├čner which gives you the best of both worlds – access to the SharePoint Object Model using C# code in a PowerShell script. Great for re-using code you might already have written, for when you want to use the IntelliSense features in Visual Studio to create your code, or if you want to run code without having to compile an executable each time. Just make sure you test it on a dev environment first!!

Saturday, 26 June 2010

Create a new SharePoint site column using PowerShell

The script below can be used to create a new site column. It assumes you are creating the column in the top-level site of a site collection. It can be used to create columns in a Content Type Hub site, which can then be propagated to other site collections in the farm using the Managed Metadata Service Application (a story for another blog article maybe).

The main advantages of using PowerShell to create the site column are that you can document the changes, easily repeat them across development, staging and production environments, and you can set some properties that are not available to select in the UI (e.g., the “ShowIn” properties at the end of the XML string).

#Assumes that you are creating the site column in the top-level site of a site collection
#Script can also be used to create a site column in the Content Type Hub

#Get site and web object
$site = Get-SPSite -Identity "http://portal"
$web = $site.RootWeb

#Assign fieldXML variable with XML string for site column
$fieldXML = '<Field Type="DateTime"
Name="TestDate"
Description="This is a test date column."
DisplayName="Test Date"
StaticName="TestDate"
Group="Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'

#Output XML to console
write-host $fieldXML

#Create site column from XML string
$web.Fields.AddFieldAsXml($fieldXML)

#Example of changing the site column settings after creation
#Configure Test Date column to be Date Only instead of Date & Time
$dtField = $web.Fields["Test Date"]
$dtField.DisplayFormat = "DateOnly"
$dtField.Update()

#Dispose of Web and Site objects
$web.Dispose()
$site.Dispose()

Note at the end of the script that I call the new column back so that I can change a setting. You could add column settings to the XML during creation, but changing it after creation can be useful if you have already created the column and wanted to make further changes.

Friday, 25 June 2010

Hide Title column from a SharePoint list using PowerShell

There are a number of times where you may not want the Title column to appear in a list. It is not recommended to delete the Title column, but the following PowerShell script hides it from view in the View and Edit Item pages and also the List Settings page. Note: You will still need to remove it from all list views.

#Get web object
$web = Get-SPWeb -identity "http://portal/testsite"

#Get list and Title column
$list = $web.Lists["TestList"]
$titleColumn = $list.Fields["Title"]

#Set Title to optional and hidden
$titleColumn.Required = $false
$titleColumn.Hidden = $true

#Update Title column and list
$titleColumn.Update()
$list.Update()

#Dispose of Web object
$web.Dispose()

Windows PowerShell ISE

I know there are a few PowerShell GUI interfaces available out there, but there is actually one included with Windows Server 2008 R2. It's called Windows PowerShell Integrated Scripting Environment (ISE) and you can load it as a feature from Server Manager. It includes the following features:

  • Command prompt pane similar to the one provided with the standard PowerShell application
  • Output pane showing the results of commands and scripts executed
  • Scripting pane allowing you to create *.ps1 PowerShell script files. Commands can be copied and pasted into here from the other two panes
  • Script debugging with breakpoints
  • Script and individual line execution from the toolbar

ISE

To enable Windows PowerShell ISE for use with SharePoint, the only thing you will need to do is make sure the SharePoint PowerShell add-ons are loaded. The following steps create a Windows PowerShell ISE profile on the machine for the local user (taken from http://www.harbar.net/archive/2010/05/03/adding-sharepoint-2010-poweshell-cmdlets-to-your-powershell-ise.aspx):

  • Install Windows PowerShell ISE from Server Manager and launch it from the Accessories > Windows PowerShell folder on the start menu
  • Copy or type the following text in the Command Pane (View > Go to Command Pane) and press ENTER:

# Creates a local user PowerShell ISE profile
if (!(test-path $profile ))
{new-item -type file -path $profile -force}
# opens it for edit
psEdit $profile

  • Running these commands will open a new script file in the Script Pane. Type the following commands in the script file and save it by clicking the Save button on the toolbar:

# Copy the following into the new file and save it
cd 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration'
.\SharePoint.ps1
cd \

From now on, the SharePoint cmdlets will automatically load every time you start Windows PowerShell ISE as the same user. If you are still having problems using it, you may need to run it as an Administrator by right-clicking the shortcut and selecting Run as administrator (you can also force it to run as an administrator every time by right-clicking the shortcut, selecting Properties and then the Advanced button).

Welcome!

Welcome to Get-SPScripts : the PowerShell Scripting for SharePoint blog. The aim of this site is to provide as much info and sample scripts on using PowerShell to manage and configure SharePoint 2010 – really as I learn it myself.

I have been working as an IT Pro with SharePoint since WSS 2.0 / SharePoint Portal Server 2003. Over the years I have got into writing C# code to perform various automation, configuration and management tasks in SharePoint, but whilst I haven’t been afraid to get into Visual Studio when required, I would always prefer not to if it can be done out-of-the-box first.

Well, PowerShell bridges that gap. Most of us IT Pro’s will hate it at first, but once you get your head around how to use it – and more importantly what you can do with it – you will never want to revert back to STSADM again! I’m not going to get into how to use PowerShell from the ground up (download this or this if you want to learn that) – instead I am going to provide some sample scripts that I have used myself or found on other sites which you will be able to modify and use yourselves.

Hopefully I can provide a resource to help you save lots of time and effort in managing your own SharePoint environments. Please feel free to add comments or suggestions and let me know if there are scripts you would like to see or if you have written any yourself and would like to add them to the site.