Provision SharePoint List With Custom Site Columns And Content Type Using SharePoint Framework

SharePoint Framework is the new development model in which a lot of work has been going on since it went to General Availability on Feb 232017. It is a page and web part model that provides full support for client-side SharePoint development, easy integration with SharePoint data and support for open source tooling. With the SharePoint Framework, you can use modern web technologies and tools in your preferred development environment to build productive experiences and apps in SharePoint.

In a previous article, we saw how to set up the development environment for SharePoint Framework. We also saw the basic Hello World webpart creation and how to retrieve and display list items using SharePoint Framework. In this article, we will see how to provision SharePoint List with custom Site Columns and Content Type

We can create the directory, where we will be adding the solution, using the command given below.

md ProvisionSPList

Let’s move to the newly created working directory, using the command.

cd ProvisionSPList

SharePoint

We will then create the client Web part by running the Yeoman SharePoint Generator.

yo @microsoft/sharepoint

This will display the prompt, which we will have to fill up, so as to proceed with the project creation.

  • What is your solution name? : Set it to ‘ProvisionSPList’.

On pressing enter, we will be asked to chose the working folder for the project.

SharePoint

  • Where do you want to place your files- Use current folder.
  • What framework would you like to start with- Select “No javaScript web framework” for the time being, as this is a sample Web part.
  • What is your Webpart name- We will specify it as ‘ProvisionSPList‘ and press Enter
  • What is your Webpart description- We will specify it as this Webpart will provision SharePoint List

    SharePoint

Yeoman has started working on the scaffolding of the project. It will install the required dependencies and scaffold the solution files for the ‘ProvisionSPList’ Web part, which will take some time to complete. Once completed, we will get a congratulations message.

SharePoint

Edit the web part

Run the Code to scaffold and open the project in Visual Studio Code.

SharePoint

Now, let’s add the folder named ‘Sharepoint’ to maintain the SharePoint files that will be deployed as a package.

SharePoint

SharePoint

Within the SharePoint folder, let’s add another sub folder named Assets.

SharePoint

We will be creating two xml files - elements.xml and schema.xml. These will hold the information required to provision the site columns, content type and then use them to create the list. Let’s create the first supporting xml file elements.xml.

SharePoint

Elements.xml file will contain the list information that will be used to provision the list. At first, we will be defining the site columns using the ‘Field’ tag and then the content type that will be deployed to the site. We will also be defining the default data that will be provisioned along with the list. 

Add the Default data to SharePoint List

We will be adding the default data within the Rows tag, as shown below.

SharePoint

Elements.XML

The complete elements.xml that are used within the project is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.     <Field ID="{11ED4026-1C15-4636-80EF-C27C41DB90E0}" Name="EmployeeName" DisplayName="Employee Name" Type="Text" Required="FALSE" Group="Employee" />  
  4.     <Field ID="{1DA0BA30-F87A-4D1B-9303-729AA02BEE25}" Name="PreviousCompany" DisplayName="Previous Company" Type="Text" Required="FALSE" Group="Employee" />  
  5.     <Field ID="{145B5D00-E3AE-48EB-BB75-9699922AF8D8}" Name="JoiningDate" DisplayName="JoiningDate" Type="DateTime" Format="DateOnly" Required="FALSE" Group="Employee" />  
  6.     <Field ID="{197F8587-C417-458D-885E-4FBC28D1F612}" Name="Expertise" DisplayName="Expertise" Type="Choice" Required="FALSE" Group="Employee">  
  7.         <CHOICES>  
  8.             <CHOICE>SharePoint</CHOICE>  
  9.             <CHOICE>Java</CHOICE>  
  10.             <CHOICE>.NET</CHOICE>  
  11.             <CHOICE>Python</CHOICE>  
  12.             <CHOICE>C++</CHOICE>  
  13.             <CHOICE>Web Designer</CHOICE>  
  14.         </CHOICES>  
  15.     </Field>  
  16.     <Field ID="{10E72105-7577-4E9E-A758-BBBE8FF4E9BA}" Name="Experience" DisplayName="Experience" Group="Employee" Type="Number" Required="False" Min="0" Max="30" Percentage="FALSE"> </Field>  
  17.     <ContentType ID="0x010100FA0963FA69A646AA916D2E41284FC9D1" Name="EmployeeContentType" Group="Employee Content Types" Description="This is the Content Type for Employee Onboarding">  
  18.         <FieldRefs>  
  19.             <FieldRef ID="{11ED4026-1C15-4636-80EF-C27C41DB90E0}" />  
  20.             <FieldRef ID="{1DA0BA30-F87A-4D1B-9303-729AA02BEE25}" />  
  21.             <FieldRef ID="{145B5D00-E3AE-48EB-BB75-9699922AF8D8}" />  
  22.             <FieldRef ID="{197F8587-C417-458D-885E-4FBC28D1F612}" />  
  23.             <FieldRef ID="{10E72105-7577-4E9E-A758-BBBE8FF4E9BA}" /> </FieldRefs>  
  24.     </ContentType>  
  25.     <ListInstance CustomSchema="schema.xml" FeatureId="00bfea71-de22-43b2-a848-c05709900100" Title="Employee" Description="Employee List created using SharePoint Framework" TemplateType="100" Url="Lists/Employee">  
  26.         <Data>  
  27. <Rows>  
  28. <Row>  
  29. <Field Name="EmployeeName">Priyaranjan</Field>  
  30. <Field Name="PreviousCompany">Cognizant</Field>  
  31. <Field Name="JoiningDate">10/08/2010</Field>  
  32. <Field Name="Expertise">SharePoint</Field>  
  33. <Field Name="Experience">7</Field>  
  34. </Row>  
  35. <Row>  
  36. <Field Name="EmployeeName">Nimmy</Field>  
  37. <Field Name="PreviousCompany">SunTech</Field>  
  38. <Field Name="JoiningDate">11/04/2012</Field>  
  39. <Field Name="Expertise">Java</Field>  
  40. <Field Name="Experience">4</Field>  
  41. </Row>  
  42. <Row>  
  43. <Field Name="EmployeeName">Jinesh</Field>  
  44. <Field Name="PreviousCompany">IBM</Field>  
  45. <Field Name="JoiningDate">12/03/2006</Field>  
  46. <Field Name="Expertise">.NET</Field>  
  47. <Field Name="Experience">11</Field>  
  48. </Row>  
  49. </Rows>  
  50. </Data> </ListInstance>  
  51. </Elements>  
