Wednesday, July 30, 2008

Powershell Scripting for SharePoint

C# has long been the developers preference when working with Microsoft SharePoint Products and Technologies; however, since the release of Microsoft Office SharePoint Server 2007/Windows SharePoint Server 3.0 Powershell has become more widely used to support the automation of common administrative tasks - though C# is and will long be integral in application development, Powershell provides both simplicity and flexibility for the automation of routine tasks.

For example, let's assume an administrator would like to programmatically provision a Web application outside of the SharePoint administration tool and/or the SharePoint 3.0 Central Administration user interface.

In this example (C#) you can reference the Microsoft.SharePoint.Administration Namespace and call the SPWebApplicationBuilder class to create a new SPWebApplication object.

static void Main(string[] args)
{
SPWebApplicationBuilder pWebApp = new SPWebApplicationBuilder(SPFarm.Local);

int iPort = 80;
pWebApp.Port = iPort;

SPWebApplication WebApplication = pWebApp.Create();

WebApplication.Provision();

SPSite SiteCollection = WebApplication.Sites.Add("/", "company\\someone", "someone@company.com");

SiteCollection.Close();
}
Using Powershell, the underlying concept remains the same with reduction in overall code:
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")

$webAppBuilder=new-object Microsoft.SharePoint.Administration.SPWebApplicationBuilder( [Microsoft.SharePoint.Administration.SPFarm]::Local)

$webAppBuilder.Port=80

$webApp=$webAppBuilder.Create()

$webApp.Provision()

$webApp.Sites.Add("/","company\someone",someone@company.com)

0 comments: