With the Office 365 PnP PowerShell extensions we have a very simple way to create new columns with the cmdlet Add-PnPFieldFromXml. The xml to use with this cmdlet could be taken from the Field-Element schema: https://msdn.microsoft.com/de-de/library/office/aa979575.aspx. So, to add new fields we just need to build the xml for the new column. Next some examples, tested as site columns:
Text field
<Field Type="Text" DisplayName="Document Title English" Description="Document Title in English" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" MaxLength="255" Group="Demo" ID="{161ef8f6-e73c-4c56-8a5f-c6a8900f2fc8}" StaticName="TitleEN" Name="TitleEN"> </Field>
Multitext field
<Field ID="{B76B58EC-0549-4f00-9575-2FD28BD55010}" Name="DemoDescription" DisplayName="Description" Description="A summary of the asset" StaticName="DemoDescription" Group="Demo" Type="Note" NumLines="5" UnlimitedLengthInDocumentLibrary="TRUE" Hidden="FALSE" ShowInEditForm="TRUE" SourceID="http://schemas.microsoft.com/sharepoint/v3" />
Number field
<Field ID="{f9e31c80-7de8-4577-8df6-d7821aff4bce}" Type="Number" Name="DSStatus" DisplayName="DS Status" StaticName="DSStatus" Group="Demo" Description="" Hidden="false" ShowInNewForm="FALSE" ShowInEditForm="FALSE" Min="0" Max="2" Decimals="0"> <Default>0</Default> </Field>
Currency field
<Field Type="Currency" DisplayName="Price" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Decimals="2" LCID="1033" ID="{57717312-4640-4408-93bb-1cb8eabb8f9a}" SourceID="{269f1ad3-0434-4032-8677-99e06e3e6a01}" StaticName="Price" Name="Price" CustomFormatter="" />
Choice field
<Field Type="Choice" DisplayName="Process Group" Description="Process Group" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="Dropdown" FillInChoice="FALSE" Group="Demo" ID="{cdc426f9-4db3-4d03-936c-e9986ade7254}" StaticName="ProcessGroup" Name="ProcessGroup"> <Default>Process group #1</Default> <CHOICES> <CHOICE>Process group #1</CHOICE> <CHOICE>Process group #2</CHOICE> <CHOICE>Process group #3</CHOICE> <CHOICE>Process group #4</CHOICE> </CHOICES> </Field>
Yes/No field (Boolean)
<Field Type="Boolean" DisplayName="ReminderEmailSent" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Hidden="TRUE" Group="Demo" ID="{CE1411F7-7B47-4EE3-AD4B-E70AE5FC21F9}" StaticName="ReminderEmailSent" ShowInEditForm="FALSE" ShowInNewForm="FALSE" ShowInViewForms="FALSE" ShowInDisplayForm="FALSE" Name="ReminderEmailSent"> <Default>FALSE</Default> </Field>
DateTime field
<Field Type="DateTime" DisplayName="Approved" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" Format="DateTime" Group="Demo" FriendlyDisplayFormat="Disabled" ID="{2eb9160b-7f84-4f35-9834-5253ee84c292}" StaticName="Approved" ShowInEditForm="FALSE" ShowInNewForm="FALSE" Name="Approved"> </Field>
Person field
<Field Type="User" DisplayName="Approved by" List="UserInfo" Required="FALSE" EnforceUniqueValues="FALSE" ShowField="ImnName" UserSelectionMode="PeopleOnly" UserSelectionScope="0" Group="Demo" ID="{c4994fe6-24fe-458e-b06a-b9ed9fcb5c39}" StaticName="Approvedby" ShowInEditForm="FALSE" ShowInNewForm="FALSE" Name="Approvedby"> </Field>
Hyperlink field
<Field Type="URL" DisplayName="Review" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" ShowInEditForm="FALSE" ShowInNewForm="FALSE" Format="Hyperlink" Group="Demo" ID="{b30b37aa-c6ea-4e9f-a848-7e251a7571b1}" StaticName="Review" Name="Review"> </Field>
Calculated field
<Field ID="{111b0f4c-3135-4a8a-9d7d-d53daaf1931a}" Type="Calculated" ResultType="Number" Name="WorkflowStatus" DisplayName="Workflow Status" StaticName="WorkflowStatus" Group="Demo" Description="" Hidden="false" JSLink="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js|~site/Scripts/Fields.JSLink.js"> <FieldRefs> <FieldRef Name="IMStatus" /> <FieldRef Name="ISStatus" /> <FieldRef Name="ITStatus" /> <FieldRef Name="FBStatus" /> <FieldRef Name="DSStatus" /> </FieldRefs> <Formula>=[IMStatus]+[ISStatus]+[ITStatus]+[FBStatus]+[DSStatus]</Formula> </Field>
Lookup field
<Field ID="{8e99027f-501b-4902-8ee4-97020d04df74}" DisplayName="Company" Name="DemoCompany" StaticName="DemoCompany" Group="Demo" Type="Lookup" Required="FALSE" EnforceUniqueValues="FALSE" List="Companies" ShowField="Title" />
When defining a lookup field from xml, you must replace the listname (“Companies” in the example) by the guid of the list, before the xml could be used.
To create taxonomy fields, use the cmdlet Add-PnPTaxonomyField instead of the Add-PnPField cmdlet.
When you are not sure, which attributes you need in the xml, simply create the field in a library using the web interface, then execute the Get-PnPField cmdlet as shown in this example:
That’s it, really simple.
How are the Guid’s generated in the above samples so that they are unique?
Hi Werner,
I use http://www.guidgen.com to create the IDs in my projects, since the GuidGen tool in Visual Studio won’t work in my dev environments.
Cheers,
Olaf