Copied to clipboard

Flag this post as spam?

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


  • progproger 52 posts 130 karma points
    Sep 18, 2015 @ 12:22
    progproger
    0

    Override Content Property Value

    Hi,

    I'd like to override property value when page is loaded in "edit" mode... For example:

    I have doc type with some properties (e.g. EventPrice, StartDate, EndDate etc). I add this into umbraco nodes. Is saves data ofc, but I'd like to save some values into another table.(And also get values) I've done this only for "insert" mode using new class which is inherited from "ApplicationEventHandler" :

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Published += ContentServicePublished; }

        private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
        {
            foreach (var node in args.PublishedEntities)
            {
                if (node.ContentType.Alias == EventModel.EventTypeAlias)
                { //code here
                }
            }
        }
    

    But i also need to get values from another table on load event. Unfortunately i could not find corresponding event in "ContentService" class. Can you please help?

  • jivan thapa 194 posts 681 karma points
    Sep 20, 2015 @ 07:14
    jivan thapa
    0

    You need to use "DelegatingHandler". Every time Umbraco backend page is loaded, it requests to backend url "/umbraco/backoffice/umbracoapi/content/getbyid".

    You have possibility to override the property value before page loads on this step.

    Have a look at Hybrid Framework for Umbraco 7. https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises

    He have done, dynamically redirecting Multinode tree url picker's start node. Same concept might use to solve your issue.

    https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/52755-Dynamically-set-starting-node-of-media-picker-in-richtext-editor

  • progproger 52 posts 130 karma points
    Sep 22, 2015 @ 12:39
    progproger
    0

    Thank you so much!

  • progproger 52 posts 130 karma points
    Sep 22, 2015 @ 14:48
    progproger
    0

    Hi Jivan,

    I tried the following code:

    protected override Task

    { var path = request.RequestUri.AbsolutePath.ToLower(); switch (path) { case "/umbraco/backoffice/umbracoapi/content/getbyid": return SetEventStartNode(request, cancellationToken); default: return base.SendAsync(request, cancellationToken); } }

        }
    
        private Task<HttpResponseMessage> SetEventStartNode(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            return base.SendAsync(request, cancellationToken)
               .ContinueWith(task =>
               {
                   var response = task.Result;
                   try
                   {
                       var d = response.Content;
                       var c = ((ObjectContent)(d)).Value as ContentItemDisplay;
    
                       if (c != null)
                           SetStartNode(request, Convert.ToInt32(c.Id), c.ContentTypeAlias, c.Properties);
                   }
                   catch (Exception ex)
                   {
                       throw ex;
                   }
                   return response;
               }, cancellationToken);
        }
    

    private void SetStartNode(HttpRequestMessage request, int toInt32, string contentTypeAlias, IEnumerable

        {
            var cs = ApplicationContext.Current.Services.ContentService;
    
            var currentEvent = cs.GetById(toInt32);
    
            if (currentEvent == null)
                return;
    
            bool updated = false;
            string summaryFromMerchello = "testvalue";
    
            if (summaryFromMerchello != currentEvent.GetValue<string>("summary"))
            {
                currentEvent.SetValue("summary", summaryFromMerchello);
            }
    
            cs.SaveAndPublishWithStatus(currentEvent);
        }
    

    It updates property values successfully, but page still renders an old value. After second page load i see correct value... How can i SaveAndPublish property before Umbraco renders an old values?

  • jivan thapa 194 posts 681 karma points
    Sep 22, 2015 @ 16:10
    jivan thapa
    0

    You may do some pre check for the old value.

    var oldValue = currentEvent.GetValue<string>("summary");
    
    if(oldValue == "oldValue")
    {
    // do somthing
     return ;
    }
    
    if (summaryFromMerchello != currentEvent.GetValue<string>("summary"))
            {
                currentEvent.SetValue("summary", summaryFromMerchello);
            }
    

    It's a bit difficult to provide solution with little info.

  • progproger 52 posts 130 karma points
    Sep 23, 2015 @ 07:48
    progproger
    0

    I'm not sure if you understand me correctly.

    All works fine, but Umbraco loads an old value "earlier" than "SendAsync" method.

    For example:

    I have field with value = "currentValue". When i refresh content edit page SendAsync method tries to set the value for property e.g. value = "newValue". But page still shows "currentValue" (i'd like to show "newValue" already). On second page load it shows correctly "newValue"

Please Sign in or register to post replies

Write your reply to:

Draft