Edit properties in Document_BeforeSave - new values not shown in editor
Hi,
I created a custom TextString property on a document type and also a Document.BeforeSave handler. In the handler I set the property to a value. When I save the form my handler is hit and the form screen is refreshed. Afterwards however the property is still blank in the editor despite the value being set programmatically. When I then publish the document I can see the property value is set correctly as it shows in the umbraco.config xml but in the edit form the property is still blank.
The only way to get the property value to appear in the form is by clicking on another document and then back again to "refresh" the values.
Is there another API call I need to make to get the programmatically generated property values to appear in the edit form after my Document_BeforeSave handler?
I am just guessing here, but I think the properties on the editor page are saved in the viewstate, so that could be the reason that is is not shown right after a save. Because the values are not fetched from the database on a postback.
I don't have a solution for this (if this is indeed the case), but maybe you can get the current httpcontext, and get a hold of the page an change the viewstate.
Again, I am just guessing here. Maybe take a look at the umbraco source to see what is going on?
Because of the way that the editor form creates the controls on the screen (Morten is correct, it is part of the ViewState) the setting of the value programmatically wont refresh on the editor screen.
I gather you're event handler is setting the data like this:
document.getProperty("some_alias").Value = "abc";
The only way to update the form would be to then programmatically find the text box which is rendered out and setting the value in there as well. Using the TextString though that may be next to impossible. You'd need to look into the code to work out how it generates the text box that is displayed.
Got round to fixing this tonight. I don't really know if the following code is all that reusable, for example I hard code "ctl00$body$" as the control id's prefix which may or not be stable. If not there are other ways of seeking it eg; find controls that end with the property name. Call the SetProperty() function like this (c#) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.web;
namespace Gareth.Umbraco
{
public static class Helper
{
private const string m_sEditFormPrefix = "ctl00$body$";
/// <summary>
/// Find a property in a document and set it's value
/// </summary>
/// <param name="oDocument">The Umbraco document containing the property you want to set</param>
/// <param name="sPropertyName">The property name you want to find</param>
/// <param name="sPropertyValue">The value you want to assign to the property (if the property is found)</param>
/// <returns></returns>
public static Property SetProperty(
Document oDocument,
string sPropertyName,
string sPropertyValue)
{
return SetProperty( oDocument, sPropertyName, sPropertyValue, false);
}
/// <summary>
/// Find a property in a document and set it's value and optionally update the edit
/// form so that the programmatically assigned value is displayed in the edit form
/// </summary>
/// <param name="oDocument">The Umbraco document containing the property you want to set</param>
/// <param name="sPropertyName">The property name you want to find</param>
/// <param name="sPropertyValue">The value you want to assign to the property (if the property is found)</param>
/// <param name="bUpdateEditForm">If true then if the umbraco.editorControls.textfield.TextFieldEditor
/// is found then the value is alswo assigned. This effectively passes the new property to the front end edit screen</param>
/// <returns></returns>
public static Property SetProperty(
Document oDocument,
string sPropertyName,
string sPropertyValue,
bool bUpdateEditForm)
{
Property oProperty = null;
if( oDocument != null)
{
//Find the property first
oProperty = oDocument.getProperty( sPropertyName);
if( oProperty != null)
{
//Set the value
oProperty.Value = sPropertyValue;
if( bUpdateEditForm == true)
{
//Get the Page object
umbraco.BasePages.UmbracoEnsuredPage oPage =
(umbraco.BasePages.UmbracoEnsuredPage)oDocument.HttpContext.CurrentHandler;
//Find the form control on the edit form that represents the
//document property we just set the value of
umbraco.editorControls.textfield.TextFieldEditor oTextField =
(umbraco.editorControls.textfield.TextFieldEditor)oPage.FindControl( m_sEditFormPrefix + sPropertyName);
if( oTextField != null)
{
//Set the form control's Text value; this is displayed in the form
oTextField.Text = (string)oProperty.Value;
}
}
}
}
return oProperty;
}
}
}
Slace how do you get that prettyprint formatted code? I spent an hour messing about and got nowhere in the end
Edit properties in Document_BeforeSave - new values not shown in editor
Hi,
I created a custom TextString property on a document type and also a Document.BeforeSave handler. In the handler I set the property to a value. When I save the form my handler is hit and the form screen is refreshed. Afterwards however the property is still blank in the editor despite the value being set programmatically. When I then publish the document I can see the property value is set correctly as it shows in the umbraco.config xml but in the edit form the property is still blank.
The only way to get the property value to appear in the form is by clicking on another document and then back again to "refresh" the values.
Is there another API call I need to make to get the programmatically generated property values to appear in the edit form after my Document_BeforeSave handler?
Thanks
I am just guessing here, but I think the properties on the editor page are saved in the viewstate, so that could be the reason that is is not shown right after a save. Because the values are not fetched from the database on a postback.
I don't have a solution for this (if this is indeed the case), but maybe you can get the current httpcontext, and get a hold of the page an change the viewstate.
Again, I am just guessing here. Maybe take a look at the umbraco source to see what is going on?
Ah you're probably right there. I'm trying to debug the source at the moment I'll get to the bottom of it and come back with findings thanks
Because of the way that the editor form creates the controls on the screen (Morten is correct, it is part of the ViewState) the setting of the value programmatically wont refresh on the editor screen.
I gather you're event handler is setting the data like this:
The only way to update the form would be to then programmatically find the text box which is rendered out and setting the value in there as well. Using the TextString though that may be next to impossible. You'd need to look into the code to work out how it generates the text box that is displayed.
Got round to fixing this tonight. I don't really know if the following code is all that reusable, for example I hard code "ctl00$body$" as the control id's prefix which may or not be stable. If not there are other ways of seeking it eg; find controls that end with the property name. Call the SetProperty() function like this (c#) :
It returns the Property object if you need it
Here's the class
Oh ok it formats it itself on page refresh. What a wasted hour, hint text in the post toolbar would have been nice!
Is this issue posted in codeplex issue tracker? If so I can't find it. so I created an issue for this here: http://umbraco.codeplex.com/workitem/30604
is working on a reply...