Categories
Configuring Developing Good to know SharePoint Talk

Automated deployment on SharePoint Online and SharePoint 2013 using PowerShell and CSOM

Automated deployment for SharePoint Online can be one hell of a job. Apps and sandboxed solutions are the designated methods for provisioning, but are very limited. How about PowerShell? Yes! But not the default flimsy SPO cmdlets library. No, we are using SharePoint PowerShell, a third party PowerShell API for SharePoint (Online and On-premise).

SharePoint PowerShell

SharePoint PowerShell in fact a bunch of PowerShell Modules talking to CSOM, created by a guy called Jeffrey Paarhuis 🙂  Very usefull for Office 365 and private clouds where you don’t have access to the physical server.

What can you do with it?

  • Site Collection
    • Test connection
  • Site
    • Manage subsites
    • Manage permissions
  • Lists and Document Libraries
    • Create list and document library
    • Manage fields
    • Manage list and list item permissions
    • Upload files to document library (including folders)
    • Add items to a list with CSV
    • Add and remove list items
    • File check-in and check-out
  • Master pages
    • Set system master page
    • Set custom master page
  • Features
    • Activate features
  • Web Parts
    • Add Web Parts to page
  • Users and Groups
    • Set site permissions
    • Set list permissions
    • Set document permissions
    • Create SharePoint groups
    • Add users and groups to SharePoint groups
  • Solutions
    • Upload sandboxed solutions
    • Activate sandboxed solutions

And yet more to come…

How does it work? Well, like this:

[code lang=”powershell”]

# Include SPPS
Import-Module .\spps.psm1

# Setup SPPS
Initialize-SPPS -siteURL "https://example.sharepoint.com/" -online $true -username "sitecollectionadmin@example.onmicrosoft.com" -password "password"

# Activate Publishing Site Feature
Activate-Feature -featureId "f6924d36-2fa8-4f0b-b16d-06b7250180fa" -force $false -featureDefinitionScope "Site"

#Activate Publishing Web Feature
Activate-Feature -featureId "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" -force $false -featureDefinitionScope "Web"

# Create a subsite
Add-Subsite -title "Subsite" -webTemplate "STS#0" -description "Description…" -url "subsite" -language 1033 -useSamePermissionsAsParentSite $true

# Create document library
Add-DocumentLibrary -listTitle "Testdoclib"

# Copy testfiles to this document library
Copy-Folder "C:\example\Testdoclib" "Testdoclib" $false
[/code]

Try it yourself: example application 

And let me know what you think of it and how it can be improved!

13 replies on “Automated deployment on SharePoint Online and SharePoint 2013 using PowerShell and CSOM”

Great initiative. I was just looking for CSOM or Rest Api option to automate our SharePoint Online environment. I also left a feature request at the codeplex page

I’m sorry but at this moment I’m not able to help you in translating it into a working piece of PowerShell. Maybe there is a programmer at your fingertips that’s able to do this? Otherwise I might do it later on.

var list = ctx.Web.Lists.GetByTitle(listName);
var folder = list.RootFolder;
ctx.Load(folder);
ctx.ExecuteQuery();
folder = folder.Folders.Add(folderName);
ctx.ExecuteQuery();

In PowerShell this becomes:
$list = $ctx.web.lists.getbytitle($listname)
$folder = $list.RootFolder
$ctx.Load($folder)
$ctx.ExecuteQuery();
$folder = $folder.Folders.Add($folderName);
$ctx.ExecuteQuery();

And yep I know this is late, however it may aid someone else 🙂

Hi,
I am not able to deploy WSP file(sandboxed solutions) in solution gallery of SharePoint online (16 version)365 site of SharePoint 2013.

Please do some needful.

Leave a Reply to Ronald van Herk Cancel reply