Sharepoint Online and Powershell

Lately I've been solving a lot of problems in Sharepoint using Powershell. Previously I had reached reasonable solutions with SPD workflows. Now I'm revisiting my design and stepping up the performance and efficiency. Here's a quick run down, and some samples of my findings.

Setup

First you'll need to install SharePoint 2013 Client Components redistributable package from http://www.microsoft.com/en-us/download/details.aspx?id=35585.  This will give you all of the DLL files in c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\ that you'll need.  You can also grab these DLLs directly from a Sharepoint 2013 server.

Start Scripting

First you'll need to load your DLLs with these lines:


Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Depending on what you're needing to do, you may need to add more. For example, if you're working with Workflows add this line too:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

Then you need to connect to your Sharepoint Instance:

$SiteUrl = "https://tenant.sharepoint.com"  
$ListName = "Big List"
#$SPpassword = Read-Host -Prompt "Enter password" -AsSecureString
$SPpassword = ConvertTo-SecureString -AsPlainText "password123" -Force
$SPusername = "scriptaccount@tenant.com"

# Connect to the site
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPUserName, $SPpassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()

Note the two $SPpassword lines, the commented out one will prompt for your password each time. Nice for security because it isn't being saved in a text file but it doesn't allow for scripting.

Now you can work your magic:

This is where things get more difficult, you can now go in any direction depending on what you'd like to do.  The most common thing I want to do is iterate through a lists' items:




Comments

Popular posts from this blog

Sync iTunes with MythMusic

Using Homebridge and Broadlink RM Mini to automate

LetsEncrypt and AWS ELB Load Balancers