PnP- Example using XMLSharePointTemplateProvider , CSOM
I was working
on PnP site provisioning, and looking for an example for using the
PnP XMLSharePointTemplateProvider class. No documentation or example I found in detail.
Problem was:
In all the examples/samples code written or get
on git is follow the sequence of getting the web template and apply it to new. Function
used are GetProvisioningTemplate and followed by ApplyProvisioningTemplate.
I was having scenario like, I extracted the XML template once, stored at
central location on my SharePoint Online tenant in document library. Wanted to
reuse the same template for the every time when user runs the site provisioning.
(Technically skip calling the GetProvisioningTemplate step)
After digging into the PnP.core framework code, found some test
cases related to XMLSharePointTemplateProvider that gave
some detail about how and what parameters need to pass to achieve this
scenario.
Solution:
private static ProvisioningTemplate GetSharePointTemplate()
{
//TODO: Check null if template not exists at the document library then call the GetProvisioningTemplate()
string resourceWebUrl = "<your site url>";
string username = "<your user name>";
string password = "<password>";
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
var credentials = new SharePointOnlineCredentials(username, secureString);
using (var ctx = new ClientContext(resourceWebUrl))
{
ctx.Credentials = new SharePointOnlineCredentials(username, secureString);
XMLSharePointTemplateProvider provider =
new XMLSharePointTemplateProvider(ctx,
resourceWebUrl,
"Templates"); //Pass document library name.
var result = provider.GetTemplate("template.xml"); // pass the name of your template
return result;
}
}
Usage:
var template= GetSharePointTemplate();
// APPLY the template to new site call PnP ApplyProvisioningTemplate()
ApplyProvisioningTemplate(defaultForeground, targetWebUrl, username, secureString, template);
I hope this example will save your time.
I am getting error object reference not set to an instance of an object when calling GetTemplate() method
ReplyDeleteNice blogs it`s better to select tool wisely to do hassle-free migrations which boost our productivity too... give a try SharePoint Migration Tool
ReplyDelete