Forum Discussion
Add to PnP Provisioning template using CSOM
- Aug 03, 2017
OK, see the changes I've made below.
You should implement FindDocumentLibraries to get the document libraries in your site. I think you can either use list.RemoveExistingContentTypes or remove the existing Content Type through a ContentTypeBinding (both included in my code below).
In addition to everything required, you should also initialize the list variable further to reflect your existing Document Library's configuration to prevent undesired changes after applying your template.
var documentLibraries = FindDocumentLibraries(); var template = new ProvisioningTemplate(); foreach (var documentLibrary in documentLibraries) { var list = new ListInstance();
list.RemoveExistingContentTypes = true;
var listContentTypeToAdd = new ContentTypeBinding(); listContentTypeToAdd.Default = true; listContentTypeToAdd.ContentTypeId = "0x01010003860C260AF340CB8B30D74959AB0FC4" // Custom Document var listContentTypeToRemove = new ContentTypeBinding(); listContentTypeToRemove.Remove = true; listContentTypeToRemove.ContentTypeId = "0x0101"; // Document list.ContentTypeBindings.Add(listContentTypeToAdd); list.ContentTypeBindings.Add(listContentTypeToRemove); template.Lists.Add(list); }Hope this helps!
OK, see the changes I've made below.
You should implement FindDocumentLibraries to get the document libraries in your site. I think you can either use list.RemoveExistingContentTypes or remove the existing Content Type through a ContentTypeBinding (both included in my code below).
In addition to everything required, you should also initialize the list variable further to reflect your existing Document Library's configuration to prevent undesired changes after applying your template.
var documentLibraries = FindDocumentLibraries();
var template = new ProvisioningTemplate();
foreach (var documentLibrary in documentLibraries)
{
var list = new ListInstance();
list.RemoveExistingContentTypes = true;
var listContentTypeToAdd = new ContentTypeBinding();
listContentTypeToAdd.Default = true;
listContentTypeToAdd.ContentTypeId = "0x01010003860C260AF340CB8B30D74959AB0FC4" // Custom Document
var listContentTypeToRemove = new ContentTypeBinding();
listContentTypeToRemove.Remove = true;
listContentTypeToRemove.ContentTypeId = "0x0101"; // Document
list.ContentTypeBindings.Add(listContentTypeToAdd);
list.ContentTypeBindings.Add(listContentTypeToRemove);
template.Lists.Add(list);
}Hope this helps!
- Piero NegriMar 16, 2018Copper Contributor
is there some general documentation to use CSOM with PNP engine, thus updating the template at run time?
Paul Quirk wrote:
That's awesome! Thanks.