Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field to content type throws error content type has no children [BUG] #1092

Closed
1 task done
pvranich opened this issue Sep 10, 2021 · 2 comments · Fixed by #1992
Closed
1 task done

Add field to content type throws error content type has no children [BUG] #1092

pvranich opened this issue Sep 10, 2021 · 2 comments · Fixed by #1992
Labels
bug Something isn't working

Comments

@pvranich
Copy link

pvranich commented Sep 10, 2021

Hi All, I will start but saying I think this is the right place to log the issue. If it should go in the pnp framework issues please let me know and i will move it there.

Reporting an Issue or Missing Feature

Ultimately the issue is when trying to add a list field\column to a list content type I get the following error

The content type has no children.

Cmdlet called is Add-PnPFieldToContentType

Now that being said, I have two content types in my library and it consistently adds the field to one content type, which happens to be the default content type, but consistently fails when trying to add the field to any other subsequent content types with the message above.

Neither content types have any children. So not really sure why it is even trying to do that and 2 it still works on the default content type even though it has no children.

There is no parameter on the cmdlet to updatechildren $true$false so I am guessing this is always set to $true? If I look at the source code this seems to be the case.

Expected behavior

The field is successfully added to the content type.

Actual behavior

When calling Add-PnPFieldToContentType with a field object it fails with the message The content type has no children.
and the field is not added to the content type.

Steps to reproduce behavior

Create two site content types, ContentTypeA and ContentTypeB, inheriting from Document and add both these content types to a library. Remove the Document content type from the library. Set ContentTypeA as the default content type.

I have a collection of fields and their schema xml that I am trying to add to the list and then to the content types. It doesn't matter what sort of field. for example:

$fieldSchemaXml = '<Field Type="Currency" DisplayName="ACurrencyField" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" LCID="3081" ID="{550c35a9-f880-454a-b2f0-cf0fa62de8bf}" SourceID="{18eea96c-3bb4-4c98-bfd7-7d5b1fdb63c0}" StaticName="ACurrencyField" Name="ACurrencyField" ColName="float2" RowOrdinal="0" />'

$connection = Conntect-PnPOnline -ReturnConnection .....
$destinationList = Get-PnPList -Connection $connection -Identity 'PnPIssueTest';
$field = Get-PnPField -Connection $connection -List $destinationList -Identity 'ACurrencyField' -ErrorAction SilentlyContinue;

if ($null -eq $field) {
     $field = Add-PnPFieldFromXml -Connection $connection -List $destinationList -FieldXml $fieldSchemaXml -ErrorAction Stop;
}
# assume here that ContentTypeA is the default content type
$contentTypeA = Get-PnPContentType -Connection $connection -List $destinationList -Identity 'ContentTypeA';
$contentTypeB = Get-PnPContentType -Connection $connection -List $destinationList -Identity 'ContentTypeB';

#This is the default content type and this works
Add-PnPFieldToContentType -Connection $connection -Field $field -ContentType $contentTypeA; 

#This fails
Add-PnPFieldToContentType -Connection $connection -Field $field -ContentType $contentTypeB;

What is the version of the Cmdlet module you are running?

1.7.0

Which operating system/environment are you running PnP PowerShell on?

  • Windows
@pvranich pvranich added the bug Something isn't working label Sep 10, 2021
@pvranich
Copy link
Author

pvranich commented Sep 10, 2021

Continuing on I just tried this thinking that maybe it had something to do with it being the default content type but this didn't work either

Set-PnPDefaultContentTypeToList -List $destinationList -ContentType $contentTypeB;

Add-PnPFieldToContentType -Connection $connection -Field $field -ContentType $contentTypeB;

I have since run another test, whereby I created a new document library in the same site collection PnPIssueTest2 and added the site content type ContentTypeB to the library first and then added ContentTypeA ran the same script as before and it successfully added the field to ContentTypeB but not ContentTypeA. ContentTypeA is still the default content type.

$fieldSchemaXml = '<Field Type="Currency" DisplayName="ACurrencyField" Required="FALSE" EnforceUniqueValues="FALSE" Indexed="FALSE" LCID="3081" ID="{550c35a9-f880-454a-b2f0-cf0fa62de8bf}" SourceID="{18eea96c-3bb4-4c98-bfd7-7d5b1fdb63c0}" StaticName="ACurrencyField" Name="ACurrencyField" ColName="float2" RowOrdinal="0" />'

$connection = Connect-PnPOnline -ReturnConnection ....;
$destinationList = Get-PnPList -Connection $connection -Identity 'PnPIssueTest2';
$field = Get-PnPField -Connection $connection -List $destinationList -Identity 'ACurrencyField' -ErrorAction SilentlyContinue;

if ($null -eq $field) {
     $field = Add-PnPFieldFromXml -Connection $connection -List $destinationList -FieldXml $fieldSchemaXml -ErrorAction Stop;
}
# assume here that ContentTypeA is the default content type
$contentTypeA = Get-PnPContentType -Connection $connection -List $destinationList -Identity 'ContentTypeA';
$contentTypeB = Get-PnPContentType -Connection $connection -List $destinationList -Identity 'ContentTypeB';

#This fails. Added second to the library but is still the default content type.
Add-PnPFieldToContentType -Connection $connection -Field $field -ContentType $contentTypeA; 

#This works
Add-PnPFieldToContentType -Connection $connection -Field $field -ContentType $contentTypeB;

gautamdsheth added a commit to gautamdsheth/powershell that referenced this issue Jun 16, 2022
@gautamdsheth
Copy link
Collaborator

Hi @pvranich , have added additional parameter -UpdateChildren to this cmdlet.
This allows users to control the pushing of fields to child content types.
It will be available in tomorrow's nightly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants