Buy Me a Coffee

Buy Me a Coffee!

Friday, January 27, 2017

PnP Core Provisioning: Part 4

The PnP.Core template is more versatile than the built in SharePoint template because you can composite templates on the same site.  By mixing and matching lists and features, you can create a custom solution from component parts.  You can also create a data only template that will populate data for your QA team and only apply it into QA.  The ability to generate a template containing data is not built into the PnP.Core library because the team responsible for it don't want to have it thought of as a backup tool.  That doesn't mean that we can't build something ourselves to do it.
For now, let's just look at how we would manually create a template to just add data.  There are actually two ways, one through XML and the other through code.  First, let's look at the XML option.  Here is the template for a simple list for holding countries.  Now, before anyone starts ranting about it, yes, I know that it would be better in most cases to store such flat/static data in a Term Store.  No, I am not going to show how to provision a term store through the template yet.  Here is the XML:


    <pnp:ListInstance
           Title="CountryList"
           Description=""
           DocumentTemplate=""
           TemplateType="100"
           Url="Lists/CountryList"
           MinorVersionLimit="0"
           MaxVersionLimit="0"
           DraftVersionVisibility="0"
           TemplateFeatureID="00bfea71-de22-43b2-a848-c05709900100"
           EnableFolderCreation="false">
       <pnp:ContentTypeBindings>
         <pnp:ContentTypeBinding ContentTypeID="0x01" Default="true" />
         <pnp:ContentTypeBinding ContentTypeID="0x0120" />
       </pnp:ContentTypeBindings>
       <pnp:DataRows>
         <pnp:DataRow>
           <pnp:DataValue FieldName="Title">Canada</pnp:DataValue>
         </pnp:DataRow>
         <pnp:DataRow>
           <pnp:DataValue FieldName="Title">UK</pnp:DataValue>
         </pnp:DataRow>
         <pnp:DataRow>
           <pnp:DataValue FieldName="Title">US</pnp:DataValue>
         </pnp:DataRow>
       </pnp:DataRows>
     </pnp:ListInstance>
This will create a default list of items and populate three rows.  They are just added within the pnp:DataRows section.

The other way is to create a data filled provisioning template is to create an instance of the ProvisioningTemplate object and add a ListInstance filled with DataRow(s).  Once you have populated the collection you can export it to XML.   Here is some sample code that creates a list named Test List and populates one row of data:


using OfficeDevPnP.Core.Framework.Provisioning.Model;
using System.Collections.Generic;

namespace GenerateAPopulateDataTemplate
{
    class Program
    {
        static void Main(string[] args)
        {
            var fileName = "testTemplate.xml";
            ProvisioningTemplate provisioningTemplate
                = new ProvisioningTemplate();
            ListInstance list = new ListInstance();
            list.Title = "Test List";

            var data = new Dictionary<string, string>();
            data.Add("Title", "First Item");
            list.DataRows.Add(new DataRow(data));
            provisioningTemplate.Lists.Add(list);
            System.IO.File.WriteAllText(
                string.Format(@".\{0}", fileName),
                provisioningTemplate.ToXML());
        }
    }
}
Here is the XML templated created by running the code above:



<pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2016/05/ProvisioningSchema">
  <pnp:Preferences Generator="OfficeDevPnP.Core, Version=2.8.1610.1, Culture=neutral, PublicKeyToken=null" />
  <pnp:Templates ID="CONTAINER-">
    <pnp:ProvisioningTemplate Version="0">
      <pnp:Lists>
        <pnp:ListInstance Title="Test List" TemplateType="0" MinorVersionLimit="0" MaxVersionLimit="0" DraftVersionVisibility="0">
          <pnp:DataRows>
            <pnp:DataRow>
              <pnp:DataValue FieldName="Title">First Item</pnp:DataValue>
            </pnp:DataRow>
          </pnp:DataRows>
        </pnp:ListInstance>
      </pnp:Lists>
    </pnp:ProvisioningTemplate>
  </pnp:Templates>
</pnp:Provisioning>


Happy coding!