How to Add and Use Custom Properties in Sitecore Content Hub Schemas

Hello Sitecorians👋, Welcome to another interesting blog! Today, we will learn about very basic yet core and important topic in Sitecore Content Hub, the concept of schemas. Let's get started.



What is a Schema in Sitecore Content Hub?

In Sitecore Content Hub, the Schema page serves as a comprehensive directory that lists all the entity definitions configured within the system. Each entity definition on this page comprises member groups and members, which collectively define the structure and content of data. These definitions provide the foundation for various content types, such as assets, products, or projects. For example, an asset is created based on the M.Asset entity definition, which specifies its properties and relationships. This approach ensures that the system's data architecture is robust, flexible, and tailored to organizational needs.


A prime example of such customization is the addition of new properties to the M.Asset. In our use case, we will explore how to enhance the M.Asset definition by introducing a property named IsPublicAsset, which will serve to toggle the visibility of assets based on their public or private status. This addition will allow content managers to dynamically control which assets are displayed on various platforms, directly from the Content Hub.


Customizing Schemas: A Simple Use Case

In my use case, i will be adding a new property to the M.Asset entity definition- a Boolean property named IsPublicAsset. This property will determine whether an asset is public and should be visible to all users, or private and hidden from public view. The inclusion of IsPublicAsset serves a critical function; it allows content managers to seamlessly control the visibility of assets directly through the Content Hub, thus integrating content management and access control into a single workflow.

Steps to Add the IsPublicAsset Property

Step 1: Log in to your Sitecore Content Hub and navigate to the Manage section ⚙️, then select Schemas.



Step 2:
Choose the M.Asset schema from the list of available schemas.


Step 3: You will see the list of member groups there. In the overview group, I will add my property called IsPublicAsset. To do this, click on "New Member" on the right side.


Step 4:
After clicking, a pop-up will appear where you can see options to add properties, relations, and taxonomies to the overview group. For my use case, I will add one property, so I will click on the "Select" button for the property.


Step 5: Another pop-up will ask for the type of property. I will choose a Boolean property there.


Step 6: Name the property as IsPublicAsset and provide a label for that property. In the help text, I will provide some text to give context to what the property's functionality will do. Then click on save.


Step 7: After saving, you will see your property added to the overview group.


Step 8: After adding the property, you have to apply the changes. Click on "Apply Changes" and you might receive a pop-up warning message that applying changes will affect 'n' number of entities and will require confirmation to apply changes.


Step 9: Go to the asset page, then choose any asset from the asset page. An asset detail page will open which will now show your added property in the overview, with the default flag set to false or represented as a cross sign.



Step 10:
 Click on "Edit" in the overview to edit your property. Click on the checkbox to make that value true and save your changes.


Step 11: The changed value can now be seen on your asset detail page for your specific asset.




Accessing Asset Properties via API 

Before we jump into the code implementation, it's crucial to understand how to retrieve and utilize the properties of assets stored in Sitecore Content Hub. This will enable us to manipulate these properties programmatically, enhancing our application’s interaction with Sitecore.


Retrieving Asset Properties 

To access the asset properties, you need to interact with the Sitecore Content Hub API. This API provides a direct route to fetch detailed information about each asset, including custom properties like IsPublicAsset which we are interested in for our visibility control feature.


API Request: 

To fetch an asset's details, send a GET request to the following endpoint: 

https://your-sitecore-content-hub-instance/api/entities/{assetId}


Replace {assetId} with the actual ID of the asset you're interested in. This request will return a JSON object containing all the properties associated with that asset.

Example JSON Response: 

Here's an example of what you might receive as a response from the API:

{
  "id": 39342,
  "identifier": "AcZ3xDv5ToCps6MwaWW3BA",
  "cultures": [
    "en-US"
  ],
  "properties": {
    "FileName": "download.jpg",
    "Title": "download.jpg",
    "Description": {
      
    },
    "IsPublicAsset": true,
    "ChiliType": null,
    "Asset.ExplicitApprovalRequired": false,
    "Asset.HasComplexRightsProfiles": null,
    "Asset.DrmComplexity": null,
    "Asset.Copyright": null,
    "ReasonForStatus": null,
    "VisionDescription": null,
    "VisionOcrText": null,
    "SitecoreMLStatus": null,
    "ImageSimilarityTags": null,
    "FileSize": 0.01,
    "FileProperties": {
      "properties": {
        "colorspace": "sRGB",
        "content_type": "image/jpeg",
        "extension": "jpg",
        "filename": "download.jpg",
        "filesize": 0.01,
        "filesizebytes": "10902",
        "group": "Images",
        "height": "275",
        "megapixels": "0.050325",
        "resolution": "183x275",
        "width": "183"
      }
    }
  }
}

In this JSON response, the IsPublicAsset property is particularly important for our use case. It tells us whether the asset is public (true) or should remain private (false). 

Utilizing the API Data in Code

Now that we understand how to retrieve asset properties from the API, we can use this data in our application logic. By checking the IsPublicAsset property, we can decide programmatically whether to display certain assets, ensuring that only appropriate content is visible based on its public or private status.

Code Implementation:

Step 1: In your Content Hub connector model, add a property that you will receive from the hub. Below is the code snippet:

public class Properties
{
    public string FileName { get; set; }
    public string Title { get; set; }
    public Dictionary <string, string> Description { get; set; }
    public bool IsPublicAsset { get; set; }
}     

For the connector class, I've shared details in my blog about "How to Retrieve Asset Details from Sitecore Content Hub using WebClient SDK". Refer to it to include it in your project or implementation. Code Snippet for Content Hub Connector Class

Controller Logic: 

Here, I have handled asset visibility at the controller level, but you can modify this example code as per your case. This is to give an idea of how to go about it. I have created an AssetsDetailsController to fetch asset ID data and hide it if the IsPublicAsset value is true:

using System.Linq;
using System.Web.Mvc;
using WebApplication1.Models;
using WebApplication1.Helper;

namespace WebApplication1.Controllers
{
    public class AssetsDetailsController : Controller
    {
        // GET: Assets Details
        public ActionResult GetAssetDetails(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var connector = new ContentHubConnector();
                var status = connector.GetAssetDetails(id);
                var hideAsset = status.properties.IsPublicAsset;
                if (hideAsset)
                {
                    return View();
                }
                var assetsDetails = new AssetsDetailsModel
                {
                    AssetTitle = status.properties.Title,
                    AssetBodyCopy = status.properties.Description != null && status.properties.Description.ContainsKey("en-US") ? status.properties.Description["en-US"] : null,
                    AssetImageThumbnailSrc = status.renditions?.bigthumbnail?.FirstOrDefault()?.href,
                    AssetImageURL = status.renditions?.downloadOriginal?.FirstOrDefault()?.href,
                    VideoUrl = status.renditions?.video_mp4?.FirstOrDefault()?.href,
                    IsPDF = status.properties.FileName?.Contains(".pdf") ?? false
                };

                return View(assetsDetails);
            }

            return View(); 
        }

    }
}


Conclusion:

Sitecore Content Hub's ability to customize entity definitions within its schema by adding new members or member groups empowers businesses to refine their content management strategies effectively. This flexibility helps organizations meet their evolving content needs and fully leverage Sitecore's extensive features for digital asset management.


Thank you for reading! Continue exploring and enhancing your skills. Happy coding!👋
Post a Comment (0)
Previous Post Next Post