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
Steps to Add the IsPublicAsset Property
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}
Example JSON Response:
{
"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"
}
}
}
}
Utilizing the API Data in Code
Code Implementation:
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; }
}
Controller Logic:
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();
}
}
}