I'm trying to add a new field to a SharePoint list and set its internal name. I have both the static name and display name specified but I can't figure out why the internal name gets set as First_x0020_Name
.
$web = Get-SPWeb 'https://server'
$list = $web.Lists['listName']
$newFieldXML = '<Field Type="Text" StaticName="FirstName" DisplayName="First Name"></Field>'
$list.Fields.AddFieldAsXml($newFieldXML)
This simply works for me-
XML should be of format:
$newFieldXML = '<Field Type="Text" Name="FirstName" StaticName="FirstName" DisplayName="First Name"></Field>'
In PowerShell use [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldInternalNameHint
as third parameter of AddFieldAsXml
function
$spListFields = $spList.Fields.AddFieldAsXml($newFieldXML, $true, [Microsoft.SharePoint.SPAddFieldOptions]::AddFieldInternalNameHint)
It looks like AddFieldAsXml cannot be used to set the internal name
According to the article, if you want to use AddFieldAsXml to set the internal name you need to:
- Define your CAML so that the Internal Name you want to use is actually set as the Display Name
- Create the field with the call to AddFieldAsXml
- After the field is created, retrieve it using the internal name and set the Title property to the real Display Name you wanted it to be in the first place
You need two additional steps: retrieve the field and set the internal name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With