SaaS CMS has officially launched! Learn more now.

DisplayName not saved and variant not published when creating variant for language branch



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?



Jul 04, 2019 12:28

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

variant.Code= "Name_sv";
Jul 04, 2019 15:08

Hi Sergiu!

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.


Jul 04, 2019 15:12

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.

Jul 04, 2019 15:58

Hi Quan!

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>?



Jul 04, 2019 17:38

Now it is my bad. Please try with Get<T>(vaRef,  CultureInfo.GetCultureInfo("no"));

Jul 04, 2019 17:48

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!


Edited, Jul 04, 2019 19:08

More info:

var noBranch = _contentRepository.CreateLanguageBranch<Variant>(varRef, CultureInfo.GetCultureInfo("no"));

Gives me noBranch.Language = "no", but still no DisplayName and still not published.


Jul 04, 2019 20:04

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

Jul 04, 2019 20:06

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!


Jul 04, 2019 20:40
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.