Use Windows PowerShell with SharePoint Object Model

What is Windows PowerShell?

From Windows PowerShell Frequently Asked Questions,
Windows PowerShell is a new command line shell and task-based scripting technology that provides information technology (IT) administrators comprehensive control and automation of system administration tasks, increasing administrator productivity. Windows PowerShell includes numerous system administration utilities, consistent syntax and naming conventions, and improved navigation of common management data such as the registry, certificate store, or Windows Management Instrumentation (WMI). Windows PowerShell also includes an intuitive scripting language specifically designed for IT administration.

Windows PowerShell (PowerShell) can be used as command-line tools and as scripting tools. With PowerShell scripts, tasks such as creating top navigation are repeatable and predictable. They can help moving top navigation from development, to test and to production servers.

PowerShell can reflect into .Net objects e.g. SharePoint Object Model.

What is SharePoint Object Model

SharePoint object model exposes most classes, properties, and methods used to create WSS sites. Anything you can do using the application and administrative interfaces within SharePoint can be done using the object model. The SharePoint object model is written entirely in .NET managed code using C#. This means that the object model is easily accessible via .NET enabled tool. PowerShell is a such tool.

Examples

The follow script create top navigation on WSS site.

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
$url = "http://localhost:29688"
$topsite = new-object Microsoft.SharePoint.SPSite $url;
$topweb = $topsite.OpenWeb()
$topnavs = $topweb.Navigation.TopNavigationBar

$node0 = $topnavs[0]
while ($node0.Children.Count -gt 0) { $node0.Children.Delete($node0.Children[0])}

$node1 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Solutions", "/solutions", $False)
$node1 = $node0.Children.AddAsLast($node1)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Overview", "", $False)
$node2 = $node1.Children.AddAsLast($node2)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Web Solutions", "", $False)
$node2 = $node1.Children.AddAsLast($node2)
$node3 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("ASP.NET Solutions", "", $False)
$node3 = $node2.Children.AddAsLast($node3)
$node3 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("SharePoint Solutions", "", $False)
$node3 = $node2.Children.AddAsLast($node3)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Smart Client Solutions", "", $False)
$node2 = $node1.Children.AddAsLast($node2)

$node1 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Products and Services", "/products_services", $False)
$node1 = $node0.Children.AddAsLast($node1)

$node1 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("", "/customers", $False)
$node1 = $node0.Children.AddAsLast($node1)

$node1 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Parterns", "/parterns", $False)
$node1 = $node0.Children.AddAsLast($node1)

$node1 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Company", "/company", $False)
$node1 = $node0.Children.AddAsLast($node1)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Our Vision", "", $False)
$node2 = $node1.Children.AddAsLast($node2)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("News", "", $False)
$node2 = $node1.Children.AddAsLast($node2)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Events", "", $False)
$node2 = $node1.Children.AddAsLast($node2)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Careers", "", $False)
$node2 = $node1.Children.AddAsLast($node2)
$node2 = new-object Microsoft.SharePoint.Navigation.SPNavigationNode -argumentlist @("Contact Us", "", $False)
$node2 = $node1.Children.AddAsLast($node2)

28377

If to write the above script, it can be tedious and error-prone. So PowerShell code generator comes to rescue.

Last edited Feb 24, 2008 at 11:50 PM by yysun, version 3

Comments

No comments yet.