How to Fetch CMP Items from Sitecore Content Hub Using WebClient SDK

Welcome, Sitecorians! 👋 Welcome to another interesting blog on Sitecore Content Hub.


In the rapidly evolving digital landscape, managing and delivering content efficiently is crucial for maintaining a strong online presence. Sitecore Content Hub offers a comprehensive solution to content management, and its Content Marketing Platform (CMP) is a key component in strategizing and managing digital assets. In this post, we'll dive into what CMP is and how you can fetch CMP items using the WebClient SDK, including authenticating and retrieving content.



Understanding CMP in Sitecore Content Hub

Before we go into the technicalities, let's understand what CMP stands for. CMP, or Content Marketing Platform, is a part of Sitecore Content Hub designed to streamline content planning, creation, collaboration, and management. It acts as a centralized platform for managing all types of digital content across various channels, making it easier for marketers and content creators to deliver consistent and relevant content. 

CMP streamlines campaign management and offers a unified view of content performance, essential for planning and analyzing content marketing efforts. Initially focused on authoring and collaboration, CMP's functionality has expanded to support omnichannel content distribution, servicing all platforms and consumers. It aims to attract and retain customers through consistent, relevant content creation, integrated into an overall marketing strategy. CMP also features comprehensive tools for editing, collaboration, approval, and a robust data modeler for managing digital content, enhancing its adaptability and localization capabilities.


Fetching CMP Items Using WebClient SDK

Fetching CMP items from Sitecore Content Hub requires interaction with the platform's API. This process involves two main steps: authentication to obtain a token and using that token to fetch the desired content. Here’s a detailed guide on how to achieve this.

Step 1: Obtaining an Authentication Token 

To interact with the Sitecore Content Hub API, you first need to authenticate and obtain a token. This token will be used in subsequent requests to the API to fetch CMP items. Below is a C# code snippet demonstrating how to obtain this token.


public string GetHubToken()
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://your_sitecore_instance.sitecoresandbox.cloud/api/authenticate");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    StatusModel obj = new StatusModel();
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = new JavaScriptSerializer().Serialize(new
        {
            user_name = "CMPConnector",
            password = "YourPassword"
        });

        streamWriter.Write(json);
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        obj = Newtonsoft.Json.JsonConvert.DeserializeObject<StatusModel>(result);
    }
    return obj.token;
}

public class StatusModel
{
    public string token { get; set; }
}


Note: Replace <your_sitecore_instance> with your actual Sitecore instance URL and YourPassword with your actual password.

Step 2: Fetching CMP Items

Once you have the token, you can proceed to fetch CMP items. The following C# code snippet shows how to make a POST request to the Sitecore Content Hub API to retrieve content based on certain criteria.

public ContentRoot GetContentPage(string token)
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://your_sitecore_instance.sitecoresandbox.cloud/api/search");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    httpWebRequest.Headers.Add("X-Auth-Token", token);

    ContentRoot obj = new ContentRoot();
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = new JavaScriptSerializer().Serialize(new
        {
            defaults = "SearchConfiguration",
            configuration_category = "PortalConfiguration",
            component = 9953,
            take = 100,
            fields = new Liststring() { "ContentTypeToContent", "Content.Name", "ContentLifeCycleToContent", "Content.PublicationDate", "LocalizationToContent" }
        });

        streamWriter.Write(json);
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ContentRoot>(result);
    }
    return obj;
}

public class ContentRoot
{
    
    public int totalItemCount { get; set; }
    public int returnedItemCount { get; set; }
    public List<Item> items { get; set; }
   
}

public class Item
{
    public int id { get; set; }
    public string identifier { get; set; }
    public List<string> cultures { get; set; }
    public Properties properties { get; set; }
  
    public DateTime created_on { get; set; }
  
    public DateTime modified_on { get; set; }
   
    
    public bool is_root_taxonomy_item { get; set; }
    public bool is_path_root { get; set; }
    public bool inherits_security { get; set; }
    public bool is_system_owned { get; set; }
    public int version { get; set; }
    public Full full { get; set; }
    
    public Renditions renditions { get; set; }
    public RelatedPaths related_paths { get; set; }
}
public class Properties
{
    [Newtonsoft.Json.JsonProperty("Content.Name")]
    public string ContentName { get; set; }

    
}

public class RelatedPaths
{
    public List<List<object>> AuthorToContent { get; set; }
    public List<List<object>> ContentTopicsToContent { get; set; }
    public List<List<object>> CountryToContent { get; set; }
    public List<List<object>> AccessLevelToContent { get; set; }
}

public class Renditions
{
    public List<Thumbnail> thumbnail { get; set; }       
}  


Conclusion 

Integrating Sitecore Content Hub with your applications using the WebClient SDK allows for seamless content management and delivery. By following the steps outlined above, developers can efficiently fetch CMP items, leveraging Sitecore's robust API. This opens up numerous possibilities for creating personalized, dynamic content experiences across digital platforms.

Post a Comment (0)
Previous Post Next Post