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

Cannot Create Taxonomy Programmatically from Migration #17510

Closed
fuzl-llc opened this issue Feb 22, 2025 · 2 comments
Closed

Cannot Create Taxonomy Programmatically from Migration #17510

fuzl-llc opened this issue Feb 22, 2025 · 2 comments
Labels

Comments

@fuzl-llc
Copy link
Contributor

I'm trying to create a taxonomy programatically during a migration based on code I saw in issue #3721 where @barthamark used the following code:

public async Task<int> CreateAsync()
{
            ...
            var taxonomy = await _contentManager.NewAsync("Taxonomy");
            taxonomy.DisplayText = "My Taxonomy";
            var taxonomyPart = taxonomy.As<TaxonomyPart>(); // taxonomyPart is null for me
            taxonomyPart.TermContentType = "MyTermType";
            taxonomy = taxonomy.Apply(taxonomyPart);
            await _contentManager.CreateAsync(taxonomy, VersionOptions.Published);
            ...

It was throwing an exception but there was some new code at the time (about 5 years ago) and apparently then it just started working. A couple experienced Orchard develoeprs, including @sebastienros, commented on that post so I assumed it was an ok approach... at least at that time, again, ~5 years ago. The above code doesn't work for me. On the taxonomy.As() line, taxonomyPart is null for me. Sure enough, when I look at the content definitions, the TaxonomyPart is not added to the Taxonomy content type. Has something changed or am I just missing something? Should the above work?

Orchard Core version

2.1.5

To Reproduce

Create a new Orchard Core site.

Expected behavior

Taxonomy has TaxonomyPart.

@barthamark
Copy link
Contributor

Do you have the Taxonomies feature enabled? Is it set as a dependency to your module/feature in the Manifest.cs file? Maybe the Taxonomies feature is enabled later in the pipeline and that's where the part would be added; adding it as a dependency might fix it if it's not already there.

@fuzl-llc
Copy link
Contributor Author

Wow. @barthamark thank you so much! That was it. I did have taxonomies enabled but my manifest dependency was wrong. I had brilliantly put in the singlular Taxonomy instead of plural Taxonomies. I guess it is time to create some constants for those strings... or maybe I'll just start using something like $"{nameof(OrchardCore)}.{nameof(OrchardCore.Taxonomies)}" for the dependency declaration.

I've come back to this a few times over the years and could never make it work or find the right help. I didn't realize how important those dependencies in the manifest are. I always felt it was kind of weird because I already have to add the nuget OrchardCore.Taxonomies packages and the taxonomy type generally seemed to be working just fine - my workaround of just creating the taxonomy by executing a recipe as my first step had always been just fine. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants