Create View in lists or libraries CSOM PowerShell Office 365 SharePoint 2013

I have posted articles to create a content type, list and add respective columns. Today I will be sharing code to create a view in lists/libraries and include required columns in the view using CSOM and PowerShell scripts.

New to Office 365 SharePoint Online? Get Office 365 Enterprise E3 Subscription & Try out all the features

Please find below Script and input XML file.
Script Code-


$0 = $MyInvocation.MyCommand.Definition
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$xmlFilePath = $(“$dp0\Inputs.xml”)
$xmldata = [xml](Get-Content($xmlFilePath));

$username = “”
$password = Read-Host -Prompt “Please enter your password” -AsSecureString

$url = $xmldata.WebSite.Url

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-ObjectMicrosoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Publishing.dll”
Add-Type -Path “C:\Lib\Microsoft.SharePoint.Client.Taxonomy.dll”

if (!$clientContext.ServerObjectIsNull.Value)
Write-Host “Connected to SharePoint Online site: ‘$Url'” -ForegroundColor Green

$web = $clientContext.Web


function createViews([Microsoft.SharePoint.Client.Web] $web)

foreach($view in $xmldata.WebSite.Views.View)
$pageList = $web.Lists.GetByTitle($view.List)


$viewFields = New-Object System.Collections.Specialized.StringCollection

foreach($field in $view.Field){

$viewQuery = “<Where><Gt><FieldRef Name=’ID’/><Value Type=’Counter’>0</Value></Gt></Where>”

$ViewInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
$ViewInfo.ViewTypeKind =[Microsoft.SharePoint.Client.ViewType]::Html
$ViewInfo.Query = $viewQuery
$ViewInfo.RowLimit = 50
$ViewInfo.ViewFields = $viewFields
$ViewInfo.Title = $view.Title
$ViewInfo.Paged = $true
$ViewInfo.PersonalView = $false


createViews $web

<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”” >
<View List=”Pages” Title=”Sample View” >
<Field Name=”LinkFilename”/>
<Field Name=”Title”/>
<Field Name=”Test1″/>

<View List=”Pages” Title=”Sample2 View” >
<Field Name=”LinkFilename”/>
<Field Name=”Title”/>
<Field Name=”Modified”/>


You can add multiple Views nodes and add Field tags.

Read some SharePoint tutorials:

Hope this will be useful.

Check out Best Alternative to InfoPath -> Try Now


(Installation & Features)

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →