SOLVED
Home

Add to PnP Provisioning template using CSOM

%3CLINGO-SUB%20id%3D%22lingo-sub-92821%22%20slang%3D%22en-US%22%3EAdd%20to%20PnP%20Provisioning%20template%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-92821%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20currently%20creating%20a%20console%20application%20using%20the%20PnP%20provisioning%20engine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'd%20like%20to%20be%20able%20to%20add%20a%20list%20to%20the%20template%20in%20memory%20using%20CSOM%20as%20per%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-Guidance%2Fblob%2Fmaster%2Farticles%2Fprovisioning-console-application-sample.md%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ethis%20example%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Etemplate.Lists.Add(new%20ListInstance()%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Title%20%3D%20%22PnP%20Sample%20Contacts%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Url%20%3D%20%22lists%2FPnPContacts%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TemplateType%20%3D%20(Int32)ListTemplateType.Contacts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20EnableAttachments%20%3D%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%3B%3C%2FPRE%3E%3CP%3EWhat%20I%20can't%20figure%20out%20is%20how%20to%20do%20the%20content%20type%20bindings.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-92821%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECSOM%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eprovisioning%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-172634%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20to%20PnP%20Provisioning%20template%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-172634%22%20slang%3D%22en-US%22%3E%3CP%3Eis%20there%20some%20general%20documentation%20to%20use%20CSOM%20with%20PNP%20engine%2C%20thus%20updating%20the%20template%20at%20run%20time%3F%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F32412%22%20target%3D%22_blank%22%3E%40Paul%20Quirk%3C%2FA%3E%20wrote%3A%3CBR%20%2F%3EThat's%20awesome!%20Thanks.%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-93297%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20to%20PnP%20Provisioning%20template%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93297%22%20slang%3D%22en-US%22%3EThat's%20awesome!%20Thanks.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-93227%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20to%20PnP%20Provisioning%20template%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93227%22%20slang%3D%22en-US%22%3E%3CP%3EOK%2C%20see%20the%20changes%20I've%20made%20below.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20should%20implement%20FindDocumentLibraries%20to%20get%20the%20document%20libraries%20in%20your%20site.%20I%20think%20you%20can%20either%20use%20list.RemoveExistingContentTypes%20or%20remove%20the%20existing%20Content%20Type%20through%20a%20ContentTypeBinding%20(both%20included%20in%20my%20code%20below).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20addition%20to%20everything%20required%2C%20you%20should%20also%20initialize%20the%20list%20variable%20further%20to%20reflect%20your%20existing%20Document%20Library's%20configuration%20to%20prevent%20undesired%20changes%20after%20applying%20your%20template.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20documentLibraries%20%3D%20FindDocumentLibraries()%3B%0Avar%20template%20%3D%20new%20ProvisioningTemplate()%3B%0A%0Aforeach%20(var%20documentLibrary%20in%20documentLibraries)%0A%7B%0A%20%20%20%20%20var%20list%20%3D%20new%20ListInstance()%3B%3CBR%20%2F%3E%20%20%20%20%20list.RemoveExistingContentTypes%20%3D%20true%3B%3CBR%20%2F%3E%0A%20%20%20%20%20var%20listContentTypeToAdd%20%3D%20new%20ContentTypeBinding()%3B%0A%20%20%20%20%20listContentTypeToAdd.Default%20%3D%20true%3B%0A%20%20%20%20%20listContentTypeToAdd.ContentTypeId%20%3D%20%220x01010003860C260AF340CB8B30D74959AB0FC4%22%20%2F%2F%20Custom%20Document%0A%0A%20%20%20%20%20var%20listContentTypeToRemove%20%3D%20new%20ContentTypeBinding()%3B%0A%20%20%20%20%20listContentTypeToRemove.Remove%20%3D%20true%3B%0A%20%20%20%20%20listContentTypeToRemove.ContentTypeId%20%3D%20%220x0101%22%3B%20%2F%2F%20Document%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%20%20%20%20%20list.ContentTypeBindings.Add(listContentTypeToAdd)%3B%0A%20%20%20%20%20list.ContentTypeBindings.Add(listContentTypeToRemove)%3B%0A%0A%20%20%20%20%20template.Lists.Add(list)%3B%0A%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3BHope%20this%20helps!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-93184%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20to%20PnP%20Provisioning%20template%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-93184%22%20slang%3D%22en-US%22%3E%3CP%3EThanks.%20The%20specific%20bit%20I'm%20having%20trouble%20with%20is%20in%20the%20ContentTypeBinding%20how%20to%20define%20the%20ContentTypeId%2C%20whether%20it's%20the%20default%20one%20or%20whether%20the%20content%20type%20is%20to%20be%20removed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20general%20aim%20is%20for%20a%20document%20library%2C%20I%20want%20to%20remove%20the%20Document%20content%20type%20and%20add%20a%20custom%20document%20content%20type.%20I%20have%20this%20done%20in%20Xml%20that%20works%20for%20any%20known%20document%20libraries%20in%20a%20site%2C%20but%20I'd%20like%20to%20be%20able%20to%20check%20what%20document%20libraries%20are%20in%20a%20site%20and%20then%20add%20them%20to%20the%20template%20while%20it's%20in%20memory%20so%20I%20can%20apply%20a%20standard%20configuration.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-92885%22%20slang%3D%22en-US%22%3ERe%3A%20Add%20to%20PnP%20Provisioning%20template%20using%20CSOM%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-92885%22%20slang%3D%22en-US%22%3E%3CP%3ESomething%20like%20below%20should%20work.%20You'd%20still%20have%20to%20initialize%20the%20objects%20to%20reflect%20your%20requirements%20%26nbsp%3Band%20required%20values%20off%20course...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20template%20%3D%20new%20ProvisioningTemplate()%3B%0Avar%20list%20%3D%20new%20ListInstance()%3B%0Avar%20listContentTypeBinding%20%3D%20new%20ContentTypeBinding()%3B%0A%0Alist.ContentTypeBindings.Add(listContentTypeBinding)%3B%0A%0Atemplate.Lists.Add(list)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Paul Quirk
Occasional 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.

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.

 

Related Conversations
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
PacketMon Components are not loading in WAC 1909
HotCakeX in Windows Admin Center on
2 Replies