Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
CSOM SharePoint PowerShell Demo
Introduction
- CSOM is an acronym for Client Side Object Model
- CSOM provides a subset of the Server Object Model
- CSOM Supports remote execution via JavaScript and .NET
Download Links
- SharePoint 2010 CSOM - http://www.microsoft.com/en-us/download/details.aspx?id=21786%20
- SharePoint 2013 CSOM - http://www.microsoft.com/en-us/download/details.aspx?id=35585
- SharePoint Online CSOM - http://www.microsoft.com/en-us/download/details.aspx?id=42038
Loading DLL's
Once the required Client Components are installed we can import the required DLL and use in our PowerShell Script.
Note: In this Demo, we are using SharePoint Online Client Components.
#Method 1
Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' |
#Method 2
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' |
How to Use?
- Declare the Variable
- Load the context
- Execute the Query
- Use the declared variable as used in Step 1
Let's dive into the demo.
Update SharePoint List Description
- Created a New List 'PowerShell CSOM'
- Added one sample item in the list
- Left Description Intentionally blank
#Import the required DLL Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' #OR Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' #Mysite URL $site = 'https://Domain.sharepoint.com/' #Admin User Principal Name $admin = '[email protected]' #Get Password as secure String $password = Read-Host 'Enter Password' -AsSecureString #Get the Client Context and Bind the Site Collection $context = New-Object Microsoft.SharePoint.Client.ClientContext($site) #Authenticate $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password) $context.Credentials = $credentials $list = $context.Web.Lists.GetByTitle('PowerShell CSOM') $context.Load($list) $list.Description = "CSOM PowerShell - Did it!!!" $list.Update() $context.ExecuteQuery() |
Explore the Context
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site) $context | GM |
The above code shows all the methods and properties - Go Object ! Now we got to know the information to trigger some methods to meet client requirements.
Update SharePoint Online List Item
#Common Mistakes #To add item to List $Columns = $context.Web.AvailableFields $Columns |
Error
An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.. At line:2 char:1 + $Columns + ~~~~~~~~ + CategoryInfo : InvalidOperation: (Microsoft.Share...t.Client.Field]:d__0) [], RuntimeException + FullyQualifiedErrorId : BadEnumeration
Oops - This is wrong. Because we missed to follow the How to Use? CSOM - PowerShell way of doing - Check the below Code:
#To add item to List $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation $Item = $List.AddItem($ListItemInfo) $Item["Title"] = "Ms" $Item.Update() $Context.ExecuteQuery() |
Summary
How can I find the Class Names?
Simply do the below and you will come to know the Class name
$list.AddItem |
Returns
OverloadDefinitions ------------------- Microsoft.SharePoint.Client.ListItem AddItem(Microsoft.SharePoint.Client.ListItemCreationInformation parameters) |