How to get children of a item using SitecoreContext?

1,330 views
Skip to first unread message

Per Bering

unread,
Jun 7, 2013, 5:40:32 PM6/7/13
to glasssite...@googlegroups.com
Hi, 

Using SitecoreContext, how do I get the children of a item? Is there a _context.GetItems<T>(Guid id / string path) method someplace or should I use _context.Query? Does the Query method take just a path or id or does it have to be a Sitecore Query?

/Per 

Michael Edwards

unread,
Jun 8, 2013, 3:59:11 AM6/8/13
to glasssite...@googlegroups.com

Per
I  am away this weekend, I will send you a detailed response on Monday.

Mike

Hi, 

Using SitecoreContext, how do I get the children of a item? Is there a _context.GetItems<T>(Guid id / string path) method someplace or should I use _context.Query? Does the Query method take just a path or id or does it have to be a Sitecore Query?

/Per 

--
You received this message because you are subscribed to the Google Groups "Glass.Sitecore.Mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glasssitecorema...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Edwards

unread,
Jun 10, 2013, 8:25:49 AM6/10/13
to glasssite...@googlegroups.com
Per

To get children:

OnDemand:

public class MyModel{

  public virtual IEnumerable<MyOtherModel> Children{ get; set;}

}

Attribute:

[SitecoreType]
public class MyModel{

   [SitecoreChildren]
   public virtual IEnumerable<MyOtherModel> SomePropertyName{ get;set;}

}

Fluent:

var model = loader.Add<MyModel>();
model.Children(x=> x.SomePropertyName);


You can also filter children by using a relative query:

[SitecoreType]
public class MyModel{


   [SitecoreQuery("./*[@@templatename='News']", IsRelative=true)]
   public virtual IEnumerable<NewsStories> NewsStories{get;set;}

}

Cheers

Mike

Per Bering

unread,
Jun 10, 2013, 8:58:56 AM6/10/13
to glasssite...@googlegroups.com
mmm my question is more how to get the children of a Guid? Just like Query("./*[@@templatename='News']")? Without Glass I would have done something like:

var database = Factory.GetDatabase("web");
var targetItem = database.Items.GetItem(someId);
var children = targetItem.Children;

I got this working in my controller:

_context.Query<ArticleModel>("//*[@@id = '" + settings.Source.ToString("B").ToUpperInvariant() + "']/*")

I just worry that doing a Sitecore Query is not as permanent as item.Children. Any thoughts?




--
You received this message because you are subscribed to a topic in the Google Groups "Glass.Sitecore.Mapper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/glasssitecoremapper/w5acXTLw8J8/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to glasssitecorema...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages