2

I am trying to retrieve a Sitecore.Data.Items.Item using this line:

var myItem = Context.Database.GetItem("/sitecore/content/x");

but instead of an Item I get a null exception. I have also tried

var myItem = Context.Data.Database.GetItem("/sitecore/content/x")

with same result. It seems like Database is null and when I try to veryfy that with:

var databases = Sitecore.Configuration.Factory.GetDatabases();

I get an empty list back. I have also tried to get a database item directly with:

string databaseName = "master";
var db = Sitecore.Configuration.Factory.GetDatabase(databaseName);

but that gave me another exception

Message = "Could not find configuration node: databases/database[@id='master']"

At other places in the code it works fine to retrive Sitecore Items. Where it does work, I have a CommercePipelineExecutionContext (even though I don't directly uses it. Where it doesn't work I have a CommerceContext in which I have not found any database info. Any pointers to why this isn't working in this spot when it does work in others are appreciated.

  • Are you doing this on a hardened CD server where it has no master DB in the config? – Chris Auer Nov 3 '18 at 0:28
  • I don't think the problem is in a config file (not sure though) since other parts of the code are able to access the master db. – Thomas Wegebrand Nov 5 '18 at 10:13
2

You can use method:

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

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;
    }
| improve this answer | |
  • Yes, normally I could. In this case that won't work since I don't have access to the itemId (sorry for not mention that in the original question). The item I need to fetch I only have the Item name (which also is the last part of the item path). – Thomas Wegebrand Nov 5 '18 at 9:30
  • if you are using public static List<ItemModel> GetItemsByPath(CommerceContext commerceContext, string path, string language = null) does it work for you? – Vlad Iobagiu Nov 5 '18 at 9:33
  • I had to search in a bucket media items and I build myself a SSC api controller – Vlad Iobagiu Nov 5 '18 at 9:34
  • Not sure how that would help me since the problem I am facing is that Sitecore.Configuration.Factory.GetDatabases() returns nothing. It seems like I don't have a database to call GetItem() on. – Thomas Wegebrand Nov 5 '18 at 10:22
  • you don't have to call GetDatabases, you need to use SitecoreConnectionManager, is normal behaviour to crash call of Sitecore.Configuration.Factory.GetDatabases() because you are not in Sitecore Context – Vlad Iobagiu Nov 5 '18 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.