SharePoint columns adhere to a Field Definition Schema, which is in XML. You can get by with the PnP command to create fields without using the XML schema format.

Knowing the schema format for SharePoint fields can come in handy. You can use the Add-PnPFieldFromXml cmdlet to create a field in a list or a site column based on a CAML/XML field definition.

All fields share some common mandatory and optional elements in addition to field specific elements.

Common Mandatory Attributes

  • Type – Required Text. Defines the type of field
  • Name – Required Text. The internal name of the field. The name must be unique with respect to the set of fields in a list or website.
  • ID – Required Text. Uniquely identifies the field. The value should be the string representation of a GUID contained within braces ({}).
  • Required – Required Boolean. Determines whether a field is mandatory or not.

Common Optional Attributes

  • DisplayName – Optional Text. Display name of the field. The display name can be edited by the user and can change after initial field creation.
  • Description – Optional Text. Provides the description that is shown in the edit form for a list if a field description is specified. 
  • Group – Optional Text. Specifies the column group to which the field belongs.
  • Indexed – Optional BooleanTRUE if the column is indexed for use in view filters.
  • Hidden – Optional Boolean. If TRUE, the field is completely hidden from the user interface.
  • AllowDuplicateValues – Optional Boolean. Specifies whether duplicate values are allowed in a given list field.

A Field definition can contain a Default element to specify the default value of the field.

Let’s look at a few of the common fields.

Text Field

<Field
    Type="Text"
    ID="{723893ff-3f99-4cb7-b52f-f0b9727570e5}"
    Name="MyName"
    Required="TRUE"
    DisplayName="My display name"    
    Description="Description for users"    
    AllowDuplicateValues="FALSE"
    Indexed="FALSE"
    MaxLength="255"
    Group="My Group"
    >
</Field>

The following are optional attributes you can use with textfield

  • MaxLength – Optional Integer. Specifies the maximum number of characters allowed in a field value.

Multi Line TextField

<Field
    Type="Note"
    ID="{4aa7d9df-f9ab-4056-b9a7-c87a6c16f1c1}"
    Name="MyName"
    DisplayName="My display name"
    Description="Description for users"
    Group="My Group"   
    NumLines="5"
    Required="TRUE"
>
</Field>

The following are optional attributes you can use with multi line textfield

  • NumLines – Optional Integer. Recommends the number of lines (rows) to display in a TEXTAREA block during editing.

Number Field

<Field
    Type="Number"
    ID="{e440e3e8-1dba-4eef-8b73-3c3ff01358ca}"
    Name="MyName"
    DisplayName="My display name"
    Required="TRUE"
    Description=""
    Min="0"
    Max="2"
    Decimals="0">
    <Default>0</Default>
</Field>

The following are optional attributes you can use with multi line number field

  • Decimals – Optional Integer. Determines the number of decimals to display.

Currency Field

<Field
    Type="Currency"
    DisplayName="My display name"
    Required="TRUE"
    Decimals="2"
    LCID="1033"
    ID="{c58d3a3f-d237-46e1-a6dc-a96c45ec9af8}"
    Name="MyName"
/>

The following are optional attributes you can use with line number field

  • LCID – Optional Integer. The LCID attribute can be used to specify the country/region whose currency format is being used, for example, 1033 for the United States.

DateTime Field

<Field
    Type="DateTime"
    DisplayName="My display name"
    Format="DateTime"
    Required="TRUE"
    ID="{bd880db9-2e07-4ca1-ba4d-a8a00282d93c}"
    Name="MyName">
</Field>

The following are optional attributes you can use with datetime field

  • Format – Optional Text. Specifies the formatting to use for numerical values. Values include DateOnly, DateTime , ISO8601

Yes/No Field

<Field
    Type="Boolean"
    DisplayName="My display name"
    Required="FALSE"
    ID="{c5f5f117-06f7-4f6f-a298-7ac4cef8f3df}"
    Name="MyName">
    <Default>FALSE</Default>
</Field>

Choice Field

