Tip – Difference between Add-PnPFieldFromXml and Add-PnPField While Creating Calculated Fields Using PnP PowerShell

Sathish Nadarajan
 
Solution Architect
April 17, 2020
 
Rate this article
 
Views
2214

In this article, let me try to explain my experience with Add-PnPField and Add-PnPFieldFromXml methods in PnP PowerShell. Usually both the methods were used to create the fields and then, we can add those site columns to the content types and the lists will get refreshed.

But, when I was expecting the same thing while creating a Calculated Field using Add-PnPField method, the content type was getting updated, but the lists were not refreshed/updated with the newly created calculated field. The code which I used to create the Calculated Field is,

Add-PnPField -DisplayName ‘MyCalculatedField' -InternalName ' MyCalculatedField' -Type Calculated -Group ‘MyGroup’ –Formula ‘=MyOtherColumnInternalName*7.5’

Add-PnPFieldToContentType -ContentType ‘MyContentType’ -Field 'MyCalculatedField'

The above lines, were working without any exception. The field got created and added with the Content Type as well. But, when I expect this field on the List, it was not there. Later found that, there is a parameter called ResultType which is mandatory to add the calculated field to the List. Hence, we cannot use Add-PnPField to create a Calculated field. Instead, we have to use Add-PnpFieldFromXML.

The piece of code as below.

$webUrl = 'http://MySiteCollection/sites/EuroStat/'
$fieldGroupName = 'MyGroup'

Connect-PnPOnline -Url $webUrl -CurrentCredentials -ErrorAction Inquire 

$currentContext = Get-PnPContext 


$fieldXML = "<Field Type='Calculated' Group='MyGroup'  ID='{519e9eda-b137-4308-bfbd-d99cbbf66213}' StaticName='MyCalculatedField' Name=' MyCalculatedField' DisplayName= MyCalculatedField' LCID='1033' ResultType='Number' ReadOnly='TRUE'>
              <Formula>=MyOtherColumn*7.5</Formula>
              <FieldRefs>
                <FieldRef Name='MyOtherColumn' />
              </FieldRefs>
            </Field>"

Add-PnPFieldFromXml -FieldXml $fieldXML
 
$ctype =  Get-PnPContentType 'MyContentType'

$currentContext.Load($ctype.Fields)
$currentContext.ExecuteQuery()
$timeSheetHoursField =  $ctype.Fields | Where-Object InternalName -eq 'MyCalculatedField'
if(-not  $timeSheetHoursField){
    
    Add-PnPFieldToContentType -Field ‘MyCalculatedField' -ContentType $ctype
    $ctype.Update($true)
}


Disconnect-PnPOnline

The above piece of code will create a calculated field, add to the content type and the Lists inheriting this content type will also be populated with the field. A small tip, which will help someone with similar issue.

Happy Coding,
Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment