Overview
PowerShell is the de facto standard for SharePoint automation and scripting nowadays. But there are several competitors for the .NET platform, one of them is CS-Script.
According to the official site information:
CS-Script is a CLR (Common Language Runtime) based scripting system which uses ECMA-compliant C# as a programming language. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono.
CS-Script is an open-source initiative that is distributed under the license agreement, which can be found here. However commercial support is also available.
CS-Script combines the power and richness of C# and FCL with the flexibility of a scripting system. CS-Script can be useful for system and network administrators, developers and testers. For any one who needs an automation for solving variety of programming tasks.
CS-Script could be used for solving variety of programming tasks such as:
- analysing and adjusting system configuration
- extending functionality of a software system with flexible scripting
- configuring development or testing environment
- automating software batch build
- automating testing, and collecting test results
CS-Script In Action
So, lets take a look at different examples of using Script C# with SharePoint and get started with the classic example of printing the greeting.
Example 1. Hello World
//css_host /version:v3.5 /platform:x64; | |
//css_dir C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI; | |
using System; | |
using Microsoft.SharePoint; | |
namespace CSScript4SP | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var site = new SPSite("http://intranet.contoso.com")) | |
{ | |
using (var web = site.OpenWeb()) | |
{ | |
Console.WriteLine("Hello SP!"); | |
} | |
} | |
} | |
} | |
} |
Running it via command-line using CS-Script engine executable (eg. csc.exe) gives us the the following results
Points to note:
Since it is running under SharePoint 2010, the following directives should be specified:
- //css_host [/version:<CLR_Version>] [/platform:<CPU>]
– used to force the script to be compiled and executed against legacy CLR and alternative CPU architecture on Windows x64 - //css_dir <path> – include assemblies from SharePoint directory into script and assembly probing
Example 2. Reference another C# script
In this example is demonstrated how to reference another C# script
//css_host /version:v3.5 /platform:x64; | |
//css_dir C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI; | |
//css_import WebPartManagerExtensions; | |
using System; | |
using Microsoft.SharePoint; | |
using Microsoft.SharePoint.WebPartPages; | |
namespace CSScript4SP | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var site = new SPSite("http://intranet.contoso.com")) | |
{ | |
using (var web = site.OpenWeb()) | |
{ | |
web.ForEachWebPartOnPage<Microsoft.SharePoint.WebPartPages.WebPart>("/en/Pages/default.aspx",PrintWebPartInfo); | |
} | |
} | |
} | |
private static void PrintWebPartInfo(SPWeb web, Microsoft.SharePoint.WebPartPages.WebPart webPart, SPLimitedWebPartManager wpm) | |
{ | |
Console.WriteLine("{0} {1} {2} {3}", webPart.Title, webPart.GetType().Name, webPart.ZoneIndex, webPart.ZoneID); | |
} | |
} | |
} |
Points to note:
- //css_import<file> [, preserve_main][, rename_namespace(<oldName>, <newName>)] – reference the method ‘ForEachWebPartOnPage’ of a WebPartManagerExtensions.cs
Example 3. Reference library
In this example is demonstrated how to expose classes in an assembly CustomSharePointLibrary.dll
Points to note:
//css_reference <file> – load the assembly at execution time
Hi Vadim,
Nice post ,thanks for sharing this article.
Sharepoint Developers
A bit over my head but you made it possible for me to follow along the steps. Great info and a timely post for me. Thanks!
John.