SharePoint

Schema.XML

Finally, we will be creating the schema.xml file which contains the list xml. Here, we will be adding the Content Type that we have declared in the elements.xml.

  1. <ContentTypes>  
  2.     <ContentTypeRef ID="0x010100FA0963FA69A646AA916D2E41284FC9D9" />   
  3. </ContentTypes>  
SharePoint

The complete schema.xml will look like below.

  1. <List xmlns:ows="Microsoft SharePoint" Title="Basic List" EnableContentTypes="TRUE" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/Basic List" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">  
  2.     <MetaData>  
  3.         <ContentTypes>  
  4.             <ContentTypeRef ID="0x010100FA0963FA69A646AA916D2E41284FC9D9" /> </ContentTypes>  
  5.         <Fields></Fields>  
  6.         <Views>  
  7.             <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">  
  8.                 <XslLink Default="TRUE">main.xsl</XslLink>  
  9.                 <JSLink>clienttemplates.js</JSLink>  
  10.                 <RowLimit Paged="TRUE">30</RowLimit>  
  11.                 <Toolbar Type="Standard" />  
  12.                 <ViewFields>  
  13.                     <FieldRef Name="LinkTitle"></FieldRef>  
  14.                     <FieldRef Name="EmployeeName"></FieldRef>  
  15.                     <FieldRef Name="PreviousCompany"></FieldRef>  
  16.                     <FieldRef Name="JoiningDate"></FieldRef>  
  17.                     <FieldRef Name="Expertise"></FieldRef>  
  18.                     <FieldRef Name="Experience"></FieldRef>  
  19.                 </ViewFields>  
  20.                 <Query>  
  21.                     <OrderBy>  
  22.                         <FieldRef Name="ID" /> </OrderBy>  
  23.                 </Query>  
  24.             </View>  
  25.         </Views>  
  26.         <Forms>  
  27.             <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />  
  28.             <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />  
  29.             <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" /> </Forms>  
  30.     </MetaData>  
  31. </List>  
SharePoint

Before we can deploy the package, we have to update the feature in the package-solution.json file.

Update Package-Solution.json

SharePoint

Initially, the file contents will contain only the solution name. We must add the feature node as well to this file.

SharePoint

Add the below content after the version tag. Here, the id is a Visual Studio created GUID that identifies a unique feature.

SharePoint

The contents of the package-solution.json will look like below.

  1. {  
  2.     "solution": {  
  3.         "name""provision-sp-list-client-side-solution",  
  4.         "id""f26589ce-0cd0-49c4-9ca3-f4a559851a0d",  
  5.         "version""1.0.0.0",  
  6.         "features": [{  
  7.             "title""provision-sp-list-client-side-solution",  
  8.             "description""provision-sp-list-client-side-solution",  
  9.             "id""7BC1C758-F2A2-4775-B26E-DC60F8620E9A",  
  10.             "version""2.0.0.0",  
  11.             "assets": {  
  12.                 "elementManifests": ["elements.xml"],  
  13.                 "elementFiles": ["schema.xml"]  
  14.             }  
  15.         }]  
  16.     },  
  17.     "paths": {  
  18.         "zippedPackage""solution/provision-sp-list.sppkg"  
  19.     }  
  20. }  

Now, let’s package the solution using gulp bundle.

Package and deploy the Solution

Now, we must package and bundle the solution.

gulp bundle

SharePoint

SharePoint

gulp package-solution

SharePoint

Thus, we are done with the packaging of the solution.

SharePoint

If we head over to the solutions folder, we can see the ‘provision-sp-list package’ which we will be uploading to SharePoint.

SharePoint

Make a note of the solution URL in the local computer as we will need it to upload to SharePoint.

SharePoint

Let’s head over to the SharePoint App Catalog site to where we will be uploading the solution.

SharePoint

Click on Upload to add the solution file to the site.

SharePoint

Click on OK to complete the upload.

SharePoint

It will ask to trust and Deploy the solution to SharePoint.

SharePoint

We can see the uploaded solution in the App Catalog.

SharePoint

Now let’s head over to the site contents and add the solution to the site.

SharePoint

On searching for the deployed app, it will list out the recently added solutions.

SharePoint

Click on it to add the solution to the site.

SharePoint

After a few seconds, we can see the newly created custom site.

SharePoint

Going inside it we can see the default data that we had added to the list.

SharePoint

The main solution files used in this section are uploaded in Microsoft TechNet gallery. Feel free to download it.

Summary

Thus, we saw how to Provision SharePoint List with custom Site Columns and Content Type using SharePoint Framework. We will see more of SharePoint Framework in action in the coming articles.

Up Next
    Ebook Download
    View all
    Learn
    View all