EDIT: THIS PROJECT IS CONTINUED ON CODEPLEX!
Due to the success of this post I continued this as a project on CodePlex: https://sharepointpowershell.codeplex.com
But if you still want to continue reading this post, go ahead.
How it works
Download this file and extract the DLL to an easy location to reach from PowerShell. Because this assembly uses the Client Object Model you need to install the COM Redistributable as well.
These are the PowerShell commands to run:
You need to run PowerShell in single-threaded mode (the authentication requires it).
[code gutter=”false” lang=”powershell”]powershell -STA[/code]
Import the DLL.
[code gutter=”false” lang=”powershell”][Reflection.Assembly]::LoadFile("D:\ClaimsAuth.dll")[/code]
Instantiate a new SPOContext providing your SharePoint Online site URL. (Don’t forget the https)
[code gutter=”false” lang=”powershell”]$spoContext = new-object SPOContext("https://jeffreypaarhuis.sharepoint.com")[/code]
Now, let’s test it:
[code lang=”powershell”]$spoContext = new-object SPOContext("https://jeffreypaarhuis.sharepoint.com")
$web = $spoContext.Web
$spoContext.Load($web)
$spoContext.ExecuteQuery()
Write-Host $web.Title[/code]
As you already might understand, this solution is based on the SharePoint Client Object Model. This means that everything that’s possible with the COM is possible to script. This post explains how to work with Sites, Permissions, Documents, etc. with the COM. It is written in C# but it’s fairly easy to translate to PowerShell.
Samples
[code lang=”powershell” gutter=”false”]
——————-
Init spoContext
——————-
powershell -sta
[Reflection.Assembly]::LoadFile("D:\ClaimsAuth.dll")
$spoContext = New-Object SPOContext("https://mysharepointonline.sharepoint.com")
——————-
Print sitename
——————-
$web = $spoContext.Web
$spoContext.Load($web)
$spoContext.ExecuteQuery()
$web.Title
——————-
Add property to bag
——————-
$web.AllProperties.FieldValues.Add("propA","Property A")
$spoContext.ExecuteQuery()
$web.AllProperties.Item("propA")
——————-
Show features
——————-
$site = $spoContext.Site
$spoContext.Load($site)
$spoContext.ExecuteQuery()
$features = $site.Features
$spoContext.Load($features)
$spoContext.ExecuteQuery()
$features
——————-
Permissions stuff
——————-
Function GetWeb
{
$ctx.ExecuteQuery()
Return $ctx.Web
}
Function GetList ($name)
{
$web = GetWeb
if ($web -ne $null)
{
$lists = $web.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()
$list = $lists | where {$_.Title -eq $name}
return $list
}
return $null
}
Function GetRole ($rType)
{
$web = GetWeb
if ($web -ne $null)
{
$roleDefs = $web.RoleDefinitions
$ctx.Load($roleDefs)
$ctx.ExecuteQuery()
$roleDef = $roleDefs | where {$_.RoleTypeKind -eq $rType}
return $roleDef
}
return $null
}
Function GetPrincipal ($name)
{
$web = GetWeb
if ($web -ne $null)
{
$principal = $web.EnsureUser($name)
$ctx.Load($principal)
$ctx.ExecuteQuery()
return $principal
}
return $null
}
Function GetGroup ($name)
{
$web = GetWeb
if ($web -ne $null)
{
$groups = $web.SiteGroups
$ctx.Load($groups)
$ctx.ExecuteQuery()
$group = $groups | where {$_.Title -eq $name}
return $group
}
return $null
}
Function GetDocumentLibrary ($name)
{
$web = GetWeb
if ($web -ne $null)
{
$docLibs = $web.Lists
$ctx.Load($docLibs)
$ctx.ExecuteQuery()
$docLib = $docLibs | where {$_.Title -eq $name}
return $docLib
}
return $null
}
$web = GetWeb
$web.BreakRoleInheritance($true, $false);
$principal = GetGroup "MyGroup"
$roleType = [Microsoft.SharePoint.Client.RoleType]"Contributor"
$role = GetRole $roleType
$collRdb = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
$collRdb.Add($role)
$collRoleAssign = $web.RoleAssignments
$collRoleAssign.Add($principal, $collRdb)
$ctx.ExecuteQuery()
$list = GetList "Shared Documents"
$list.BreakRoleInheritance($false, $false);
$roleType = [Microsoft.SharePoint.Client.RoleType]"Reader"
$role = GetRole $roleType
$collRdb = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
$collRdb.Add($role)
$collRoleAssign = $list.RoleAssignments
$collRoleAssign.Add($principal, $collRdb)
$ctx.ExecuteQuery()
[/code]
Errors?
You might see the following errors from time to time, which aren’t a big deal:
– “The requested site does not appear to have claims enabled or the Login Url has not been set.”.
Problem: This usually means that no session can be instantiated (SharePoint Online bug).
Solution: Navigate to your site in Internet Explorer, when your site doesn’t show, refresh it a few times until it shows, or go to https://portal.microsoftonline.com. When you get a login page, go back to your script.
– “The remote name could not be resolved: ‘mysp.sharepoint.com'”
Problem: During scripting, the context timed out.
Solution: Re-instantiate the $spoContext object. When running a script in one go, this error shouldn’t pop because the Context will not prematurely expire.
Under the hood
How does it work under the hood? What’s in the DLL? What makes SharePoint Online so difficult?
In line with this one I’ve written a post that explains what the problem with SharePoint Online is and how to build your own DLL. Read it here.
UPDATE (11-7-2012):
Forgot to mention you need to install the COM Redistributable.
UPDATE (3-9-2012):
Added samples.