Update automatically a field while publishing a content
Hi,
I try to update, while publishing a content, a field Name for other node in Umbraco.
I use Microsoft Translator and I try to update fields (when I wrote in english and automatically publish in Romanian)
I did like:
public class MyApplicationEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted( UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext )
{
ContentService.Publishing += ContentServicePublishing;
}
private void ContentServicePublishing( IPublishingStrategy sender, PublishEventArgs<IContent> e )
{
IContent content = e.PublishedEntities.FirstOrDefault();
if ( content != null ) {
var prop = content.Properties.FirstOrDefault( s => s.Alias.Equals( "umbracoRedirect" ) ); // used to link to other document where I will make update on Name field
if ( prop != null ) {
int siblingId = (int) prop.Value;
// try to get IPiblishedContent by siblingId
// try to set Name property value but I cannot because Name is read-only
}
}
}
}
How to update node field Name ? How to get IPublishedContent or IContent by siblingId value ?
To get a IContent by an Id, you can use the ContentService.GetById(id) method.
// Get IContent by siblingId
var sibling = ApplicationContext.Current.Services.ContentService.GetById(siblingId);
The Name value will not be read-only for this Content and remember you need to Save so that your changes will take affect.
So your code example would be something like:
private void ContentServicePublishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
IContent content = e.PublishedEntities.FirstOrDefault();
if (content != null)
{
// used to link to other document where I will make update on Name field
var prop = content.Properties.FirstOrDefault(s => s.Alias.Equals("umbracoRedirect"));
int siblingId;
if (prop != null && prop.Value != null && int.TryParse(prop.Value.ToString(), out siblingId))
{
// Get IContent by siblingId
var sibling = ApplicationContext.Current.Services.ContentService.GetById(siblingId);
// Set the Name property value
sibling.Name = "New Name value here.."; // Example.
// IMPORTANT: Save your changes.
ApplicationContext.Current.Services.ContentService.Save(sibling);
}
}
}
Which will give you this functionallity: (Then you just need to update it so that is replaces the Name with a translated value instead of this hard-coded value, but you get the idea.)
Update automatically a field while publishing a content
Hi,
I try to update, while publishing a content, a field
Name
for other node in Umbraco.I use Microsoft Translator and I try to update fields (when I wrote in english and automatically publish in Romanian)
I did like:
public class MyApplicationEventHandler : ApplicationEventHandler { protected override void ApplicationStarted( UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext ) { ContentService.Publishing += ContentServicePublishing; }
}
How to update node field
Name
? How to getIPublishedContent
orIContent
bysiblingId
value ?Hi Mihail.
To get a IContent by an Id, you can use the ContentService.GetById(id) method.
The Name value will not be read-only for this Content and remember you need to Save so that your changes will take affect.
So your code example would be something like:
Which will give you this functionallity: (Then you just need to update it so that is replaces the Name with a translated value instead of this hard-coded value, but you get the idea.)
Hope this was helpful.
Do you have any idea how to do in case when I want to create automatically a new node ?
This example creates a child node, hope it helps.
I figured it out. Thanks for help !
Awesome! Glad i could help! Take care! :)
is working on a reply...