Custom DocumentUrlPath for documents of a particular page type

Shweta S asked on May 17, 2019 21:10

Hi, I have a page called News and I am creating folders for each year and adding document of page type CMS.News under each year folder. I do not want to have each news detail page to have url as /News/2019/SomeNewsTitle. So in CustomInitializationModule file, I wrote the following code.

protected override void OnInit()
{
    base.OnInit();
    DocumentEvents.Insert.Before += SetDocumentURL;
    DocumentEvents.Update.Before += SetDocumentURL;
}

private void SetDocumentURL(object sender, DocumentEventArgs e)
{
    TreeNode node = e.Node;

    if (node.ClassName.ToLower() == "CMS.News")
    {
        var doc = DocumentHelper.GetDocument(node.NodeID, new TreeProvider());
        if (doc != null)
        {
            node.DocumentUrlPath = "/News/" + node.DocumentName;
            node.DocumentUseNamePathForUrlPath = false;
            node.Update();
        }

    }
}

Note: Workflow and Versioning is enabled.

So when I create a new page, it sets correct path in Properties -> Urls. The problem is when I update the document name it does not update the path in Properties -> Urls

What am I doing wrong?

Thanks

Recent Answers


Mike Wills answered on May 17, 2019 21:21

Hi Shweta,

This is just a start. If the documents are under workflow, you need to use events from the WorkflowEvents class instead of DocumentEvents. For example, to handle when the document is updated use the SaveVersion event.

Mike

1 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on May 20, 2019 09:00

Or, you do not need to use Workflow events, just use the workflow API in the handler code to properly create and save given version of the page.

0 votesVote for this answer Mark as a Correct answer

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