I am creating a view using powershell.
Below is the XML file which contains the columns for the view and list name.
<Views>
<View List="Emp List" Title="EMP Requests" >
<Field Name="EmpName"/>
<Field Name="Status"/>
<Field Name="Emp ID"/>
<Field Name="Date of Joining"/>
</View>
</Views>
Below is the Powershell code(CSOM) to create the view:
[xml]$ViewDef = Get-Content -Path "C:\.....XML File Path\CompletedReqView.xml"
$context = New-Object Microsoft.SharePoint.Client.ClientContext("----URL Goes Here ----")
$context.Credentials = $credentials
$web=$context.Web;
$context.Load($web)
$context.ExecuteQuery()
Write-Host "Creating View" -foregroundcolor white -backgroundcolor Yellow
foreach($view in $ViewDef.Views.View)
{
$List = $context.Web.Lists.GetByTitle($view.List)
$context.Load($List)
$context.ExecuteQuery()
$View=$List.Views
$context.Load($View)
$context.ExecuteQuery()
$ViewFields = New-Object System.Collections.Specialized.StringCollection
foreach($field in $view.Field){
$ViewFields.Add($field.Name)
}
$ViewQuery = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Done</Value></Eq></Where>"
$ViewInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
$ViewInfo.ViewTypeKind =[Microsoft.SharePoint.Client.ViewType]::Html
$ViewInfo.Query = $compReqViewQuery
$ViewInfo.RowLimit = 50
$ViewInfo.ViewFields = $compReqViewFields
$ViewInfo.Title = $view.Title
$ViewInfo.Paged = $true
$ViewInfo.PersonalView = $false
$addi=$List.Views.Add(ViewInfo)
$context.Load($List)
try
{
$context.ExecuteQuery()
}
catch
{
Write-Host "Error : $_.Exception.Message" -foregroundcolor white -backgroundcolor Red
return
}
}
Using the above code, I am able to successfully create a view. I am facing two problems here :
Group By : How to perform a groupby in this view. Suppose I want the data to be grouped by a column "Status". I tried giving the Group By in the caml query ($ViewQuery in the above code) but grouping doesnt work. Should this grouping be given as an Attribute?
I want the list item in "Name" column to be a hyperlink, how to achieve this?
Please suggest.