Breaking change in GetChildren()

by: Mari Jørgensen


Let’s say that you need to get hold of all child pages of a certain parent in the EPiServer tree (and by ‘all’ I mean also those not published). With some knowledge of the API, you would quickly assume that the GetChildren method would be the way to go.

The breaking change

Prior to EPiServer CMS 5, the GetChildren method would return all child pages, also including any pages not published yet.

In version 5, this functionality has changed:

This methods does not filter any pages due to access rights, but it will filter pages not published for the preferred culture

EPiServer CMS 5 SDK GetChildren method

A workaround

A possible solution (but probably not ‘best practice’) would be to use the parent reference as a Criteria and then find all child pages using the FindPageWithCriteria method.

   1: PageReference examples = new PageReference(21);
   2: 
   3: PropertyCriteria parentCr = new PropertyCriteria();
   4: parentCr.Type = PropertyDataType.PageReference;
   5: parentCr.Value = examples.ID.ToString();
   6: parentCr.Name = "PageParentLink";
   7: parentCr.Condition =
   8:     EPiServer.Filters.CompareCondition.Equal;
   9: 
  10: PropertyCriteriaCollection col =
  11:     new PropertyCriteriaCollection();
  12: col.Add(parentCr);
  13: 
  14: PageDataCollection allChildren =
  15:  DataFactory.Instance.FindPagesWithCriteria(examples, col);
 

17 February 2009


Comments

  1. Your code looks pretty much like my GetChildren method I have made :). Dont know of any otherr way of get unpublished pages, so we can well call it "Only practice" :)
  2. I would try: PageDataCollection locations = DataFactory.Instance.GetChildren(LocationIndexPage.PageLink, LanguageSelector.AutoDetect(true)); The key is using a Language Selector with fallback to the Master Language if there is no page in the current content language./Fredrik
  3. Anders: To get unpublished versions (talking about status: Not ready, Ready to publish and Previously published) you must use: PageVersionCollection pageVersions = PageVersion.List(page.PageLink); /Fredrik
  4. Fredrik. The problem is to find the children of a given page. PageVersion.List I guess will only returns that page version.
  5. Anders: Yes, GetChildren will return unpublished pages but only when used with LanguageSelector.AutoDetect(true).

    But the last time I tested you GetChildren always returned skelleton PageData objects for unpublished pages. That is a you get ID and Name but all user defined properties are null. To get a full PageData object for a unpublished version you have to use PageVersion class for each unpublished page returned by GetChildren.

  6. Fredrik: Cool, didnt know that, so Mari should have used GetChildren(pageRef,LanguageSelector.AutoDetect(true)) to get all child nodes to a pageRef. Have to test this myselft :)
  7. Correction: You need PageVersion to get the WorkID so GetPage can return PageData for an unpublished version. Read more: http://blog.fredrikhaglund.se/blog/2009/02/18/work-with-unpublished-pagedata-from-code/
  8. Thanks Fredrik! Passing the 'LanguageSelector.AutoDetect(true)' parameter into the GetChildren() method worked like a charm!
Post a comment    
User verification Image for user verification  
EPiTrace logger