Forum Discussion
Add-PnPFieldFromXml - Assistance
- Jan 09, 2021
cawool Glad my approach worked for your original question.
For Current Error:
You are getting this error because whenever you create any field it converts the number and special characters in Name attribute to hexadecimal values to store it as internal name of your field. Maybe it is not liking the '4' character at the beginning of field name.
So, you can pass the different DisplayName (which you see in list view and list forms) and Name (internal name) in your XML like:
$docCreationDate = '<Field Type="DateTime" Format="DateOnly" DisplayName="Document Creation Date" Required="TRUE" Group="some group name" ID="{an iD}" Name="DocCreationDate" Description=""/>' Add-PnPFieldFromXml -FieldXml $docCreationDate $fourDate = '<Field Type="DateTime" Format="DateOnly" DisplayName="4 Date" Required="TRUE" Group="some group name" ID="{an iD}" Name="FourDate" Description=""/>' Add-PnPFieldFromXml -FieldXml $fourDate
Please click Mark as Best Response if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
it is definitely supported, maybe you have a column already with the same name?
i have used Add-PnPFieldFromXml for various types and its is working for me
- cawoolJan 06, 2021Copper Contributor
Hi,
Its not that I cannot add a field type, I cannot add multiple field types using Add-PnPFieldFromXml. I'm trying to add three field types, it adds the first one but fails on the next ones. If I selectively only run the 2nd or 3rd Add-PnPFieldFromXml it works fine so I know the fields dont already exist plus they have unique names different from the out of the box ones.
To be clear I'm running three Add-PnPFieldFromXml in a script as I dont believe you can add all the required field types using one XML.
Relevant part of script below:
$docownXml = '<Field Type="Choice" DisplayName="Document Owner" Required="TRUE" Group="some group name" ID="{an ID}" FillInChoice="TRUE" Name="a name" Description=""> <CHOICES> <CHOICE>Choice 1</CHOICE> <CHOICE>Choice 2</CHOICE> <CHOICE>Choice 3</CHOICE> <CHOICE>Choice 4</CHOICE> </CHOICES> <Default></Default> </Field>' Add-PnPFieldFromXml -FieldXml $docownXml $appnameXml = '<Field Type="Choice" DisplayName="Application Name" Required="TRUE" Group="some group name" ID="{an ID}" FillInChoice="TRUE" Name="a name" Description=""> <CHOICES> <CHOICE>Choice 1</CHOICE> <CHOICE>Choice 2</CHOICE> <CHOICE>Choice 3</CHOICE> <CHOICE>Choice 4</CHOICE> <CHOICE>Choice 5</CHOICE> <CHOICE>Choice 6</CHOICE> <CHOICE>Choice 7</CHOICE> <CHOICE>Choice 8</CHOICE> </CHOICES> <Default></Default> </Field>' Add-PnPFieldFromXml -FieldXml $appnameXml $doccredate = '<Field Type="DateTime" Format="DateOnly" DisplayName="Document Creation Date" Required="TRUE" Group="some group name" ID="{an iD}" Name="a name" Description=""/>' Add-PnPFieldFromXml -FieldXml $doccredate
When the script tries to run the second (and third) Add-PnPFieldFromXml it fails with the below error:
add-PnPFieldFromXml : The local device name is already in use. (Exception from HRESULT: 0x80070055)
Hope this makes sense.
Thanks,
- ganeshsanapJan 06, 2021MVP
First of all, Add-PnPFieldFromXml cmdlet adds a field to a list or as a site column based upon a CAML/XML field definition and not content type. Using this command you can add multiple site columns/fields to a single content type.
You are getting this exception because there is already a field with name "local device" on your SharePoint site.
So, I will suggest you to use unique DisplayName, Name and ID in CAML every time you are using Add-PnPFieldFromXml cmdlet.
Please click Mark as Best Response if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.
- cawoolJan 06, 2021Copper Contributor
ganeshsanap Hi,
I should apologies first for mixing up my terminology, not sure why I did that but you knew what I was referring to so thanks.
I will go away and give this another look and take on your comments and hopefully post back with success asap. I think I see my problem now.