How can I set an existing text column's maximum length with PnP Powershell?
-
17-02-2021 - |
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?!
$field = Add-PnPField -DisplayName "Company" -InternalName "Company" -Type Text -AddToDefaultView -List $ListName -ErrorAction Stop
$field.IsPropertyAvailable("MaxLength")
$field | Set-PnPField -Values @{MaxLength=4}
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" />
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:
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange