How to Provision the Lookup Fields in SharePoint Office 365 using Patterns and Practices PNP


Sathish Nadarajan
SharePoint MVP
Published On :   16 Jul 2018
Visit Count
Today :  5    Total :   929



Few months back, we had seen, how to provision the site columns and content types in Office 365 using PNP here. It was a straight forward implementation.

But, in most of the cases, we require Lookup Fields nowadays. Patterns and Practices is capable enough to take that as well.

Usually, the understanding was like, PNP engine will create the Sitecolumns first, so that if it is a straight forward sitecolumns, without any issue, it will get created. But, when a lookup field tried to create, then at that time, the list was not created. Exception will be thrown. But that understanding was totally a wrong perception.

PNP is intelligent enough to create the site columns, which are having no relationship and create the lists, with those site columns, and come back to create the related site columns. It was really interesting. Let me explain further with the help of the below XML.


 <?xml version="1.0"?>
 
 <pnp:ProvisioningTemplate ID="SI.SOWTracker" Version="1" xmlns:pnp="http://schemas.dev.office.com/PnP/2015/12/ProvisioningSchema">
 
 <pnp:SiteFields>
 
 <Field Name="ClientCode"
 
 StaticName="ClientCode"
 
 DisplayName="Client Code"
 
 ID="{316281FD-F612-4798-8D96-350EC8E511D8}"
 
 Type="Text"
 
 MaxLength="10"
 
 Group="MyGroup"
 
 Required="TRUE"
 
 Indexed="TRUE"
 
 EnforceUniqueValues ="TRUE"
 
 ShowInEditForm='TRUE'
 
 ShowInDisplayForm='TRUE'
 
 ShowInNewForm='TRUE' >
 
 </Field>
 
 <Field Name="ClientCodeLookup"
 
 StaticName="ClientCodeLookup"
 
 DisplayName="ClientCode"
 
 ID="{175BF436-FB20-4746-914D-44DFB918EFB7}"
 
 Type="Lookup"
 
 List="{{listid:Clients}}"
 
 WebId="{siteid}"
 
 SourceID="{{siteid}}"
 
 ShowField="SIClientCode"
 
 UnlimitedLengthInDocumentLibrary="FALSE"
 
 Group="My Group"
 
 Required="TRUE"
 
 ShowInEditForm='TRUE'
 
 ShowInDisplayForm='TRUE'
 
 ShowInNewForm='TRUE'>
 
 </Field>
 
 </pnp:SiteFields>
 
 <pnp:ContentTypes>
 
 <pnp:ContentType ID="0x0100F96F997E5680487B9F800DCEC2901F1D" Name="Client" Description="Client Master Content Type" Group="My Group">
 
 <pnp:FieldRefs>
 
 <!--OOB Fields-->
 
 <pnp:FieldRef ID="c042a256-787d-4a6f-8a8a-cf6ab767f12d" Name="ContentType" />
 
 <pnp:FieldRef ID="fa564e0f-0c70-4ab9-b863-0177e6ddd247" Name="Title" Hidden="true" />
 
 <!--Custom Fields-->
 
 <pnp:FieldRef ID="316281FD-F612-4798-8D96-350EC8E511D8" Name="ClientCode" />
 
 </pnp:FieldRefs>
 
 </pnp:ContentType>
 
 </pnp:ContentTypes>
 
 <pnp:Lists>
 
 <pnp:Lists>
 
 <!--Clients List-->
 
 <pnp:ListInstance Title="Clients" 
 
 Description="Master list of the Clients" 
 
 OnQuickLaunch="true" 
 
 TemplateType="100" 
 
 Url="Lists/Clients" 
 
 MinorVersionLimit="0" 
 
 MaxVersionLimit="0" 
 
 DraftVersionVisibility="0" 
 
 RemoveExistingContentTypes="true" 
 
 ContentTypesEnabled="true" 
 
 EnableFolderCreation="false">
 
 <pnp:ContentTypeBindings>
 
 <pnp:ContentTypeBinding ContentTypeID="0x0100F96F997E5680487B9F800DCEC2901F1D" Default="true" />
 
 </pnp:ContentTypeBindings>
 
 <pnp:Views RemoveExistingViews="true">
 
 <View Name="{54C0247A-CFE2-429C-957A-8B2AF031AE66}" 
 
 DefaultView="TRUE" 
 
 Type="HTML" 
 
 DisplayName="Clients" 
 
 Url="/sites/SOWTracker/Lists/Clients/Clients.aspx" 
 
 Level="1" 
 
 BaseViewID="1" 
 
 ContentTypeID="0x" 
 
 ImageUrl="/_layouts/15/images/generic.png?rev=44">
 
 <Query>
 
 <OrderBy>
 
 <FieldRef Name="ID" />
 
 </OrderBy>
 
 </Query>
 
 <ViewFields>
 
 <FieldRef Name="ClientCode" />
 
 </ViewFields>
 
 <RowLimit Paged="TRUE">30</RowLimit>
 
 <JSLink>clienttemplates.js</JSLink>
 
 </View>
 
 </pnp:Views>
 
 <pnp:FieldRefs>
 
 <pnp:FieldRef ID="316281fd-f612-4798-8d96-350ec8e511d8" Name="ClientCode" DisplayName="Client Code" />
 
 </pnp:FieldRefs>
 
 </pnp:ListInstance>
 
 </pnp:Lists>
 
 </pnp:ProvisioningTemplate>
 


On the above XML, the column ClientCode is referred in a content type and a list got created using that content type. The lookup column which refers the client code from the clients list can also be created using the XML itself. PNP is enough intelligent to create the site column Clientcode, then create the Content Type and then create the List. Now, come back to the site columns section again and create the lookup column Clientcode Lookup, so that, the column referred does not break anything.

Happy Coding,

Sathish Nadarajan.

Categories