Forum Discussion
How can I create multiple site columns in SharePoint using PowerShell?
If Add-PnFPield command does not work for you to set description, default value, etc. additional properties related to site columns, you can also use the Add-PnPFieldFromXml cmdlet in PnP PowerShell to create site columns.
This command allows to add new site columns by specifying its definition in CAML/XML format.
You can use all properties related to site columns in CAML/XML snippet as mentioned here:
Please click Mark as Best Response & Like 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.
- StephanieDuryHydroOct 09, 2023Copper ContributorSvenSieverding and ganeshsanap - thanks for your responses.
So would I need to run that command for each column I want to create? Or is there a script I can use which refers to a csv file containing the 64 columns and all the details to create the columns? Thanks.- SvenSieverdingOct 09, 2023Bronze Contributor
Hi StephanieDuryHydro ,
yes, you would have to create such a script and call one of the commands for each of your columns.
If you just need to create 64 columns once then I personally would create the columns manually though the UI.
If I need to re-create multiple site columns repeatedly, then I normally create a "PnP Provisioning Schema" / Site Template XML File like this<?xml version="1.0"?> <pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2022/09/ProvisioningSchema"> <pnp:Preferences Generator="ME" /> <pnp:Templates ID="CONTAINER-TEMPLATE-MyColumns"> <pnp:ProvisioningTemplate ID="TEMPLATE-MyColumns" Version="1" BaseSiteTemplate="SITEPAGEPUBLISHING#0" Scope="RootSite"> <pnp:SiteFields> <Field Type="DateTime" DisplayName="MyDateColumn" Description="Description" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateOnly" Group="MyCustomColumns" FriendlyDisplayFormat="Disabled" ID="{e8468b37-b2cc-4384-bef7-5c4c96fc50be}" SourceID="{site}" StaticName="MyDateColumn" Name="MyDateColumn"> <Default>2024-01-01T00:00:00Z</Default> </Field> <Field Type="Text" DisplayName="TextTextColumn" Description="My Description" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="MyCustomColumns" ID="{a2d0bf3c-515b-445d-b884-d09e6e71a811}" SourceID="{site}" StaticName="TextTextColumn" Name="TextTextColumn"> <Default>MyDefaultValue</Default> </Field> <Field Type="Number" DisplayName="MyNumberColumn" Description="Description" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Group="MyCustomColumns" ID="{fd928d5a-41bc-4ced-8350-7a95910f76f3}" SourceID="{site}" StaticName="MyNumberColumn" Name="MyNumberColumn"> <Default>234</Default> </Field> <Field Type="Choice" DisplayName="MyChoiceColumn" Description="Description" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="Dropdown" FillInChoice="FALSE" Group="MyCustomColumns" ID="{3fd4367d-80b8-44f1-8af6-8aa164814d55}" SourceID="{site}" StaticName="MyChoiceColumn" Name="MyChoiceColumn"> <Default>Enter Choice #1</Default> <CHOICES> <CHOICE>Enter Choice #1</CHOICE> <CHOICE>Enter Choice #2</CHOICE> <CHOICE>Enter Choice #3</CHOICE> </CHOICES> </Field> </pnp:SiteFields> </pnp:ProvisioningTemplate> </pnp:Templates> </pnp:Provisioning>
And then apply that to a site usingInvoke-PnPSiteTemplate -Path template.xml
In the XML file you have all the information about all site columns to be provisioned.
If you want another i.e. "Number Column", then just copy the Element '<Field type="Number" .... </Field>' and replace the attributes with your values. You need a new and different GUID for each ID column.
Best Regards,
Sven- StephanieDuryHydroOct 10, 2023Copper ContributorHey SvenSieverding,
It looks like it would take me just as long to figure out the script and XML file as it would to just create the columns in the UI, as you suggest.
It does have me thinking, do I REALLY need to create these columns at the CTH level, or should I use your script/XML file option to create them at each site level and give the Site Owners the freedom to change the columns as and when it occurs, rather than them raising a ticket to IT to change at the CTH level each time.
This gives me something to think about to ensure I deliver a more fit for purpose solution to the end user. Thank you SvenSieverding