Question

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}
Était-ce utile?

La solution

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*

Autres conseils

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

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top