Forum Discussion

StephanieDuryHydro's avatar
StephanieDuryHydro
Copper Contributor
Oct 09, 2023

How can I create multiple site columns in SharePoint using PowerShell?

Hi all,

 

I'm trying to create 64 site columns (not list columns) in the Content Type Hub site so I can reuse these columns throughout our tenancy. I have found posts about how to create multiple list columns for a list using PowerShell, but I would like to create site columns.

 

The columns I'm looking at creating are straight forward, text, number, choice, person and currency. At the time of creating the columns I'd like to include the display name, internal name, description, choice options (if it's a choice column), default value, whether the column is required and which group to add the site column to. Any help with this would be much appreciated. Thanks.

5 Replies

  • StephanieDuryHydro 

     

    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: 

    1. Field element (Field) 
    2. Field element (List) 

    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.

    • StephanieDuryHydro's avatar
      StephanieDuryHydro
      Copper Contributor
      SvenSieverding 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.
      • SvenSieverding's avatar
        SvenSieverding
        Bronze 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 using 

        Invoke-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

Resources