I'm trying to auto update the properties/metadata fields from other properties in the form tab on insert/update.
I based my solution on the example given here and came up with the following:
[assembly: RegisterModule(typeof(ApplySEOMetaDataModule))]
public class ApplySEOMetaDataModule : Module
{
public ApplySEOMetaDataModule()
: base("ApplySEOMetaData")
{
}
protected override void OnInit()
{
base.OnInit();
DocumentEvents.Update.Before += UpdateSEOFields;
DocumentEvents.Insert.Before += UpdateSEOFields;
}
private static void UpdateSEOFields(object sender, DocumentEventArgs e)
{
if (e.Node == null)
{
return;
}
var pageDescription = e.Node.GetStringValue("DocumentPageDescription", String.Empty);
var rollupText = GetPlainText(e.Node.GetStringValue("ListableDocumentRollupText", String.Empty));
Logger.Write("page description: " + pageDescription);
Logger.Write("rollup text: " + rollupText);
if(string.IsNullOrEmpty(pageDescription)){
e.Node.SetValue("DocumentPageDescription", rollupText);
pageDescription = e.Node.GetStringValue("DocumentPageDescription", String.Empty);
Logger.Write("page description after update: " + pageDescription);
}
//e.Node.UpdateField("DocumentPageKeywords", (node, keywords) => keywords ?? String.Empty);
}
private static string GetPlainText(string text)
{
if (String.IsNullOrWhiteSpace(text))
{
return text;
}
return Regex.Replace(text, @"<(.|\n)*?>", string.Empty);
}
}
However, when I check my logs, the event seems to be called multiple times:
[10/30/2017 1:00:40 PM] page description: Rollup text
[10/30/2017 1:00:40 PM] rollup text: Rollup text
[10/30/2017 1:00:40 PM] page description: Rollup text
[10/30/2017 1:00:40 PM] rollup text: Rollup text
[10/30/2017 1:00:40 PM] page description: Rollup text
[10/30/2017 1:00:40 PM] rollup text: Rollup text
[10/30/2017 1:00:40 PM] page description: Rollup text
[10/30/2017 1:00:40 PM] rollup text: Rollup text
[10/30/2017 1:00:40 PM] page description: Rollup text
[10/30/2017 1:00:40 PM] rollup text: Rollup text
[10/30/2017 1:00:40 PM] page description:
[10/30/2017 1:00:40 PM] rollup text: Rollup text
[10/30/2017 1:00:40 PM] page description after update: Rollup text
Could someone point me in the right direction please? I know here they say to write directly to the treenode properties, but these ones only have a getter.
Thanks in advance