Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mihail 39 posts 142 karma points
    Sep 13, 2016 @ 06:36
    Mihail
    1

    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 ?

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Sep 13, 2016 @ 07:07
    Dennis Adolfi
    100

    Hi Mihail.

    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.)

    enter image description here

    Hope this was helpful.

  • Mihail 39 posts 142 karma points
    Sep 14, 2016 @ 08:23
    Mihail
    1

    Do you have any idea how to do in case when I want to create automatically a new node ?

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Sep 14, 2016 @ 09:01
    Dennis Adolfi
    0

    This example creates a child node, hope it helps.

    var newNode = ApplicationContext.Current.Services.ContentService.CreateContent(name: "New node name", parentId: 1052, contentTypeAlias: "page", userId: -1);
    ApplicationContext.Current.Services.ContentService.Save(newNode);
    
  • Mihail 39 posts 142 karma points
    Sep 14, 2016 @ 09:31
    Mihail
    1

    I figured it out. Thanks for help !

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Sep 14, 2016 @ 09:33
    Dennis Adolfi
    0

    Awesome! Glad i could help! Take care! :)

Please Sign in or register to post replies

Write your reply to:

Draft