0

On my SharePoint Online DEV environment, I try to add or read some Fields with PNP PowerShell.

Add-PnPFieldFromXml -FieldXml <Field Type="Boolean" Name="AddedtoSchedule" DisplayName="Added to Schedule" Group="Change Requests" Description="" ID="{5cc7cd51-596c-432d-a15a-34987d597786}"/>

Add-PnPFieldFromXml : Input string was not in a correct format. Get-PnPField : Input string was not in a correct format.

this works on all other environments... but not on this dev site collection... It works also on all subsites

How I build the xml:

$xml = ('<Field Type=' + ""$type"" + ' Name=' + ""$iName"" + ' DisplayName=' + ""$dName"" + ' Group=' + ""$group"" + ' Description=' + ""$description"" + ' ID=' + ""$id"" + '/>')

Working example

Get-PnPWeb

The strange thing is, if I try the same commands on two other environments or on a subsite of the PWA, it works without any problem... I have compared the environments and all of the site collections feature settings and there is no difference...

function AddSiteColumns($sheet, $rowMax) {
try {

    Write-Host "######################"
    Write-Host "#Add new Site Columns#"
    Write-Host "######################"$nl

    for ($i = 2; $i -le $rowMax; $i++) {
        $dName = $sheet.Cells.Item($i, 1).text
        $iName = $sheet.Cells.Item($i, 2).text
        $type = $sheet.Cells.Item($i, 3).text
        $group = $sheet.Cells.Item($i, 4).text
        $choices = $sheet.Cells.Item($i, 5).text
        $choicesArray = $choices -split ","
        $choicesArray = $choicesArray.trim()
        $required = $sheet.Cells.Item($i, 6).text
        $description = $sheet.Cells.Item($i, 7).text
        $new = $sheet.Cells.Item($i, 8).text
        $properties = $sheet.Cells.Item($i, 9).text

        if ($new -eq "new") {


            try { 

                if ($required -eq "TRUE" -and $choices) {
                    #Add-PnPField -DisplayName "$dname" -InternalName "$iName" -Type "$type" -Choice $choicesArray -Required -Group "$group"       
                    $id = createGUID
                    $choices = buildChoiceXML $choicesArray
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + ' Required="TRUE">' + $choices + '</Field>')                        
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"    
                }
                elseif ($choices) {
                    #Add-PnPField -DisplayName "$dname" -InternalName "$iName" -Type "$type" -Choice $choicesArray  -Group "$group" 
                    $id = createGUID
                    $choices = buildChoiceXML $choicesArray
                    $xml = '<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + '>' + $choices + '</Field>'
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"    

                }
                elseif ($type -eq "DateOnly") {
                    $id = createGUID
                    $type = "DateTime"
                    $format = "DateOnly"
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + ' Format=' + "`"$format`"" + '/>')
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"
                }
                elseif ($type -eq "DateOnly" -and $required -eq "TRUE") {
                    $id = createGUID
                    $type = "DateTime"
                    $format = "DateOnly"
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + ' Format=' + "`"$format`"" + ' Required="TRUE"/>')
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"
                }
                elseif ($type -eq "Calculated" -and $required -eq "TRUE") {
                    $id = createGUID
                    $type = "Calculated"
                    if ($properties) {
                        $fieldFormula = "<Formula>" + $properties + "</Formula>"
                    }
                    else {
                        $fieldFormula = ""                            
                    }
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + ' Required="TRUE" >' + $fieldFormula + '</Field>')
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"
                }
                elseif ($type -eq "Calculated") {
                    $id = createGUID
                    $type = "Calculated"
                    if ($properties) {
                        $fieldFormula = "<Formula>" + $properties + "</Formula>"
                    }
                    else {
                        $fieldFormula = ""                            
                    }
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + ' Required="TRUE" >' + $fieldFormula + '</Field>')
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"
                }
                elseif ($type -eq "Lookup") {
                    $id = createGUID
                    $showField = "Title"
                    #get list GUID to connect the lookup table
                    $lid = Get-PnPList -Identity "$properties"
                    $lid = $lid.Id.GUID
                    $lid = "{$lid}"
                    Add-PnPFieldFromXml -FieldXml ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' EnforceUniqueValues=' + "`"$enforce`"" + ' List=' + "`"$lid`"" + ' ID=' + "`"$id`"" + ' ShowField=' + "`"$showField`"" + '/>')
                    # Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group" 
                } 
                elseif ($required -eq "TRUE") {
                    $id = createGUID
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + ' Required="TRUE"/>')
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group ListID = $lookupId"
                }                               
                else {
                    $id = createGUID
                    $xml = ('<Field Type=' + "`"$type`"" + ' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + '/>')
                    Add-PnPFieldFromXml -Field $xml
                    Write-Host "Added SiteColumn: Name = $dname, Internal Name = $iName, Type = $type, Group = $group"    
                } 
                Write-Host $xml

            }
            catch [Exception] {
                Write-Error "Error by setting colum" $dName
                continue
            }
        }
    }
    Write-Host -ForegroundColor Green "All fields were succesfully added"$nl
}
catch [Exception] {
    Write-Error  "At least one field was not created"
    break
}

}

4
  • Get-PnPWeb works per default as it does not need any mandatory parameters. However, Add-PNPFieldFromXml do have mandatory parameters. What was the full command you ran for pnpFieldFromXml? Apr 13, 2018 at 10:09
  • The Add-PnPFieldFromXml takes the following xml as example: <Field Type="Boolean" Name="AddedtoSchedule" DisplayName="Added to Schedule" Group="Change Requests" Description="" ID="{5cc7cd51-596c-432d-a15a-34987d597786}"/> this works on all other environments... but not on this dev site collection... It works also on all subsites
    – theface
    Apr 13, 2018 at 10:49
  • How I build the xml: $xml = ('<Field Type=' + ""$type"" + ' Name=' + ""$iName"" + ' DisplayName=' + ""$dName"" + ' Group=' + ""$group"" + ' Description=' + ""$description"" + ' ID=' + ""$id"" + '/>') Add-PnPFieldFromXml -Field $xml
    – theface
    Apr 13, 2018 at 10:50
  • Please add it to your question instead. :) Apr 13, 2018 at 10:54

1 Answer 1

0

I have found a solution.

If I add StaticName as an additional parameter it works.

$xml = ('<Field Type=' + "`"$type`"" + ' StaticName=' + "`"$iName`"" +' Name=' + "`"$iName`"" + ' DisplayName=' + "`"$dName`"" + ' Group=' + "`"$group`"" + ' Description=' + "`"$description`"" + ' ID=' + "`"$id`"" + '/>')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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