Skip to main content
added 147 characters in body
Source Link
Vlad Iobagiu
  • 18.8k
  • 2
  • 27
  • 53

You can use below the method:

You can use it in next way:

 var itemModel=SitecoreConnectionManager.GetItemById(context.CommerceContext,"{id}",en);

Here is the method definition:

public static ItemModel GetItemById(CommerceContext commerceContext, string itemId, string language = null)
    {
        ItemModel result;
        try
        {
            Guid guid;
            if (!Guid.TryParse(itemId, out guid))
            {
                commerceContext.Logger.LogError("SitecoreConnectionManager: ItemId is not a valid GUID.", Array.Empty<object>());
                result = null;
            }
            else
            {
                SitecoreConnectionPolicy policy = commerceContext.GetPolicy<SitecoreConnectionPolicy>();
                string action = string.IsNullOrEmpty(language) ? string.Format("sitecore/api/ssc/item/{0}?database={1}", itemId, policy.SitecoreDatabase) : string.Format("sitecore/api/ssc/item/{0}?database={1}&language={2}", itemId, policy.SitecoreDatabase, SitecoreConnectionManager.GetLanguage(commerceContext, language));
                HttpResponseMessage httpResponseMessage = SitecoreConnectionManager.ProcessRequest(commerceContext, action, "GET", null);
                if (httpResponseMessage == null || !httpResponseMessage.IsSuccessStatusCode)
                {
                    result = null;
                }
                else
                {
                    result = JsonConvert.DeserializeObject<ItemModel>(httpResponseMessage.Content.ReadAsStringAsync().Result);
                }
            }
        }
        catch (Exception ex)
        {
            commerceContext.LogException("SitecoreConnectionManager", ex);
            result = null;
        }
        return result;
    }

You can use below the method:

  public static ItemModel GetItemById(CommerceContext commerceContext, string itemId, string language = null)
    {
        ItemModel result;
        try
        {
            Guid guid;
            if (!Guid.TryParse(itemId, out guid))
            {
                commerceContext.Logger.LogError("SitecoreConnectionManager: ItemId is not a valid GUID.", Array.Empty<object>());
                result = null;
            }
            else
            {
                SitecoreConnectionPolicy policy = commerceContext.GetPolicy<SitecoreConnectionPolicy>();
                string action = string.IsNullOrEmpty(language) ? string.Format("sitecore/api/ssc/item/{0}?database={1}", itemId, policy.SitecoreDatabase) : string.Format("sitecore/api/ssc/item/{0}?database={1}&language={2}", itemId, policy.SitecoreDatabase, SitecoreConnectionManager.GetLanguage(commerceContext, language));
                HttpResponseMessage httpResponseMessage = SitecoreConnectionManager.ProcessRequest(commerceContext, action, "GET", null);
                if (httpResponseMessage == null || !httpResponseMessage.IsSuccessStatusCode)
                {
                    result = null;
                }
                else
                {
                    result = JsonConvert.DeserializeObject<ItemModel>(httpResponseMessage.Content.ReadAsStringAsync().Result);
                }
            }
        }
        catch (Exception ex)
        {
            commerceContext.LogException("SitecoreConnectionManager", ex);
            result = null;
        }
        return result;
    }

You can use method:

You can use it in next way:

 var itemModel=SitecoreConnectionManager.GetItemById(context.CommerceContext,"{id}",en);

Here is the method definition:

public static ItemModel GetItemById(CommerceContext commerceContext, string itemId, string language = null)
    {
        ItemModel result;
        try
        {
            Guid guid;
            if (!Guid.TryParse(itemId, out guid))
            {
                commerceContext.Logger.LogError("SitecoreConnectionManager: ItemId is not a valid GUID.", Array.Empty<object>());
                result = null;
            }
            else
            {
                SitecoreConnectionPolicy policy = commerceContext.GetPolicy<SitecoreConnectionPolicy>();
                string action = string.IsNullOrEmpty(language) ? string.Format("sitecore/api/ssc/item/{0}?database={1}", itemId, policy.SitecoreDatabase) : string.Format("sitecore/api/ssc/item/{0}?database={1}&language={2}", itemId, policy.SitecoreDatabase, SitecoreConnectionManager.GetLanguage(commerceContext, language));
                HttpResponseMessage httpResponseMessage = SitecoreConnectionManager.ProcessRequest(commerceContext, action, "GET", null);
                if (httpResponseMessage == null || !httpResponseMessage.IsSuccessStatusCode)
                {
                    result = null;
                }
                else
                {
                    result = JsonConvert.DeserializeObject<ItemModel>(httpResponseMessage.Content.ReadAsStringAsync().Result);
                }
            }
        }
        catch (Exception ex)
        {
            commerceContext.LogException("SitecoreConnectionManager", ex);
            result = null;
        }
        return result;
    }
Source Link
Vlad Iobagiu
  • 18.8k
  • 2
  • 27
  • 53

You can use below the method:

public static ItemModel GetItemById(CommerceContext commerceContext, string itemId, string language = null) from class Sitecore.Commerce.Plugin.Management.SitecoreConnectionManager

  public static ItemModel GetItemById(CommerceContext commerceContext, string itemId, string language = null)
    {
        ItemModel result;
        try
        {
            Guid guid;
            if (!Guid.TryParse(itemId, out guid))
            {
                commerceContext.Logger.LogError("SitecoreConnectionManager: ItemId is not a valid GUID.", Array.Empty<object>());
                result = null;
            }
            else
            {
                SitecoreConnectionPolicy policy = commerceContext.GetPolicy<SitecoreConnectionPolicy>();
                string action = string.IsNullOrEmpty(language) ? string.Format("sitecore/api/ssc/item/{0}?database={1}", itemId, policy.SitecoreDatabase) : string.Format("sitecore/api/ssc/item/{0}?database={1}&language={2}", itemId, policy.SitecoreDatabase, SitecoreConnectionManager.GetLanguage(commerceContext, language));
                HttpResponseMessage httpResponseMessage = SitecoreConnectionManager.ProcessRequest(commerceContext, action, "GET", null);
                if (httpResponseMessage == null || !httpResponseMessage.IsSuccessStatusCode)
                {
                    result = null;
                }
                else
                {
                    result = JsonConvert.DeserializeObject<ItemModel>(httpResponseMessage.Content.ReadAsStringAsync().Result);
                }
            }
        }
        catch (Exception ex)
        {
            commerceContext.LogException("SitecoreConnectionManager", ex);
            result = null;
        }
        return result;
    }