Change Page Metadata on Document Update/Insert

SOS Childrensvillages asked on October 30, 2017 14:10

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

Correct Answer

Jan Šedo answered on October 30, 2017 15:51

The reason of the event firing multiple times will likely be that you have workflow applied to the document you're updating. Internally the document has to be touched multiple times when under the workflow so if this is causing trouble for you, you can use some of the workflow events instead, for example WorkflowEvents.CheckIn.After

2 votesVote for this answer Unmark Correct answer

Recent Answers


SOS Childrensvillages answered on October 30, 2017 16:28 (last edited on October 30, 2017 16:47)

Thanks Jan, that's sorted out the multiple events issue. Adding e.Document.Update(); did the rest (didn't work with document events, only with workflow events)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.