<Field
    Type="Choice"
    DisplayName="My display name"
    Required="TRUE"
    Format="Dropdown"
    FillInChoice="FALSE"
    ID="{f5271d8c-16bf-4d64-bc1e-b9768467acf0}"
    Name="MyName">
    <Default>Choice #1</Default>
    <CHOICES>
        <CHOICE>Choice #1</CHOICE>
        <CHOICE>Choice #2</CHOICE>
        <CHOICE>Choice #3</CHOICE>
        <CHOICE>Choice #4</CHOICE>
    </CHOICES>
</Field>

The following are optional attributes you can use with choice field

  • FillInChoice – Optional BooleanTRUE if the field allows users to fill in values for the column.
  • Format – Optional Text. Specifies the formatting. Valid values include Dropdown (default), RadioButtons

LookUp Field

<Field
    ID="{16216d6d-079d-469b-a5f8-b687cbdcbba0}"
    DisplayName="My display name"
    Name="MyName"
    Type="Lookup"
    Required="FALSE"
    List="Lists/Countries"
    ShowField="Title"
/>

The following are optional attributes you can use with lookup field

  • ShowField – Optional Text. Specifies the internal name (Name attribute) of the target field to look up. If no value is specified, the hyperlinked text from the Title field of the record in the target list is displayed.
  • List – Optional Text. Used to identify the list that is the target of a lookup field
    • If the target list already exists, the value of the List attribute should be the string representation of the GUID (including braces)
    • If the target list does not yet exist, the value of the List attribute can be a web-relative URL such as “Lists/My List”

Person Field

<Field
  Type="User"
  DisplayName="My display name"
  Required="FALSE"
  UserSelectionMode="0"
  UserSelectionScope="0"
  ID="{65522846-1fb7-4e3a-9622-36d719bfe726}"
  Name="MyName"> 
</Field>

The following are optional attributes you can use with person field

  • UserSelectionMode – Optional Text. Specifies whether only the names of individual users can be selected in a user field on an item form, or whether the names of both individuals and groups can be selected. The following values are possible:
    • 0: Only the names of individuals can be selected.
    • 1: The names of both individuals and groups can be selected
  • UserSelectionScope – Optional Integer. Specifies a scope for selecting user names in a user field on an item form. If the value is 0, there is no restriction to a SharePoint group. If the value is greater than 0, user selection is restricted to members of the SharePoint group whose ID equals the value that is specified.

Calculated Field

<Field
    ID="{66c77b25-8218-4626-b7b6-38bfeacf48da}"
    Type="Calculated"
    ResultType="Number"
    Name="MyName"
    DisplayName="My display name">
    <FieldRefs>
        <FieldRef Name="Title" />
        <FieldRef Name="Location" />
    </FieldRefs>
    <Formula>=[Title]+[Location]</Formula>
</Field>

The following are optional attributes you can use with calculated field

  • ResultType – Optional Text. Specifies the data type of values that are returned from the field.
<Field
    Type="URL"
    DisplayName="My display name"
    Required="TRUE"
    Format="Hyperlink"
    ID="{be6daafe-60cb-49c7-a8ef-90ee69c15c37}"
    Name="MyName">
</Field>

The following are optional attributes you can use with hyperlink field

  • Format – Optional Text. Specifies the formatting. Valid values include
    • Hyperlink (default), which displays the URL as a normal <a> tag 
    • Image, which displays the URL as <img src="url" alt="Description" />.

Using the PnP Add-PnPFieldFromXML

You can use Add-PnPFieldFromXML to create a site column or a list column. Given a text field definition as bewlow:

$fieldXml = '<Field Type="Text" DisplayName="My display name" Required="TRUE" ID="{b30b37aa-c6ea-4e9f-a848-7e251a7571b1}" MaxLength="255" Name="MyName"/>'

You can create a site column using

Add-PnPFieldFromXml -FieldXml $fieldXml

or a list column using

Add-PnPFieldFromXml -List "MyList" -FieldXml $fieldXml