SharePoint PowerShell 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.
First we need to download the source from CodePlex and extract it to a directory with necessary permissions. I am using 'C:\SPPS\Source' 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.sitename.com/ -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:\SPPS\Source\spps.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.
No comments:
Post a Comment