0

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 :

  1. 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?

  2. I want the list item in "Name" column to be a hyperlink, how to achieve this?

Please suggest.

user2598808
  • 633
  • 5
  • 22
  • 40

1 Answers1

0

To get the grouping you need to update your CAML Query to include a group by element. If it isn't working you need to make sure your CAML is well formed. I suggest using a CAML query tool.

<GroupBy Collapse=\"True\" GroupLimit=\"300\">Your field ref in here</groupBy>

I'm not sure around the link , if I wanted it to be a hyperlink I would use a xsltview web part and customise the xslt.

Cheers

Truez

Truezplaya
  • 1,315
  • 1
  • 13
  • 26