We faced a problem with getting item data from DB. GetItem() method behaves in non deterministic way. We have two instances of Sitecore: clients instance 9.0 and our local instance 9.1. On clients instance GetItem() returns null irregularly for existing item (Item has few language versions). We used all the overloads of GetItem() method but the problem persists. On our local instance GetItem() returns the outdated state of the item irregularly but often.

Our local instance scenario:

  1. Create new template and save changes (mostly text fields)
  2. Create new item and fill it with some data and save changes.
  3. Change fields names and fields types of template and save changes.
  4. Run GetItem() method.
  5. As a result we get the original data about the fields and their types (irregularly ofcourse).

Clients instance scenario:

  1. Item exists and have few language versions
  2. Run GetItem() method.
  3. As a result we get null even we use GetItem(ID itemId, Language language, Version version) overload.

As you can see in screenshots of the code, we extract data from Master DB and in Sitecore we work in Content Editor with Master DB.

Saying "irregularly" I mean one day it can happen once or twice at long intervals. Other day it happens few times every hour. We have a lot of clients who use our code and we haven't encountered this problem with anyone else.

The methods from below are called asynchronously using the job mechanism in another method

We tried to find solution or similar problem in internet and the only valuable information was that this problem can be related to clearing cache memory (https://soen.ghost.io/why-are-my-items-not-returned-from-the-sitecore-database/)

We are using SDK. Have you ever faced problem like this? Do you have some solution of this problem or some workaround?

private Item GetItemFromDB(string sitecoreItemId, Language lang)  
   Database masterDatabase = Database.GetDatabase("master");
   Item contextLangItem = masterDatabase.GetItem(sitecoreItemId, lang);

   var firstLangItem = masterDatabase.GetItem(sitecoreItemId, lang, global::Sitecore.Data.Version.First);
   Version versionNumber1 = null;
   if (firstLangItem != null && firstLangItem.Versions != null && firstLangItem.Versions.Count > 0)
           contextLangItem = firstLangItem.Versions.GetVersions().Last();
           versionNumber1 = firstLangItem.Versions.GetVersionNumbers().Last();
              if (contextLangItem == null)
                    _jobManager.ContextJob.Messages.Add($"contextLangItem is null ");
                   var versionNumber = firstLangItem.Versions.GetVersionNumbers().Last();
                   contextLangItem = masterDatabase.GetItem(sitecoreItemId, lang, versionNumber);
   _jobManager.ContextJob.Messages.Add($"Targ lang: {lang}, ver num: {firstLangItem.Version.Number}, Version_1: {versionNumber1} ");

   return contextLangItem;
            Item translationItem = GetItemFromDB(sitecoreItemId, language);

            var itemUri = new ItemUri(translationItem.ID, language, Version.Latest, translationItem.Database);
            var xx = Database.GetItem(itemUri, true);
            var yy = Database.GetItem(itemUri, false);

            var scItemFieldsList = translationItem.Fields.Where(x => !x.Name.Contains("__") && !x.Name.Contains("XTM_"));
            var scItemFieldsListOrdered = scItemFieldsList.OrderBy(a => a.Name);

            ////////////// ItemUr true /////////////////
            _jobManager.ContextJob.Messages.Add("ItemUri true");
            var xxList = xx.Fields.Where(x => !x.Name.Contains("__") && !x.Name.Contains("XTM_"));
            var xxOrdered = xxList.OrderBy(a => a.Name);
            foreach (var field in xxOrdered)
                _jobManager.ContextJob.Messages.Add($"Name: {field.Name}, type {field.Type}, value: {field.Value}, fieldID: {field.ID}");   

            ////////////// ItemUr false /////////////////
            _jobManager.ContextJob.Messages.Add("ItemUri false");
            var yyList = yy.Fields.Where(x => !x.Name.Contains("__") && !x.Name.Contains("XTM_"));
            var yyOrdered = yyList.OrderBy(a => a.Name);
            foreach (var field in yyOrdered)
                _jobManager.ContextJob.Messages.Add($"Name: {field.Name}, type {field.Type}, value: {field.Value}, fieldID: {field.ID}");

            ////////////// Item and its fields /////////////////
            _jobManager.ContextJob.Messages.Add("Item and its fields");
            foreach (var field in scItemFieldsListOrdered)
                _jobManager.ContextJob.Messages.Add($"Name: {field.Name}, type {field.Type}, value: {field.Value}, fieldID: {field.ID}");

Weird is that below approach witch getting template of item and then looking at its (templates) fields works. Unfortunately it does not solve all my problems

 /////////////////////////Template Fields///////////////////////////////
            var itemsTemplate = ScConstants.SitecoreDatabases.MasterDb.GetTemplate(translationItem.TemplateID);
            var templateFields = itemsTemplate.Fields.Where(x => !x.Name.Contains("__") && !x.Name.Contains("XTM_"));
            _jobManager.ContextJob.Messages.Add("template fields");
            foreach (var field in templateFields)
                _jobManager.ContextJob.Messages.Add($"Name: {field.Name}, type {field.Type}, fieldID: {field.ID}");

screen from logs with cache action enter image description here

  • 1
    Can you please share code samples as text rather than images? – Michael West Sep 9 at 9:16
  • @MichaelWest post edited – pozacieniem Sep 10 at 5:58
  • Sitecore executes code from current context user. Could you clarify, what user is used to run this code? (as code smells option you can try to use SecurityDisabler to understand if problem is with access rights) – Anton Sep 10 at 14:54
  • Admin is used to run this code. I tried SecurityDisabler but problem still exists – pozacieniem Sep 18 at 7:00

Your Answer

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

Browse other questions tagged or ask your own question.