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.

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

cls

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

$username = “samtest@microsoft.com”
$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
$clientContext.Load($web)
$clientContext.ExecuteQuery()

}

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

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

$pageViews=$pageList.Views
$clientContext.Load($pageViews)
$clientContext.ExecuteQuery()

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

foreach($field in $view.Field){
$viewFields.Add($field.Name)
}

$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

$addi=$pageList.Views.Add($ViewInfo)
$clientContext.Load($pageList)
$clientContext.ExecuteQuery()
}

}
createViews $web

XML –
<?xml version=”1.0″ encoding=”utf-8″?>
<WebSite Url=”https://microsft.sharepoint.com/sites/test/” >
<Views>
<View List=”Pages” Title=”Sample View” >
<Field Name=”LinkFilename”/>
<Field Name=”Title”/>
<Field Name=”Test1″/>
</View>

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

</Views>

</WebSite>
You can add multiple Views nodes and add Field tags.

Read some SharePoint tutorials:


Hope this will be useful.
Thanks,
Sambita

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

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 →