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
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; }
}
Step 2: Fetching CMP Items
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.