M365 Dev Blog

Using PnP DataRows to update document field values

DataRows

The PnP provisioning engine covers most of the common scenarios regarding the configuration of SharePoint sites and Teams. But it does a lot more, like adding or updating records to a SharePoint list or library using PnP DataRows. In this post, I show you how to update the properties of a document using a provisioning template.

The PnP DataRows element of the XML templates allows you to declare records that are added to the target library. The engine uses a KeyColumn property to identify the records, and if they already exist, performs an update instead.

Upload documents

In my scenario, I had to provision some files into a document library as part of a site provisioning process. For this, I simply used the PnP Files element as it does just that.

<?xml version="1.0" encoding="utf-8" ?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2020/02/ProvisioningSchema" Version="0.1" Author="">
  <pnp:Templates>
    <pnp:ProvisioningTemplate ID="Provisioning-Files">
      <pnp:Files>
        <pnp:File Src="./../../assets/Template document.pdf" Folder="TemplatesLibrary" Overwrite="true" Level="Published" />
        <pnp:File Src="./../../assets/Another doc.pdf" Folder="TemplatesLibrary" Overwrite="true" Level="Published" />
        <pnp:File Src="./../../assets/Template.docx" Folder="TemplatesLibrary" Overwrite="true" Level="Published" />
      </pnp:Files>
    </pnp:ProvisioningTemplate>
  </pnp:Templates>
</pnp:Provisioning>

While this allowed me to upload the files to the target library, it was not possible to specify field values. I have used PnP DataRows multiple times in the past to add/update items in SharePoint lists, but never with documents. As the PnP DataRows sounded like a good option, I decided to give it a try.

In this particular case, the process is using multiple provisioning templates as part of a provisioning sequence. Each template contains one of the root elements for provisioning templates. keep things simple as the overall template is quite big.

Following provisioning logical order, we get custom fields created first, then content types, and after that, lists and libraries. We then upload (using the Files element above) the required files into the libraries created previously. This is where DataRows can now be used to update document field values.

Update document fields using DataRows

As DataRows belong to ListInstance elements, we add a reference to our list again (no need to declare additional properties for the list).

Also, note the use of the Name column as the unique identifier for DataRows: KeyColumn=”FileLeafRef”. This is very important as the engine will use this value to find the correct document that needs updating.

<?xml version="1.0" encoding="utf-8" ?>
<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2020/02/ProvisioningSchema" Version="0.1" Author="">
    <pnp:Templates>
        <pnp:ProvisioningTemplate ID="Provisioning-DataRows">
            <pnp:Lists>
                <pnp:ListInstance Title="Templates Library" TemplateType="101" Url="TemplatesLibrary">
                    <pnp:DataRows KeyColumn="FileLeafRef" UpdateBehavior="Overwrite">
                        <pnp:DataRow>
                            <pnp:DataValue FieldName="FileLeafRef">Template document.pdf</pnp:DataValue>
                            <pnp:DataValue FieldName="Title">Template doc</pnp:DataValue>
                            <pnp:DataValue FieldName="TemplateDescription">This is a test template document</pnp:DataValue>
                            <pnp:DataValue FieldName="TemplateImage">{"fileName":"templateIcon.png","serverRelativeUrl":"{sitecollection}/SiteAssets/templateIcon.png"}</pnp:DataValue>
                            <pnp:DataValue FieldName="TemplateType">PDF</pnp:DataValue>
                            <pnp:DataValue FieldName="TemplateOrder">1</pnp:DataValue>
                            <pnp:DataValue FieldName="Active">true</pnp:DataValue>
                        </pnp:DataRow>
                    </pnp:DataRows>
                </pnp:ListInstance>
            </pnp:Lists>
        </pnp:ProvisioningTemplate>
    </pnp:Templates>
</pnp:Provisioning>

Exit mobile version