SOLVED

Add-PnPFieldFromXml - Assistance

Copper Contributor

Hi,

 

New here, far from being any kind of developer but always looking to learn.  This time around for me its Add-PnPFieldFromXml and specifically adding multiple site content types.

As I'm learning SharePoint rather than keep manually entering the same Content types over and over in new team site iterations I wanted to run a script and add the required Content Types.

 

If I run the below and try and run the Add-PnPFieldFromXml for another field I receive an error "dd-PnPFieldFromXml : The local device name is already in use. (Exception from HRESULT: 0x80070055)"

$docownXml = '<Field Type="Choice" DisplayName="Document Owner" Required="TRUE" Group="Some Group Name" ID="{An ID}" FillInChoice="TRUE" Name="Some 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

 

I'm assuming running multiple Add-PnPFieldFromXml in a script is not supported or I'm missing something?

Does anyone have any suggestions?

 

To note, I do not have access to the /contenttypehub/.

 

Thanks and Happy New Year!

8 Replies

@cawool 

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

@cawool Hi,


Have you tried Get-PnPProvisioningTemplate? It simplifies your work even further with much more possibilites in deploying conten types, lists, pages etc.

Yours sincerely,
Aref Halmstrand

@ValerasNarbutas 

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,

@cawool 

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.

 

@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.

@cawool 

Hi,

I managed to get this working. It was the GUID that wasn't generating a unique ID each time. Fixed it with:
$FieldID = New-Guid

However, for some reason adding a custom DateTime field always results in the below error no matter what the GUID is/starts with.
Add-PnPFieldFromXml : Name cannot begin with the '4' character, hexadecimal value 0x34.

I can manually create it no problem and was hoping to use Get-PnPField with -Includes "SchemaXML" to see resultant XML but it doesn't like that... at all.

best response confirmed by cawool (Copper Contributor)
Solution

@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.

@ganeshsanap Hi,

 

We have success!  Not sure what was going on before but my DisplayName did not have a number in it even though it was complaining about it.  I deleted the site and started a fresh and made sure my code was clean and as it should be.

 

Thank you for your assistance, much appreciated!

1 best response

Accepted Solutions
best response confirmed by cawool (Copper Contributor)
Solution

@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.

View solution in original post