Add Column Formatting with PnP PowerShell

Column formatting can enhance our list/documents inside SharePoint. Today, I’ll use PnP PowerShell in combination with the Column formation to send an email to the person that creates a new item on the Documents list. This list is created when a new site collection is created.  We’ll add a new view on your list which and a simple email button that allow you to send an email for the creator of the item.

The Columns formatting can be applied using SharePoint interface – check the next link to know how to do it. https://support.office.com/en-us/article/column-formatting-1f927342-2bed-4745-b727-ff8b7ff96b22

The first step is to create a document on your desktop which will contain the JSON that we will apply to the Document library. Copy the JSON below and save it on your machine, it’ll be used by PowerShell to apply to the column. NOTE: You can check more samples in the below reference section.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "span",
      "style": {
        "padding-right": "8px"
      },
      "txtContent": "@currentField.title"
    },
    {
      "elmType": "a",
      "attributes": {
        "iconName": "Mail",
        "class": "sp-field-quickActions",
    "href": "='mailto:' + @currentField.email + '"
      }
    }
  ]
}   

The PnP PowerShell that will create a new view, with the name defined in the $titleView. Then added the column formatting to the column Author that is filled when a new document is created by SharePoint user.

$JSONFile = Get-Content 'c:\Users\Contoso\Desktop\columnFormatting.json' #PATH to the json file created
$JSONFile | ConvertFrom-Json |Out-Null

$site        = "https://contoso.sharepoint.com/sites/contosoSite" #Site
$username    = "contoso@contoso.onmicrosoft.com" #Your Email
$password    = "password" #Your password
$titleView   = “Send email to creator of item” #Title of View

$encpassword = convertto-securestring -String $password -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword

Connect-PnPOnline -Url $site -Credentials $cred

Add-PnPView -List Documents -Title $titleView -Fields "DocIcon","LinkFilename","Author" |Out-Null
$created = Get-PnPField -List Documents -Identity 'Author'
$created.CustomFormatter = $JSONFile
$created.Update()
$created.Context.ExecuteQuery()

Conclusion

With this sample, we can see the power of the Column Formatting and combinate that with PowerShell that can allow us to replicate this view to multiple sites.

References

4 Comments

  1. Earl Libby said:

    This can also be done without having to use CSOM with a PnP Cmdlet
    Set-PnPField -List Documents -Identity $created.Id -Values @{CustomFormatter =$JSONFile}
    replaces
    $created.CustomFormatter = $JSONFile
    $created.Update()
    $created.Context.ExecuteQuery()

    August 19, 2019
    Reply
  2. Mark said:

    When trying to reproduce, I get an error:

    WARNING: Setting property ‘CustomFormatter’ to ‘System.Object[]’ failed with exception ‘Object of type ‘System.Object[]’ cannot be converted to type ‘System.String’.’. Value will be ignored.

    Any suggestions, what could have gone wrong?

    Best regards,
    Mark

    February 1, 2022
    Reply
    • David Ramalho said:

      Hey Mark,

      Are you using a PowerShell object or something similar? Seems like a conversion problem. Create a JSON file and then try to import it. I found it to be the easier way.

      My best,
      David Ramalho

      February 24, 2022
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *