2

I am adding a new version of an item in a different language. This works fine. Them I am setting the field values on the new version, but those values are not saved. The code is basically (simplified):

Item englishItemVersion = _newDatabase.GetItem(swedishItem.ID, englishLanguage).Versions.AddVersion();
englishItemVersion.Editing.BeginEdit();
englishItemVersion.Fields[{guid}].Value = “my English title”;
englishItemVersion.Editing.EndEdit(false, true);

When i debug the code, all the fields on the new itemversion is set, but as soon as the code hits EndEdit(false, true);, all the values in the fields disappear. Why is that?

6
  • 1
    How do you know the values disappeared? Do you check for them in Content Editor? In other place in code? Directly in database? Are you sure your "_newDatabase" points to the master database?
    – Marek Musielak
    Commented Dec 12, 2016 at 10:39
  • 1
    I am watching the fields as i debug the code, line by line. And when i check in the content editor afterwards, there are no values either. And yes, the database reference works fine. The english item version is created even, but the fields are not set. Commented Dec 12, 2016 at 10:57
  • 1
    May be worth trying to put your code inside "using (new Sitecore.SecurityModel.SecurityDisabler()){//your code}" ? Commented Dec 12, 2016 at 11:13
  • 2
    Try changing your .EndEdit() call to .EndEdit(true, false) or .EndEdit() without flags.
    – Mark Cassidy
    Commented Dec 12, 2016 at 11:17
  • @MarkCassidy That actually did the trick. Although i do not see how that should affect it, it indeed does work when i remove those flags. Thanks for the idea. Commented Dec 12, 2016 at 11:53

1 Answer 1

2

So the problem was in the .EndEdit() call.

By calling .EndEdit(false, true) you are essentially telling Sitecore two things:

  1. Do not update revision, revision date and so on. In other words, try and "hide" that the update took place
  2. Disable events for this update

So basically you did pretty much everything you could, to try and hide the fact that you were adding versions and adding fields, to your surroundings. Including Content Editor, which would then not get notified that item changes took place. Caches would not get invalidated. And so on.

2
  • The code is to be used in a migration, where i do not care that caches etc is not updated. I just want to migrate and manipulate 100k items as fast as possible. It was my understanding that disabling events and diabling statistics would help that. But if i cant add language versions ,then that does not help at all. I am having a look at EventDisabler and BulkUpdateContext instead now. Commented Dec 13, 2016 at 11:56
  • Are you running a non-up-to-date Unicorn by any chance?
    – Mark Cassidy
    Commented Dec 13, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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