2010-11-19

SharePoint Client Model from Powershell

I have seen a few people attempt to use the client access model from powershell, but they have mostly failed. The reason being, powershell does not allow extension methods. You can get most of the way, but when you try to call Load() or ExecuteQuery() it fails. I really like being able to do things in powershell scripts, as they are super easy to edit/change when you need. Surely i could get the client model to work from scripts.

I knew that powershell allows you compile and execute c# inline, hopefully i could use that feature. It was pretty easy to throw together a test, as it was just c# i was using.

The next problem i encountered; was powershell not loading the client access assemblies. No matter what i tried, i could only get powershell to load them IF the dlls were in the same directory as the powershell executable. Well for me that is not an ideal solution, i like to be able to copy a directory somewhere, and it just works.

What i found was a way you can override the powershell assembly loading
http://stackoverflow.com/questions/2664028/how-can-i-get-powershell-added-types-to-use-added-types

Then it was just a matter of intergrating it with the rest of the script.

Add-Type -Language CSharpVersion3 -TypeDefinition @' 
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace Internal.Powershell {
public static class AssemblyResolver {
private static Dictionary<string, string> _assemblies = new Dictionary<string,string>(StringComparer.CurrentCultureIgnoreCase);
static AssemblyResolver() {
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender, ResolveEventArgs args) {
var assemblyName = new AssemblyName(args.Name);
if (_assemblies.ContainsKey(assemblyName.Name)) {
return Assembly.LoadFrom(_assemblies[assemblyName.Name]);
}
return null;
});
}
public static void AddAssemblyLocation(string path) {
_assemblies.Add(Path.GetFileNameWithoutExtension(path), path);
}
}
}
'@
[Internal.Powershell.AssemblyResolver]::AddAssemblyLocation("..\..\lib\Microsoft.SharePoint.Client.Runtime.dll")
[Internal.Powershell.AssemblyResolver]::AddAssemblyLocation("..\..\lib\Microsoft.SharePoint.Client.dll")

$ReferencedAssemblies = @(
"Microsoft.SharePoint.Client.Runtime",
"Microsoft.SharePoint.Client"
)
$TypeDefinition = @"
using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.IO;
using System.Text;
using Microsoft.SharePoint.Client;
namespace Internal.Powershell {
public static class SharePointClient {
public static void Main() {
using (ClientContext client = new ClientContext("http://url")) {
client.AuthenticationMode = ClientAuthenticationMode.Default;
client.Credentials = new NetworkCredential("username", "password", "domain");
var web = client.Web;
client.Load(web,
item => item.Title,
item => item.Description);
client.ExecuteQuery();
Console.WriteLine(web.Title);
Console.WriteLine(web.Description);
}
}
}
}
"@
Add-Type -ReferencedAssemblies $ReferencedAssemblies -TypeDefinition $TypeDefinition -Language CSharpVersion3
[Internal.Powershell.SharePointClient]::Main()

5 comments:

Anonymous said...

Спасибо понравилось !

oakleyses said...

tory burch outlet, polo ralph lauren, ugg boots, chanel handbags, tiffany jewelry, longchamp outlet, longchamp pas cher, gucci handbags, sac longchamp pas cher, uggs on sale, jordan pas cher, michael kors pas cher, ray ban sunglasses, oakley sunglasses, polo outlet, louboutin pas cher, longchamp outlet, burberry pas cher, louis vuitton, cheap oakley sunglasses, replica watches, replica watches, longchamp outlet, christian louboutin outlet, prada handbags, oakley sunglasses wholesale, louis vuitton outlet, oakley sunglasses, ray ban sunglasses, louis vuitton, prada outlet, nike outlet, louis vuitton outlet, christian louboutin, nike free run, nike roshe, ugg boots, nike air max, polo ralph lauren outlet online, kate spade outlet, christian louboutin uk, tiffany and co, nike free, oakley sunglasses, jordan shoes, louis vuitton outlet, nike air max, ray ban sunglasses

oakleyses said...

nike tn, ralph lauren uk, true religion outlet, nike air max uk, burberry handbags, michael kors, abercrombie and fitch uk, nike blazer pas cher, oakley pas cher, timberland pas cher, hogan outlet, uggs outlet, michael kors outlet online, replica handbags, sac vanessa bruno, true religion outlet, converse pas cher, guess pas cher, ray ban pas cher, ray ban uk, true religion outlet, michael kors outlet, mulberry uk, nike roshe run uk, hollister uk, coach outlet, kate spade, nike air max, new balance, true religion jeans, north face, coach outlet store online, uggs outlet, michael kors outlet online, polo lacoste, michael kors outlet, lululemon canada, nike air force, vans pas cher, michael kors outlet, hollister pas cher, michael kors outlet online, sac hermes, michael kors, burberry outlet, michael kors outlet online, north face uk, nike air max uk

oakleyses said...

converse, louboutin, soccer jerseys, ferragamo shoes, hermes belt, longchamp uk, p90x workout, north face outlet, baseball bats, instyler, nike trainers uk, babyliss, mont blanc pens, soccer shoes, nfl jerseys, valentino shoes, oakley, hollister clothing, abercrombie and fitch, chi flat iron, ralph lauren, nike air max, north face outlet, gucci, ray ban, vans, beats by dre, herve leger, jimmy choo outlet, giuseppe zanotti outlet, mac cosmetics, ghd hair, reebok outlet, nike roshe run, hollister, mcm handbags, timberland boots, nike air max, lululemon, bottega veneta, insanity workout, nike huaraches, vans outlet, hollister, iphone cases, wedding dresses, new balance shoes, converse outlet, asics running shoes, celine handbags

oakleyses said...

marc jacobs, ugg,uggs,uggs canada, louis vuitton, swarovski, barbour uk, montre pas cher, moncler outlet, louis vuitton, canada goose outlet, louis vuitton, pandora charms, wedding dresses, juicy couture outlet, moncler, canada goose outlet, louis vuitton, canada goose uk, ugg uk, canada goose, ugg pas cher, coach outlet, hollister, moncler, canada goose outlet, ugg, pandora jewelry, moncler outlet, karen millen uk, pandora jewelry, links of london, louis vuitton, canada goose, canada goose, swarovski crystal, moncler uk, moncler, moncler, doudoune moncler, juicy couture outlet, canada goose jackets, thomas sabo, replica watches, ugg,ugg australia,ugg italia, barbour, pandora uk, supra shoes, lancel, toms shoes