This is my code:
var variant = _contentRepository.GetDefault<Variant>(productRef);
variant.Name = "Name";
variant.DisplayName = "Name sv";
var vaRef = _contentRepository.Save(variant, SaveAction.Publish, AccessLevel.NoAccess);
var noVariant = _contentRepository.CreateLanguageBranch<Variant>(vaRef, CultureInfo.GetCultureInfo("no"));
noVariant.Name = "Name";
noVariant.DisplayName = "Name no";
_contentRepository.Save(noVariant, SaveAction.Publish, AccessLevel.NoAccess);
What happens is that I get two variants, but the variant for the no language is not published and is missing DisplayName?I can publish the variant from the GUI without problem.
Any idea why this is happening?
I see your code is missing setting the code for the variants. Maybe it can't create a duplicate with the same code. I'd look into that.
Maybe something like this could be what you need, or if not try setting it up the same way the code is set up on the GUI
Thanks for your suggestion.
Code is inherited from the master languauge so I think it is ok. And you cannot edit it on the created variant. I think it must be something else.
Code is shared between languages so that is correct. even if you leave it empty it would be autogenerated from name (which is required)
My suspection is with
You could try GetDefault<T>(productRef, cultureInfo) to see if it works.
Not really. Using GetDefault<T> setting the same Code as the first variant gave me an exception, something like "Code XXX is already used by YYY".Leaving Code blank gave me a genreated code from Commerce leaving me with two variants but both variants had swedish as language even though I used no as culture on the second one:
_contentRepository.GetDefault<Variant>(productRef, new CultureInfo("no"));
Still no DisplayName on the second variant, which is also strange.
Maybe I'm missing something using GetDefault<T>?
Now it is my bad. Please try with Get<T>(vaRef, CultureInfo.GetCultureInfo("no"));
This is so strange. No I tried this.
var noPage = _contentRepository.Get<Variant>(vaRef, new CultureInfo("no"));
var clone = noPage.CreateWritableClone<Variant>();
clone.Language = new CultureInfo("no");
var no = _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
var newNoPage = _contentRepository.Get<Variant>(no, new CultureInfo("no"));
The noPage.Language is "sv", but when I publish the "no" variant is created. Should the noPage have "no" as culture?
So I try to set the language, I publish the page, and then load the newly created norwegian version again. The newNoPage.DisplayName has a value but when I load the norweigan variant in Commerce the variant has no DisplayName and is not published?
Hmm, beats me!
var noBranch = _contentRepository.CreateLanguageBranch<Variant>(varRef, CultureInfo.GetCultureInfo("no"));
Gives me noBranch.Language = "no", but still no DisplayName and still not published.
Now I think I understand why it does not work. vaRef is a content link WITH the workid, and that will override the language. If you call vaRef.ToReferenceWithoutVersion() it should work
Yes, works just perfect!
I can also confirm that this doesn't work:
var noPage = _contentRepository.CreateLanguageBranch<DRVariant>(vaRef.ToReferenceWithoutVersion(), new CultureInfo("no"));
Should I report it as a bug?
Thanks for your help Quan!