Try our conversational search powered by Generative AI!

Saving page programmatically

Vote:
 

Trying to set a fallback image on a product.
Trying to get it from parent (that is category)

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentEvents : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
var events = context.Locate.ContentEvents();
events.PublishingContent += EventsPublishingContent;

}

private void EventsPublishingContent(object sender, ContentEventArgs e)
{
if (e.Content is RouteProductContent)
{
var content = e.Content as ProductContent;
if (content.IconImage != null) return;

var parentContentReference = content.ParentLink;
var repository = ServiceLocator.Current.GetInstance();
var category = repository.Get(parentContentReference);
if (category.IconImage != null)
{
var clone = content.CreateWritableClone() as RouteProductContent;
clone.Property["IconImage"] = new PropertyContentReference(category.IconImage);

// have also tried with
clone.Property["IconImage"].value = category.IconImage;
// and
clone.IconImage = category.IconImage;

}
}
}

public void Uninitialize(InitializationEngine context)
{
var contentEvents = ServiceLocator.Current.GetInstance();
contentEvents.PublishingContent -= EventsPublishingContent;
}
}

#179356
Jun 08, 2017 21:32
Vote:
 

Assuming that the problem is not getting any propterydata on the clone.

You need to call save on the clone, "repository.Save(clone, SaveAction.Publish);" for the change to be persistant.

/Oskar

#179359
Jun 08, 2017 22:37
Vote:
 

Oskar's answer is correct - the change to any content (not just catalog content, but also CMS content) needs to be explicitly saved. It'll not be saved automatically when you set it.

Apparently I didn't read the question carefully, for IContentEvents you don't have to publish it explicitly

#179380
Edited, Jun 09, 2017 10:26
Vote:
 

Not working as aspected:

Both tried with

var content = e.Content as RouteProductContent;
if (content.IconImage != null) return;

var parentContentReference = content.ParentLink;
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var category = repository.Get<SondermautCategoryNodeContent>(parentContentReference);
if (category?.IconImage != null)
{
var clone = content.CreateWritableClone();
clone.Property["IconImage"].Value = category.IconImage;
repository.Publish(clone);
}

and with 

repository.Save(clone); 

On publishing event and published event.

Not saving the value

#179617
Jun 16, 2017 11:31
Vote:
 

Also
with repository.Save(clone, SaveAction.Save, AccessLevel.Publish);

#179618
Jun 16, 2017 11:48
* 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.