Welcome to another interesting blog, Sitecorians!👋
Sitecore Content Hub offers a comprehensive content management solution that allows businesses to manage their digital assets, content, and marketing materials seamlessly. The WebClient SDK is a powerful tool for developers looking to integrate Sitecore Content Hub into their applications. In this blog, we'll explore how to retrieve asset details from Sitecore Content Hub using the WebClient SDK, with a focus on a practical example.
Setting Up the Environment
Before diving into the code, ensure that you have the WebClient SDK installed in your project. This SDK is a part of the Sitecore Content Hub API and can be added via NuGet package manager or directly referenced in your project. Additionally, you should have a username, password, client ID, and client secret, which can be viewed by navigating to Sitecore Content Hub -> Manage -> OAuthClients. Click on your OAuth client, or you may also create your own.
Understanding the Solution :
- Authentication: It authenticates against the Sitecore Content Hub API using the credentials provided in HubConnectorKeys.
- Retrieving Asset Details: It sends a GET request to the Content Hub API to retrieve the details of a specific asset by its ID.
Code Implementation:
using Newtonsoft.Json;
using System.IO;
using System.Net;
using WebApplication1.Models.ContentHubModel;
namespace WebApplication1.Helper
{
public class ContentHubConnector
{
HubConnectorKeys keys = new HubConnectorKeys
{
ClientId = "your-client-id",
ClientSecret = "your-client-secret",
UserName = "your-username",
Password = "your-password!",
EndPoint = "https://{sitecore-marketing-hub}"
};
public AssetDetailsResponse GetAssetDetails(string assetId)
{
if (!string.IsNullOrEmpty(assetId))
{
var token = GetAccessToken(keys);
if (!string.IsNullOrEmpty(token))
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(keys.EndPoint + "/api/entities/" + assetId);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
httpWebRequest.Headers.Add("X-Auth-Token", token);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
if (!string.IsNullOrEmpty(result))
{
var response = JsonConvert.DeserializeObject<AssetDetailsResponse>(result);
return response;
}
}
}
}
return null;
}
public string GetAccessToken(HubConnectorKeys keys)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(keys.EndPoint + "/api/authenticate");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"user_name\":" + "\"" + keys.UserName + "\"" + "," +
"\"password\":" + "\"" + keys.Password + "\"" + "}";
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
if (!string.IsNullOrEmpty(result))
{
var response = JsonConvert.DeserializeObject<TokenStatus>(result);
return response.Token;
}
}
return string.Empty;
}
}
}
- Instantiate the ContentHubConnector class.
- Call the GetAssetDetails method with the ID of the asset you want to retrieve.
- Access the returned AssetDetailsResponse object to get the details of the asset.
Model Logic:
using Newtonsoft.Json;
using Sitecore.Shell.Framework.Commands.ContentEditor;
using System.Collections.Generic;
using static WebApplication1.Models.ContentHubApi;
namespace WebApplication1.Models.ContentHubModel
{
public class HubConnectorKeys
{
public string EndPoint { get; set; }
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class TokenStatus
{
public string Token { get; set; }
}
public class AssetDetailsResponse
{
public int id { get; set; }
public string identifier { get; set; }
public Properties properties { get; set; }
public Renditions renditions { get; set; }
}
public class Properties
{
public string FileName { get; set; }
public string Title { get; set; }
public Dictionary<string, string> Description { get; set; }
}
public class Renditions
{
public List<RenditionItem> downloadOriginal { get; set; }
public List<RenditionItem> downloadAlternative { get; set; }
public List<RenditionItem> metadata { get; set; }
public List<RenditionItem> pdf { get; set; }
public List<RenditionItem> preview { get; set; }
public List<RenditionItem> bigthumbnail { get; set; }
public List<RenditionItem> thumbnail { get; set; }
public List<RenditionItem> video_mp4 { get; set; }
}
public class RenditionItem
{
public string href { get; set; }
}
}
- HubConnectorKeys: Holds the API credentials and endpoint URL.
- TokenStatus: Represents the response from the authentication endpoint, primarily containing the access token.
- AssetDetailsResponse and its Sub-models (Properties, Renditions, RenditionItem): These classes represent the structure of the response received when fetching asset details. They contain information such as the asset ID, file name, title, descriptions, and various renditions of the asset.