This XML syntax is working:

$HolidayWorkID = "<Field Type='Number' 
  Group='Holidays' />"

But this doesn't working:

$HolidayReportMonth = "<Field Type='Calculated' 
                                        <FieldRef Name='HolidayDateFrom' />

Error message is:

Exception calling "AddFieldAsXml" with "3" argument(s): "Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 2, position 10."


Could you try this

$HolidayReportMonth = "<Field Type='Calculated' Name='HolidayReportMonth' DisplayName='CustomMonth' Group='Holidays' ResultType='Text'> <FieldRefs> <FieldRef Name='HolidayDateFrom' /></FieldRefs>  <Formula>=MONTH([HolidayDateFrom])</Formula></Field>"

Because there are child elements Like Formula and FieldRef, I think the issue in the <Field>, that should be surrounded with > after the result type then close it at the end with </Field>

Note: Fields.AddFieldAsXml() takes 3 arguments

  1. schemaXml as String
  2. addToDefaultView as Bool
  3. Options as SPAddFieldOptions check SPAddFieldOptions enumeration

Alternatively, you can add a new calculated column via PowerShell, try the below script

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

$WebURL="http://your site"
$ListName="your List"

#Get the Web
$web = Get-SPWeb $WebURL

#Get the List
$list = $web.Lists.TryGetList($ListName)

#Add new calculated column
$list.Fields.Add($CalculatedColumnName, "Calculated", $false)

#Get the column
$CalculatedCol = $List.Fields.GetField($CalculatedColumnName)
#Set Formula for calculated column

Read more: How to Add Calculated Column to SharePoint List using PowerShell

  • 1
    Тhank you @M.Qassas – Goshky Dec 7 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.