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
$viewFields.Add("Title")
$viewFields.Add($myFieldPrivacyStatementurl)
$viewFields.Add($myFieldIsActive)
$viewFields.Add($myFieldSortOrder)
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() {
param(
$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)
$clientObjectLoad.Invoke($ctx,@($ClientObject,$null))
}
function CreateList()
{
param(
[Parameter(Mandatory=$true)][string]$url,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials
)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = $credentials
$web = $ctx.Web
Invoke-LoadMethod -ClientObject $web
$ctx.ExecuteQuery()
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $ListTitle
$ListInfo.TemplateType = "100"
$myList = $web.Lists.Add($ListInfo)
$myList.Description = $ListTitle
$myList.Update()
$Fields=$myList.Fields
$ctx.Load($Fields)
$ctx.ExecuteQuery()
$myList.Fields.AddFieldAsXml("<Field Type='URL' DisplayName='PrivacyStatementUrls' Required='TRUE' />",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$myList.Update()
$ctx.ExecuteQuery()
$myList.Fields.AddFieldAsXml("<Field Type='Boolean' DisplayName='IsActive' />",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$myList.Update()
$ctx.ExecuteQuery()
$myList.Fields.AddFieldAsXml("<Field Type='Number' DisplayName='SortOrder'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$myList.Update()
$ctx.ExecuteQuery()
$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
$Item1.Update()
$ctx.ExecuteQuery()
$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
$Item2.Update()
$ctx.ExecuteQuery()
$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
$Item3.Update()
$ctx.ExecuteQuery()
}
$credentials = New-Object System.Net.NetworkCredential('name', 'pass')
$url = 'url'
$ListTitle="Privacy Statements"
CreateList $url $credentials