Office 365 – SharePoint Powershell – Read List


We have moved to SharePoint online which is under Office 365 platform. I will share some basic examples, with which I have started the journey to this new platform myself.

First, make sure you have downloaded the following items in your local machine:

1. SharePoint Client Components SDK (URL: https://www.microsoft.com/en-us/download/details.aspx?id=35585)
2. SharePoint Online Management Shell (URL: https://www.microsoft.com/en-in/download/details.aspx?id=35588)

Now, let us create a small script to connect to our Office 365 tenant using PowerShell:

#Specify tenant admin and site URL
$User = "yourusername@yoursite.onmicrosoft.com"
$SiteURL = "https://yoursite.sharepoint.com/sites/demosite"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
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"

$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

Ok, now, you have connected PowerShell to Office 365 SharePoint site. Let’s
see how to connect to a list and fetch the items from it.

$ListTitle = "Your List Title"

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($ListTitle)

#Get All items from the list
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())

$Context.Load($List)
$Context.Load($ListItems)
$Context.ExecuteQuery()

Yes, all your items are now available in $ListItems collection. All you have to do now is traverse through it and do whatever you like. Following code is given to display few columns from it.

#Showlist items count
Write-Host $List.ItemCount
Write-Host $ListItems.Count

#Loop through items
foreach($Item in $ListItems){
    $msg = [string]::Format("Title {0}, Company {1}, WorkCity {2}", $Item["Title"], $Item["Company"], $Item["WorkCity"])
    Write-Host $msg
}

Write-Host "End of Script"

Cheers!!

Note: If you want to see how to create a list, add fields to it from Site columns and update the default view, then you can check this beautiful article which has helped me to get started:

https://blogs.technet.microsoft.com/fromthefield/2014/02/18/office-365-powershell-script-to-create-a-list-add-fields-and-change-the-default-view-all-using-csom/

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s