How can I create a view in Powershell using CSOM This works in server side I am unsure how to do it in CSOM

$viewQuery = '<Where><Eq><FieldRef Name="IsActive" /><ValueType="Integer">1</Value></Eq></Where>'
$viewFields = New-Object System.Collections.Specialized.StringCollection

Here is the code I have so far :

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'

Function Invoke-LoadMethod() {


   $ClientObject = $(throw "Please provide an Client Object instance on which to invoke the generic method")


   $ctx = $ClientObject.Context

   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 

   $type = $ClientObject.GetType()

   $clientObjectLoad = $load.MakeGenericMethod($type) 



function CreateList()






 $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)

 $ctx.Credentials = $credentials

 $web = $ctx.Web 

 Invoke-LoadMethod -ClientObject $web


$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $ListTitle
$ListInfo.TemplateType = "100"
$myList = $web.Lists.Add($ListInfo)
$myList.Description = $ListTitle

$myList.Fields.AddFieldAsXml("<Field Type='URL' DisplayName='PrivacyStatementUrls' Required='TRUE' />",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)


$myList.Fields.AddFieldAsXml("<Field Type='Boolean' DisplayName='IsActive' />",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)

$myList.Fields.AddFieldAsXml("<Field Type='Number' DisplayName='SortOrder'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)

$ListItemInfo1 = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item1 = $myList.AddItem($ListItemInfo1)
$Item1["Title"] = "test1"
$Item1["PrivacyStatementUrls"] = "http://www.google.co.uk"
$Item1["IsActive"] = 0
$Item1["SortOrder"] = 1

$ListItemInfo2 = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item2 = $myList.AddItem($ListItemInfo2)
$Item2["Title"] = "test2"
$Item2["PrivacyStatementUrls"] = "http://www.bbc.co.uk"
$Item2["IsActive"] = 1
$Item2["SortOrder"] = 2
$ListItemInfo3 = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item3 = $myList.AddItem($ListItemInfo3)
$Item3["Title"] = "test3"
$Item3["PrivacyStatementUrls"] = "http://www.skynews.com"
$Item3["IsActive"] = 1
$Item3["SortOrder"] = 3


$credentials = New-Object System.Net.NetworkCredential('name', 'pass')

$url = 'url'

$ListTitle="Privacy Statements"

CreateList $url $credentials

You use the ViewCreationInformation class:

Add this function to your script, and call it after creating the list (after the ExecuteQuery() that follows the list creation):

function CreateView()

    $viewQuery = '<Where><Eq><FieldRef Name="IsActive" /><ValueType="Integer">1</Value></Eq></Where>'
    $viewFields = New-Object System.Collections.Specialized.StringCollection

    $ViewInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
    $ViewInfo.Query = $viewQuery
    $ViewInfo.RowLimit = 50
    $ViewInfo.ViewFields = $viewFields // It takes a string array, not sure if a String Collection will work
    $ViewInfo.Title = $ViewName


I may have messed something up with the PowerShell, it's been awhile.

  • What if I have a username and password for the site will this still work? – bimbob Jun 25 '14 at 15:35
  • SharePoint Online? Or something else? – wjervis Jun 25 '14 at 15:51
  • my site requires a username and password to logon – bimbob Jun 25 '14 at 15:53
  • Does my edit help? – wjervis Jun 25 '14 at 15:59
  • its confusing me as I am trying to add it to the code I have already done. I have added my code to the question – bimbob Jun 25 '14 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.