1

I'm trying to add a text field to a document library with PowerShell PnP and set the maximum string length to 4 but I can't seem to figure out how and I'm getting nonsensical outputs.

When I add the field, I check if the property is available, but I can't set it?!

enter image description here

$field = Add-PnPField -DisplayName "Company" -InternalName "Company" -Type Text -AddToDefaultView -List $ListName -ErrorAction Stop
$field.IsPropertyAvailable("MaxLength")
$field | Set-PnPField -Values @{MaxLength=4}

3 Answers 3

0

I just did a test on my end, the pnppowershell command $field | Set-PnPField -Values @{MaxLength=4} works for me.

enter image description here

You could try to install the latest updated version using the following command:

Update-Module SharePointPnPPowerShell*
1
  • Somehow there were two versions installed and the older version was getting loaded. Your answer plus Get-Module SharePointPnPPowerShell* -ListAvailable | Select-Object Name,Version | Sort-Object Version -Descending helped make it more obvious. Commented Nov 2, 2020 at 18:13
0

Use Add-PnPFieldFromXml insead. The following worked for me.

$ListName= "TestLibrary"
 
#Define XML Schema for Text Field
$FieldXML= "<Field Type='Text' Name='ProjectCode' ID='$([GUID]::NewGuid())' DisplayName='Project Code' Required ='FALSE' EnforceUniqueValues = 'TRUE' Indexed='TRUE' MaxLength='100'></Field>"
 
#Add Text Field to list from XML
$f2=Add-PnPFieldFromXml -FieldXml $FieldXML -List $ListName

$f2.SchemaXml

Output of the field schema after creating the field

PS C:\WINDOWS\system32> $f2.SchemaXml
<Field Type="Text" Name="ProjectCode" ID="{6a97341d-b614-4521-8448-494609dfe60b}" DisplayName="Project Code" Required="FALSE" EnforceUniqueValues="TRUE" Indexed="TRUE" MaxLength="100
" SourceID="{367c78f8-588a-4943-9a5f-67cc1ad3b1e8}" StaticName="ProjectCode" ColName="nvarchar15" RowOrdinal="0" />

enter image description here

0

Try using below code:

$ListName = "MyListName"
$guid = New-Guid
$xml = '<Field Type="Text" Name="Company" DisplayName="Company" ID="{'+ $guid.Guid +'}" MaxLength="4"/>'  
Add-PnPFieldFromXml -FieldXml $xml -List $ListName

Microsoft official documentations:

  1. Add-PnPFieldFromXml
  2. Field element (List)

Your Answer

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

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