pnp / PnP-Sites-Core

Microsoft 365 Dev PnP Core component (.NET) targeted for increasing developer productivity with CSOM based solutions.
Other
415 stars 642 forks source link

CollectionNotInitializedException when using ContentTypeBinding to a custom content type #2580

Open Rafaelki opened 4 years ago

Rafaelki commented 4 years ago

Category

[x] Bug [ ] Enhancement

Environment

[x] Office 365 / SharePoint Online [ ] SharePoint 2016 [ ] SharePoint 2013

Expected or Desired Behavior

When running ApplyProvisioningTemplate with a template that has a ContentTypeBinding I expect the library to be created with the specified content type.

Observed Behavior

ApplyProvisioningTemplate throws the following exception: Microsoft.SharePoint.Client.CollectionNotInitializedException: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. if the template contains a ListInstance with a ContentTypeBinding of a custom content type.

I can confirm that this exception is happening with the last 3 versions (3.18, 3.19 and 3.20) and that it was not happening with 3.15. Also, the template is correctly applied using PnP Powershell (3.15, 3.18, 3.19 and 3.20).

Steps to Reproduce

Run site.RootWeb.ApplyProvisioningTemplate(template) with a template containing this element:

<pnp:ListInstance Title="Documents" Description="" DocumentTemplate="{site}/Shared Documents/Forms/template.dotx" OnQuickLaunch="true" TemplateType="101" Url="Shared Documents" EnableVersioning="true" MinorVersionLimit="0" MaxVersionLimit="500" DraftVersionVisibility="0" TemplateFeatureID="00bfea71-e717-4e80-aa17-d0c71b360101" ContentTypesEnabled="true" EnableAttachments="false" DefaultDisplayFormUrl="{site}/Shared Documents/Forms/DispForm.aspx" DefaultEditFormUrl="{site}/Shared Documents/Forms/EditForm.aspx" DefaultNewFormUrl="{site}/Shared Documents/Forms/Upload.aspx" ImageUrl="/_layouts/15/images/itdl.png?rev=44" IsApplicationList="false" ValidationFormula="" ValidationMessage="">
  <pnp:ContentTypeBindings>
    <pnp:ContentTypeBinding ContentTypeID="0x01010036DF8ACB058DF9489700B19236BAD594" Default="true" />
    <pnp:ContentTypeBinding ContentTypeID="0x0120" />
  </pnp:ContentTypeBindings>
</pnp:ListInstance>

Replace "0x01010036DF8ACB058DF9489700B19236BAD594" with a custom content type ID existing in the site.

ghost commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

Denford29 commented 4 years ago

I am also having this issue with a template that has got a few content types, is there any update on this and if it helps the default content types work fine its only custom ones i.e. <pnp:ContentTypeBinding ContentTypeID="0x0101" Default="true" /> <pnp:ContentTypeBinding ContentTypeID="0x0120" />

0x0101 = document and 0x0120 = folder

Denford29 commented 4 years ago

Managed to find a fix/work around for this, so basically took out the custom content types out of the provisioning template and left the default SharePoint ones only in the template.

After the site is created then i basically run a manual process to add my custom content types after, see example code below

`List newSiteList; //add the custom content types to an array var contentTypeIds = new[] { "0x01010015DCFFB4AD974BC99CD2CC41540EA57A03" };

//go through these and add them manually foreach (var contentTypeId in contentTypeIds) { //get the content type by id var contentType = clientContext.Web.ContentTypes.GetById(contentTypeId); //check if we have the content type if (contentType != null) { //add the content type to the current folder newSiteList.ContentTypes.AddExistingContentType(contentType); } }

//commit the content types to the list clientContext.ExecuteQuery();`