0

Populate Word Document but Group with Headers

I have have two sharepoint list one with the customer information on and the other will be the services that have selected. With the services that the customer have selected I want to group them by the service so I have an Append to Array in my For each which brings the inforamtion for erach service as it should, when i try to populate the word document it does not group them and puts each on on an individual line. 

This is my append to array:

I then create the JSON

Then populate the word document:

My Word Document then contains the following:

<<foreach [varcustselection in custselections]>> 

<<[varcustselection.workscope]>>  (this should be the on that is group)

            <<[varcustselection.actionnumber]>> - <<[varcustselection.worksdescription]>> 

 

 

<</foreach>> 

 

1 comment

  • 0
    Avatar
    Jo Hall

    Hi ,

    You can achieve your requirement by using "GroupBy"

    From what you have said above I believe your JSON looks like:

    {

      "custselections": [

        {

          "workscope": "Service A",

          "actionnumber": "001",

          "worksdescription": "Description of work for Service A"

        },

        {

          "workscope": "Service B",

          "actionnumber": "003",

          "worksdescription": "Another description for Service B"

        },

        {

          "workscope": "Service A",

          "actionnumber": "002",

          "worksdescription": "Description of work for Service A"

        }

      ]

    }

     

    The following syntax groups everything by 'workscope' and then outputs the associated 'actionnumber' and 'worksdescription'

    <<foreach [group in custselections.GroupBy(c => c.workscope)]>> 

    <<[group.Key]>> 

    <<foreach [item in group]>> 

    <<[item.actionnumber]>> - <<[item.worksdescription]>>

    <</foreach>> 

    <</foreach>>

    As with all populate word syntax, you may need to amend the spacing of the above to give you the desired end result.

    To keep it cleaner and make it easier to read I have added more new lines than you probably desire.

     

    To briefly explain the above:

    1. `GroupBy(c => c.workscope)`: This groups the data by the `workscope` (service name).
    2. `group.Key`: This outputs the `workscope` (the service name) for each group.
    3. Inner `foreach` loop: Iterates through each `actionnumber` and `worksdescription` within the grouped `workscope`.

    Currently the end result will look like

    Service A  

    001 - Description of work for Service A 

    002 - Description of work for Service A 

     

    Service B  

    003 - Another description for Service B

     

Please sign in to leave a comment.