Client-side SharePoint PowerShell CSOM Read list

PowerShell CSOM

SharePoint PowerShell CSOM API provides the capability to connect to SharePoint 2010, 2013 and Online. The major advantage is that these can be run from a remote machine. The API is available in CodePlex.
 I will focus on how to use the API and read a SharePoint list in this article. First we need to download the source from CodePlex and extract it to a directory with necessary permissions. I am using ‘C:SPPSSource’ as the location for extraction.
Next we need to get a client context as in a normal CSOM.

Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password){
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$userCredentials=New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)
Initialize-SPPS -siteURL http://www.dfd.dk/ -UserCredential $userCredentials
$context = [Microsoft.SharePoint.Client.ClientContext](New-Object Microsoft.SharePoint.Client.ClientContext($Url))
$context.Credentials = $userCredentials
    return $context
}
Then we can use the following function to read the list.
Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $Context.Web.Lists.GetByTitle($listTitle)
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = ”
    $items = $list.GetItems($camlQuery)
    $Context.Load($items)
    $Context.ExecuteQuery()
    return $items
}
At last, somewhere from the necessary place, these few commands can be used to read the lists.
Import-Module ‘C:SPPSSourcespps.psm1’
$context = Get-SPOContext -Url ‘<Site Url>’ -UserName ‘<Username>’ -Password ‘<Password>’
$listItems = Get-ListItems -Context $context -ListTitle ‘<List Title>’
You can even schedule a Windows Scheduler and make this a schedule job which is very handy as it can be used even with SharePoint Online.

About the Author:
Malin is a SharePoint Specialist at Exilesoft and has been working on SharePoint since SharePoint 2007. He has been involved in many custom development projects, intranets, search, and other SharePoint projects. Malin is a regular presenter in many forums, including Sri Lanka .NET Forum and SharePoint Sri Lanka. Malin also presented in the Share-The-Point Conference Southeast Asia and at ESPC15 in Stockholm.
Share this on...

Rate this Post:

Share: