Glass just makes me so happy! I do not have to write same 10-15 lines of code every time to do something very mundane when it comes to Sitecore. Every other feature on sitecore demands some kind of relationship driven items to be accessible from context page on which your module would do some magic on.
Typically, developers tend to write their own custom code to do such things which would lead to bad performance when not done right. Why not just go with Glass if you are using it in your instance development? It is far more easier. I had done a similar blog post to grab children from Glass, but, this time around it is Ancestors. Though similar, it could help some one who did not think or know that it could be used on both the relation ships on the tree, upwards or downwards.
Steps to do this is quite simple and three step process
Typically, developers tend to write their own custom code to do such things which would lead to bad performance when not done right. Why not just go with Glass if you are using it in your instance development? It is far more easier. I had done a similar blog post to grab children from Glass, but, this time around it is Ancestors. Though similar, it could help some one who did not think or know that it could be used on both the relation ships on the tree, upwards or downwards.
Steps to do this is quite simple and three step process
- Declare a custom partial class that extends your auto generated class based on say a template. For example like below. I like to keep these guys organized, usually do it in special Model project and namespace related to it, to keep things easy and manageable, so some one can easily know if the model has been extended.
- Next up, add a Attribute that comes with Glass such as below, pass your template ID. I actually tried getting this ID from a config setting which would have been great for maintenance, see if you can do that based on your project settings
- Access the new ancestor strongly typed( A bonus, so you dont need to cast and make a messy view, the view simply get's what it needs and you access and ensure your checks are in place)
Sample Code Of Custom Partial Class
namespace YourProject.Models
{
public partial class Your_Template_Auto_Generated_Class
{
[SitecoreQuery("./ancestor-or-self::*[@@templateid='{7E6BC980-362C-4BBB-97E5-9DAA83B70812}']", IsRelative = true)]
public virtual Your_Strongly_Typed_class myAncerstorItem { get; set; }
}
}
Voila! Goal accomplished and the best part clean code, clean views and happy MVC.
Have a great weekend ahead!!!
No comments :
Post a Comment