Creating Custom User Settings in Orchard Core CMS A tutorial on Custom User Settings for Orchard Core Developers.

Custom User Settings in Orchard Core

Custom User Settings is a relatively new feature in Orchard Core that allows you to extend a user with additional information that is stored on a per user basis, such as a user profile and user preferences. In this Orchard Core tutorial, I will describe how to use custom user settings in Orchard Core to extend each user with a simple user profile. This user profile will be used to populate an about author section at the end of each blog post with the author’s name, avatar, and bio. The same concepts can be used for more complex scenarios in e-commerce websites, customer support websites, etc.

Custom User Settings Feature in Orchard Core

As with many features in Orchard Core, custom user settings is a separate feature that must be enabled in the Orchard Core Admin. Your Orchard Core Setup Recipe may enable it for you, but to be sure, navigate to Configuration -> Features in the admin menu to enable the Custom User Settings feature.

Custom User Settings - Orchard Core

Creating a Custom Content Type for Orchard Core Custom User Settings

The Custom User Settings feature in Orchard Core works very much like the Custom Settings feature in Orchard Core. We need to create a custom content type that contains the content fields and content parts that comprise the information we wish to maintain for each user. In this case, we will be creating a content type, called UserProfile. UserProfile will consist of a media content field for an avatar, a title part for the full name or handle of the user, and an HTML body part for the bio. The content type definition is shown below.

Orchard Core Development - Custom Content Type

The content type should only set to “Versionable”. You can uncheck "Creatable", "Listable", "Draftable", and "Securable". And, most importantly, it is the stereotype of “CustomUserSettings” that differentiates this content type from others and makes it behave as custom user settings.

Orchard Core Freelance Developer - Custom User Settings Sterotype

Managing Custom User Settings in Orchard Core

As you probably suspect, custom user settings are managed on a per user basis by navigating to each user in the Orchard Core website from the Orchard Core admin. As we navigate to each user, there is a new User Profile section with the content fields and content parts we defined as part of the content definition.

Orchard Core Developer Blog User Profile

These settings will be used to populate the about author section of each blog post in our Orchard Core blog.

Orchard Core Blog Bio for Blog Post Author

It’s worth noting that we can have multiple content types with a stereotype of “CustomUserSettings” and each content type will appear as a separate section. For example, we could create a new content type, called UserPreferences, with a stereotype of “CustomUserSettings” and it would appear as such by default.

User Profile and User Preferences Orchard Core CMS

By default each section will appear as a separate tab and responsively appear as a separate card for smaller viewports. You can change the appearance by modifying the placements.json file in Orchard Core. I quite like the tabbed sections for this scenario, so let's leave it as such.

The title of each section is generated by the Display Name of the content type. Therefore, you could change the display name of the UserProfile content type to just “Profile” and UserPreferences content type to just “Preferences”, since the word “User” may be a bit redundant at this point.

Displaying Custom User Settings on the Orchard Core Website

We can access both the author and owner of each blog post. These are often the same, but let’s use the owner of the blog post as the original author.

{% assign author = Model.ContentItem.Owner | users_by_id %}

We can now access the user profile of the blog post author as such.

{% assign profile = author.Properties.UserProfile %}

The relevant information in the user profile is shown below.

{
    "ContentType": "UserProfile",
    "DisplayText": "John Doe",
    "UserProfile": {
        "Avatar": {
            "Paths": [
                "avatar.jpg"
            ]
        }
    },
    "HtmlBodyPart": {
        "Html": "Lorem ipsum, or lipsum as..."
    },
    "TitlePart": {
        "Title": "John Doe"
    }
}

As such, you can probably guess how we can access the necessary information to display the author section of the blog post using the Liquid template language. It's the same as accessing content fields and content parts in all content types. For example, to display the name of the blog post author we can access the DisplayText property.

{{ profile.DisplayText }}

Conclusion

Let's wrap up what we learned about custom user settings in this Orchard Core tutorial. We enabled the Custom User Settings feature in Orchard Core, and created a custom content type, called User Profile, with a stereotype of "CustomUserSettings" that holds the user profile information we wish to manage on the Orchard Core website.

We learned that each content type with a stereotype of “CustomUserSettings” gets displayed in its own section, which by default is displayed as tabs when managing users. The default display can be modified by tweaking the display names of the content types as well as modifying the placements.json file in our Orchard Core theme or module.

And last, we can then access the custom user settings via Razor or the Liquid template language to display on the website. In this case, we used it to display the author of the blog post in an Orchard Core blog.

If you haven’t already, I encourage you to read the Orchard Core tutorial on Custom Settings, because it works very similar to custom user settings, and it is just as applicable when developing Orchard Core websites.

If you're an aspiring Orchard Core developer, browse my Orchard Core tutorials.