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" :
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?
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.
{
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?
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"
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" :
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?
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
Thank you so much!
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 void SetStartNode(HttpRequestMessage request, int toInt32, string contentTypeAlias, IEnumerable
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?
You may do some pre check for the old value.
It's a bit difficult to provide solution with little info.
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"
is working on a reply...