Programmatically Create Taxonomy Record #16680
-
Good morning, We were wondering if there is a way to programmatically create taxonomy with C# in the application server side (from a custom module), other than the UI? Here's the use case we're running into on my team: We have an API that is allowing us to create our main content item in orchard core. Our structure is fairly complex so the API serves as a gateway for bulk creation from other sources. Our content item has categories represented as taxonomy (Additionally a category can have sub categories) that can get associated to that content item. We wanted to give the ability to let new categories coming in with the content item structure from the API create the new taxonomy records and associate everything correctly, but haven't been successful in doing so. If the category is created first then everything works as expected. We tried digging through some of the source code but couldn't really find any helpers or services for doing this specifically, and it looks like some of the taxonomy creation stuff happens through shape creation on the admin controller in the taxonomy module, but that's a bit out of my depth so I could be off base there. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Are you trying to create the taxonomy itself or just adding terms to an existing taxonomy? What you are describing makes it sound like you are just trying to add additional terms to an existing taxonomy programmatically. For instance you have a taxonomy called Categories and you are just trying to add Categories (terms) to that taxonomy. |
Beta Was this translation helpful? Give feedback.
-
I think maybe just adding terms to the existing, but those terms are new. So keeping with the example, where the taxonomy Is categories, we have top level ones that get created at /Admin/Taxonomies/Create/{id}?taxonomyContentItemId={contentItemId} from the UI, and then if sub terms are added to those, they also have the taxonomyItemId appended in the query string. Categories
The API request would have the field/s for that taxonomy category and a new entry say "Specials" that we would want to create a new top level term (?). Thanks for the reply, I hope the additional information is helpful or if I can provide anything else. |
Beta Was this translation helpful? Give feedback.
You could look at the TagController to get an idea on how to create terms for existing taxonomies. You would probably need your own controller since this built in controller is using the [Admin] attribute, but this should get you started on how to do this as this is how the admin ui does it.
OrchardCore/src/OrchardCore.Modules/OrchardCore.Taxonomies/Controllers/TagController.cs
Line 45 in ed706e9
Terms are just content items, so you can
Alter
the content item with whatever values you need to handle things like "Specials"