SOLVED

Add to PnP Provisioning template using CSOM

Copper Contributor

I am currently creating a console application using the PnP provisioning engine.

 

I'd like to be able to add a list to the template in memory using CSOM as per this example

 

template.Lists.Add(new ListInstance()
            {
                Title = "PnP Sample Contacts",
                Url = "lists/PnPContacts",
                TemplateType = (Int32)ListTemplateType.Contacts,
                EnableAttachments = true
            });

What I can't figure out is how to do the content type bindings.

5 Replies

Something like below should work. You'd still have to initialize the objects to reflect your requirements  and required values off course...

 

var template = new ProvisioningTemplate();
var list = new ListInstance();
var listContentTypeBinding = new ContentTypeBinding();

list.ContentTypeBindings.Add(listContentTypeBinding);

template.Lists.Add(list);

 

 

Thanks. The specific bit I'm having trouble with is in the ContentTypeBinding how to define the ContentTypeId, whether it's the default one or whether the content type is to be removed.

 

The general aim is for a document library, I want to remove the Document content type and add a custom document content type. I have this done in Xml that works for any known document libraries in a site, but I'd like to be able to check what document libraries are in a site and then add them to the template while it's in memory so I can apply a standard configuration.

best response confirmed by Paul Quirk (Copper Contributor)
Solution

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!

That's awesome! Thanks.

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.

 

1 best response

Accepted Solutions
best response confirmed by Paul Quirk (Copper Contributor)
Solution

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!

View solution in original post