Auto Attach the DashboardPart to content types with DashboardWidget stereotype, how to add import/export to an ASP.NET Core app - This week in Orchard (29/11/2024)

This week's topics include auto-attaching the DashboardPart to content types with the DashboardWidget stereotype and supporting randomly ordering results in SQL queries. Don't forget to check out our newest Orchard Nuggets post about adding import/export to an ASP.NET Core app! Let's see the details!

Orchard Core updates

Auto Attach the DashboardPart to content types with DashboardWidget stereotype

In Orchard Core 2.1, we've introduced the IContentDefinitionHandler interface, which allows you to intercept and modify content definitions as they are being built before being cached. This provides finer control over the creation of content types, enabling customization at various stages of the content definition process.

The following events are available for handling different aspects of content definition:

  • ContentTypeBuilding(ContentTypeBuildingContext context): Triggered when creating a content type.
  • ContentPartBuilding(ContentPartBuildingContext context): Triggered during the creation of content part definitions.
  • ContentTypePartBuilding(ContentTypePartBuildingContext context): Triggered while constructing a content type part.
  • ContentPartFieldBuilding(ContentPartFieldBuildingContext context): Triggered when building fields within a content part.

These event handlers allow you to adjust definitions at each step, providing excellent customization capabilities.

The new events mentioned above enable scenarios where you can inject content parts into content definitions programmatically. These parts are designated as System-Type, meaning users cannot remove or modify them through the UI or recipes. For an example of injecting a System-Type part into a content definition, refer to the DashboardPartContentTypeDefinitionHandler class. This ensures that specific content parts are always present in the content definition, maintaining a consistent structure across the application.

This also means that creating Admin Widgets no longer requires DashboardPart. Thanks to the newly introduced System-Type capability, attaching DashboardPart to an admin dashboard is no longer necessary. To create a new admin dashboard content type, assign the DashboardWidget stereotype to the content type. This streamlined process reduces complexity while ensuring that admin widgets are properly categorized without additional manual configuration. Don't forget that the DashboardPart will no longer be available as an attachable part by default.

Here, you can see the ContentTypeBuilding method of the DashboardPartContentTypeDefinitionHandler class. As you can read, it adds the DashboardPart to the content type definition when the stereotype is set to DashboardWidget.

The new DashboardPartContentTypeDefinitionHandler

Support ordering results in SQL queries randomly

Previously, SQL Query with random ordering fails with the following error message: Syntax error, expected: ASC, DESC, ., ,, ), LIMIT, OFFSET, UNION, ;, WITH, SELECT at line:5, col:15. Random( ) in SQL is generally used to return a random row from a table present in the database.

But from now on, the order by clauses can also use the random() function (case-insensitive) to order results randomly, e.g., SELECT * FROM ContentItemIndex ORDER BY random().

To check this out quickly, you must enable the SQL Queries feature under Configuration -> Features. Once enabled, you will see a Run SQL option under Search -> Queries. We set up our site using the Blog recipe, which means we have a BlogPost content type. We created some BlogPost content items to construct a query that returns the DocumentId from the ContentItemIndex table for every published blog post in random order. As shown on the screen below, the DocumentIds are displayed in random order.

Support ordering results in SQL queries randomly

News from the community

Orchard Nuggets: How to add import/export to an ASP.NET Core app

When working with an ASP.NET Core application, it’s often necessary to move various content or configurations between different environments or back up critical information. In our newest Orchard Nuggets post, we will show you how to use the Deployment feature of Orchard Core, which can simplify this process. By checking this post, you can read about the features related to deployment, like the Remote Deployment feature or the View or Download Content as JSON feature. You can read about deployment plans and how to use them or say which parts or contents of the site you want to export. And a lot more, of course!

Orchard Nuggets how to add import and export to an ASP.NET Core App

Remember to check out the other posts for more bite-sized Orchard tips, and let us know if you have another question!

Orchard Dojo Newsletter

Lombiq's Orchard Dojo Newsletter has 466 subscribers! We started this newsletter to inform the community around Orchard of the latest news about the platform. By subscribing to this newsletter, you will receive an email whenever a new post is published in Orchard Dojo, including This week in Orchard.

Do you know other Orchard enthusiasts who want to read our weekly articles? Tell them to subscribe here!

If you are interested in more news about Orchard and the details of the topics above, don't forget to check out the recording of this Orchard meeting!