Monday, January 23, 2017

PnP Core Provisioning: Part 1

Microsoft Patterns and Practices group has developed a deployment tool for SharePoint called PnP Core that allows you to provision structure, data, settings, security, and artifacts to a SharePoint site from an XML input file.  Getting started is relatively easy.  You just need to create a Console Application and include the SharePointPnPCore2013 NuGet package:
SharePointPnPCore2013 Nuget package
and update the Newtonsoft.Json package to 9.0.1 because that is required for the NuGet package:
Update Newtonsoft.Json package
Make sure to get the correct version, online or on-prem, 2013 or 2016.  You can always check by tacking _vti_pvt/service.cnf to the end of your root site address.  If you get:
vti_encoding:SR|utf8-nl vti_extenderversion:SR|15.0.0.4841 
it is SharePoint 2013, if you get:
            vti_encoding:SR|utf8-nl
            vti_extenderversion:SR|16.0.0.4327
it is SharePoint 2016.

The code to pull a template and store it as an XML file is as simple as:
    string fileName = "template.xml";
    ProvisioningTemplateCreationInformation templateCreationInformation 
        = new ProvisioningTemplateCreationInformation(web);
    var provisioningTemplate 
        = web.GetProvisioningTemplate(templateCreationInformation);
    System.IO.File.WriteAllText(
        string.Format(@".\{0}", fileName), provisioningTemplate.ToXML()
        );

I have a full sample console application source if you want to see it all.  I will get into some more interesting PnP code tomorrow, and talk about what the XML contains and how to build your own.

Keep coding!

No comments:

Post a Comment