Copied to clipboard

Flag this post as spam?

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


  • sony 11 posts 111 karma points notactivated
    Jan 30, 2016 @ 11:04
    sony
    0

    How to set Node's property value when node is first time published?

    Hi,

    When I create node first time then if I published it then I want to set its property value.

    If I publish next time then I don't want to set its property value.

    Please share any idea or any reference.

    Current Umbraco Version is : 6.2.4

    Thanks.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 30, 2016 @ 11:07
    Jan Skovgaard
    0

    Hi Sony

    What kind of property value do you want to set and why? What is the scenario?

    You should be able to use the ContentService API for achieving this though - https://our.umbraco.org/documentation/reference/management/services/contentservice

    Just curious to figure out what and why to figure out whether it's necessary or not :)

    Hope this helps.

    /Jan

  • sony 11 posts 111 karma points notactivated
    Jan 31, 2016 @ 07:54
    sony
    100

    Thanks Jan for your reply.

    I have solve it now myself.

    below the code is working for me :

    public class updatetext : ApplicationEventHandler { protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Published += ContentService_Publishing; }

        void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<Umbraco.Core.Models.IContent> e)
        {
            foreach (var content in e.PublishedEntities)
            {
                var test = content.GetValue<int>("test");
                if (content.ContentType.Alias == "Product" && test == 0)
                {
    
                    int count = 1;                   
                    var contentService = ApplicationContext.Current.Services.ContentService;
                    var student = contentService.GetById(content.Id);
                    student.SetValue("test", count);
                    student.SetValue("start", "first time published");
                    contentService.SaveAndPublishWithStatus(student);
                    count++;
                  BasePage.Current.ClientScript.RegisterClientScriptBlock(this.GetType(), "refresh301URLTracker", "$(window).load(function(){UmbClientMgr.contentFrame('editContent.aspx?id=" + content.Id + "');})", true);
    
                }
    
    
            }
        }
    }
    
  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 31, 2016 @ 09:16
    Jan Skovgaard
    0

    Hi Sony

    Great! And very cool that you share your code so others can benefit from it or suggest improvements if they figure out something that can be done in a better way :)

    Happy coding!

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft