Download presentation
Presentation is loading. Please wait.
Published bySara Terry Modified over 8 years ago
1
Powering up your Office 365 cmdlets with CSOM Bjoern H Rapp, Senior Architect Public Cloud, SopraSteria
2
@bjoern_rappwww.sharepointviking.com
3
Introduction CSOM and Powershell Office Dev PnP Cmdlets AGENDA
4
4
5
The Office 365 PowerShell Landscape http://bit.ly/1BOIaX2 http://bit.ly/1JQYN7g http://bit.ly/1ZSOYdZ
6
SharePoint Online PowerShell Module Format: Noun-part starts with «SPO» (Add-SPOUser, Get-SPOSite) Requires Global Admin privileges Management of users, sites and organizations http://bit.ly/1K3mC7w http://bit.ly/1K3mC7w 41 cmdlets 6
8
Site Groups Create a Site Group Remove a Site Group Set a Site Group Users Add a User to a group Get a User Set a User Remove a User from a group Sites Create a SC Delete a SC Repair a SC Remove a SC Restore a deleted SC Add-Ins Get Add-In Errors Get Add-In Information Misc. Connect and disconnect Migration Jobs Set sync client properties
9
Example 1: Connect to SP Online Service
10
Import-Module Microsoft.Online.Sharepoint.PowerShell $credential = get-credential Connect-SPOService -url https://contoso-admin.sharepoint.com - Credential $credential #Create site collection New-SPOSite –Url https://contoso-admin.sharepoint.com/sites/sitename -Owner user@contoso.com –StorageQuota 1024 –Title «Site Name» –CompatibilityLevel 15 –LocaleID 1033 –ResourceQuota 300 –Template «STS#0» - NoWaithttps://contoso-admin.sharepoint.com/sites/sitenameuser@contoso.com Example 2: Create a Site Collection
11
Import-Module Microsoft.Online.Sharepoint.PowerShell $credential = get-credential Connect-SPOService -url https://contoso-admin.sharepoint.com - Credential $credential #Delete site collection Remove-SPOSite –Identity https://contoso- admin.sharepoint.com/SiteColName - NoWaithttps://contoso- admin.sharepoint.com/SiteColName Example 3: Remove a Site Collection
12
Import-Module Microsoft.Online.Sharepoint.PowerShell $credential = get-credential Connect-SPOService -url https://contoso-admin.sharepoint.com - Credential $credential #Import content from csv and create a site collection per row Import-Csv.\NewSPOSites.csv | % {New-SPOSite –Owner $_.Owner – StorageQuota $_.StorageQuota –Url $_.Url -NoWait –ResourceQuota $_.ResourceQuota –Template $_.Template –Title $_.Name Example 4: Create multiple sites by importing from.csv file
13
Other samples at: http://powershell.office.com/script- samples/
14
DEMO Using SharePoint Online Management PowerShell
15
15
16
Client-Side Object Model An API for running SharePoint code against the object model directly from a client Commands are packed as xml’s and sent in batches to SharePoint What is CSOM? JavaScript LogicManaged Logic Client.svc JSOM Managed OM Proxy Server OM DB XML Request JSON Response
17
SPO PowerShell too limited for Site Collection- level operations and below (700+ cmdlets in SP On Prem PowerShell module vs 41 in SPO ) Need for tools to perform repetitive tasks on objects in SPO (Webs, Lists, Linraries, Items, Documents). Cannot use SPO PowerShell for scripting Managed Metadata, Search, User Profiles etc. Why using CSOM with PowerShell?
18
Scripts can be written with any editor, no need to recompile using Visual Studio Can use PowerShell ISE or PowerGUI for debugging Leverage PowerShell’s strength our CSOM scripts, like reading XML, using other modules Can only perform operations where the method exists Some unusual constructs (heavy use of new-object and [ ]:: notation to reference enums and static methods CSOM with PowerShell pro’s and con’s
19
Preparing your environment
20
Simple CSOM Managed Code // Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. ClientContext context = new ClientContext("http://SiteUrl"); // The SharePoint web at the URL. Web web = context.Web; // We want to retrieve the web's properties. context.Load(web); // Execute the query to the server. context.ExecuteQuery(); //Display web properties, such as title. label1.Text = web.Title
21
Simple CSOM using PowerShell Import-Module $site = ‘http://SiteUrl’ $adm =‘ $pwd = Read-Host ‘Enter Password’ - AsSecureString #Credentials $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adm,$pwd) #Starting with ClientContext, the constructor requires a URL to the // server running SharePoint. $context = New-Object Microsoft.SharePoint.Client.ClientContext("http://SiteUrl") $context.Credentials = $credentials #We want to retrieve the web's properties. $web = $context.web $context.Load($web) #Execute the query to the server. $context.ExecuteQuery(); #Display web properties, such as title. Write-Host $web.Title
22
Creating a list Import-Module $site = ‘http://SiteUrl’ $adm =‘ $pwd = Read-Host ‘Enter Password’ –AsSecureString $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adm,$pwd) $context = New-Object Microsoft.SharePoint.Client.ClientContext("http://SiteUrl") $context.Credentials = $credentials $web = $context.web $creationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation $creationInfo.Title = "Announcements" $creationInfo.TemplateType = [int][Microsoft.SharePoint.Client.ListTemplateType]::Announcements $list = $web.Lists.Add($creationInfo) $list.Description = "An announcement list made with CSOM" $list.Update() $context.ExecuteQuery()
23
DEMO Using CSOM with PowerShell
24
24
25
Office365 Dev Patterns and Practices
26
Getting the tools https://github.com/OfficeDev/PnP-PowerShell https://github.com/OfficeDev/PnP-PowerShell 2 editions: PnPPowerShellCommands15.msi (SP 2013 On-Premises) PnPPowerShellCommands16.msi (SharePoint Online) Run the installer, and click Finish.
28
SPO PowerShell works well as a supplement to the Administration Console for Global Admin tasks in SharePoint Online SPO PowerShell cmdlets too limited for scripting tasks like Taxonomy, User Profiles, site, list and library operations PowerShell + CSOM gives you all tools you need but require a certain level of programming knowledge OfficeDev-PnP PowerShell modules look promising, but still very new and buggy from time to time. Will probably mature into a good toolset for both devs and it-pros. Summary
29
Related Sessions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.