Getting PageNode from PageDraft
Hi there,
I want to create a dynamic module item for pages automatically when they're created.
The dynamic module has a page "related item" which I want to set to the new page that's being created.
I've hooked into the PageManger.Executing event as described here:
www.sitefinity.com/.../what-event-is-raised-when-a-page-is-published-and-how-do-i-capture-it
... however, I am having trouble getting the linked page item with this code snippet from the dynamic module ...
PageManager linkedPageManager = PageManager.GetManager();
var linkedPageItem = linkedPageManager.GetPageNodes().FirstOrDefault(p => p.Id == page.Id);
if (linkedPageItem != null)
// This is how we relate an item
articleItem.CreateRelation(linkedPageItem, "LinkedPage");
... linkedPageItem is always null, I presume because the "page" object is a PageDraft and not a PageNode.
Is what I'm trying to do achievable? Is there a way to get the Id of the new page at this point in time? (in the PageManager_Executing event)
Thanks,
Michael
Hello Michael,
You can try using the executed event instead:
PageManager.Executed
Thanks for the reply Nikola. I'll check out your suggestions, but I ended up getting it working using ParentPage.NavigationNodeId like this:
PageManager linkedPageManager = PageManager.GetManager();
PageNode linkedPageItem = linkedPageManager.GetPageNodes().FirstOrDefault(p => p.Id == page.ParentPage.NavigationNodeId);
... can you see any problems with this approach? It seems to be working fine for what I need, but am not sure if there are scenario's where this will fail (?)
Cheers,
Michael
Hello Michael,
Your query can result in Null reference exception if the page node does not have a parent - in a case where ParentPage returns null.
The ParentPage property will return a PageData object and its NavigationNode is Id of the PageNode the data is linked to.
You can also refer to the following articles:
http://docs.sitefinity.com/for-developers-query-pagedata-by-pagenode-id
http://docs.sitefinity.com/for-developers-query-pagedata-by-url-of-the-pagenode
Regards,
Nikola Zagorchev
Telerik
Thanks very much for your help Nikola
Hello Michael,
I am glad you have managed to achieve the desired result.
Regards,
Nikola Zagorchev
Telerik
Hi, I'm also trying to register an event and use the publish event to trigger some custom code. I've used this article to register my event: www.sitefinity.com/.../what-event-is-raised-when-a-page-is-published-and-how-do-i-capture-it
However, I'm not sure how I can get the page URL of the page thats being published. Can someone provide sample code on how to retrieve the page URL of the page thats being published when the publish event is triggered?
Thanks,
Allen