I’ve been using Powershell for some time now to work with SharePoint on-premise. As many of you probably know Powershell is … well … very powerful when used for on-premise SharePoint, giving full access to the entire SharePoint API.
I’ve recently started doing more work with SharePoint in Office 365 (SharePoint Online), and discovered that there are also a suite of Powershell commands for SharePoint Online: http://technet.microsoft.com/en-us/library/fp161388.aspx. However, they are currently limited to administrative functions like adding a new site, new users, etc. And, of course you cannot use the native SharePoint API to interact with SharePoint Online.
What I want to be able to do from Powershell are things like add new list items to SharePoint Online lists, upload documents, etc. I want to be able to create sample sites for customers and add sample data. The answer (or one answer) is to use the SharePoint 2013 .NET Framework Client Object Model (CSOM). You can load the CSOM libraries in Powershell and away you go … you have access to the CSOM API to create list items, upload documents, etc. Here is an example that creates a new Announcement list item in a SharePoint Online site (credit to Paolo Pialorsi and his post “Using CSOM and Powershell against a SharePoint Online site” ):
# Load the SharePoint 2013 .NET Framework Client Object Model libraries. # 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" # Authenticate with the SharePoint Online site. # $siteUrl = “https://sharepoint_online_site_url” $password = Read-Host -Prompt "Enter password" -AsSecureString $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("sharepoint_online_username", $password) $ctx.Credentials = $credentials # Create list item. # $list = $ctx.get_web().get_lists().getByTitle('Announcements'); $itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation $listItem = $list.addItem($itemCreateInfo); $listItem.set_item('Title', 'My New Item!'); $listItem.set_item('Body', 'Hello World!'); $listItem.update(); $ctx.Load($listItem) $ctx.ExecuteQuery()