November Happy Hour will be moved to Thursday December 5th.

Does anybody know how to Publish updated NodeContent through code?

tss
tss
Vote:
 

Hi Guys

Im currently trying to do a integration through the new IContent API i Commerce 7.5 but I cant get it to keep the data saved on the frontend when I publish the imported data through code.

My setup is this

I have a ContentNode with the type Category

Category has a Description and a DisplayName

I go to the frontend and type in some content i the description without publishing the change.

After that I run the integration using the IContentRepository

The code looks like the following:

 

var category = (Category)_contentRepository.Get<Category>(_referenceConverter.GetContentLink(code, CatalogContentType.CatalogNode).ToReferenceWithoutVersion(), new LanguageSelector(language.Name)).CreateWritableClone();

category.DisplayName = "I Have been updated!!";

_contentRepository.Save(category, SaveAction.Publish, AccessLevel.NoAccess);

 

After running the code  i can see the DisplayName has been updated but I lost the content I typed into the Description.

How do I get it to keep both changes and publish both?

#79320
Dec 13, 2013 10:41
tss
Vote:
 

ahh found the issue, I had to use the IContentVersionRepository to get the current draft

here is the updated code

var categoryCode = "CATEGORY1234"

var link = _referenceConverter.GetContentLink(categoryCode , CatalogContentType.CatalogNode);
var draft = _versionRepository.LoadCommonDraft(link, language.Name);

var category = (Category)_contentRepository.Get<Category>(draft.ContentLink).CreateWritableClone()

category.DisplayName = "I Have been updated!!";

_contentRepository.Save(category, SaveAction.Publish, AccessLevel.NoAccess);

 

Hope it helps somebody else one day :-)

 

#79327
Dec 13, 2013 11:30
Vote:
 

It helped me. Thanks for sharing!

#79636
Dec 24, 2013 8:28
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.