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
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
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