2

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
4

1 Answer 1

2

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()
{
    param(
        [Parameter(Mandatory=$true)][Microsoft.SharePoint.Client.ClientContext]$ctx,
        [Parameter(Mandatory=$true)][Microsoft.SharePoint.Client.List]$list,
        [Parameter(Mandatory=$true)][string]$ViewName
    )

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

    $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

    $list.Views.Add($ViewInfo)
    $ctx.ExecuteQuery()
}

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

5
  • What if I have a username and password for the site will this still work?
    – bimbob
    Jun 25, 2014 at 15:35
  • SharePoint Online? Or something else?
    – wjervis
    Jun 25, 2014 at 15:51
  • my site requires a username and password to logon
    – bimbob
    Jun 25, 2014 at 15:53
  • Does my edit help?
    – wjervis
    Jun 25, 2014 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, 2014 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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