+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Getting PageNode from PageDraft

Getting PageNode from PageDraft

6 posts, 1 answered
  1. michael
    michael avatar
    17 posts
    Registered:
    15 Nov 2015
    04 Jan
    Link to this post
    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:

    http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity-/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
  2. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    04 Jan
    Link to this post
    Hello Michael,

    You can try using the executed event instead:
    PageManager.Executed

    You can also use the IDataEvent and filter by item type and status:
    http://docs.sitefinity.com/for-developers-idataevent

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. michael
    michael avatar
    17 posts
    Registered:
    15 Nov 2015
    06 Jan
    Link to this post

    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

  4. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    07 Jan
    Link to this post
    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
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
    Answered
  5. michael
    michael avatar
    17 posts
    Registered:
    15 Nov 2015
    07 Jan in reply to Nikola Zagorchev
    Link to this post
    Thanks very much for your help Nikola
  6. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    08 Jan
    Link to this post
    Hello Michael,

    I am glad you have managed to achieve the desired result.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
6 posts, 1 answered