0
votes

Quite new to Sitecore and would like to understand the best way to access sitecore items. I've seen couple of ways:

  1. Create Page ID field and get all items for given template and folder. Then do linq query on page ID.

  2. Store all Page ID (Sitecore Item ID) on Constants file. Use this to query Sitecore using GetItem(itemID) API.

Could someone please suggest what's the best practice. Either way, I can see that there will be huge Constants file containing either custom Page ID or Sitecore Item ID.

My worry is do we really need to manage this Constants file or is there an elegant way to query CMS contents for given Page.

Thanks.

2

2 Answers

1
votes

Approach 1 seems a bit odd. I don't really see why you would get a collection of items first when you already have the ID, but maybe I've misunderstood what you're saying.

I think a combination of 2 things you mention is best.

Constants classes are good for "landmark" items that will always definitely be there, so its fine for the GUID to be in code. Some of these landmark items can be "configuraton items" that have fields containing the IDs of other items (These fields might have names like "Templates allowed in search"). This approach allows some flexibility for change by Sitecore users as the site evolves.

If you're concerned about the management of a constants file, I have to wonder how many items you need to access by ID. Sitecore items have properties like Parent and Children. You can also find items by template type etc.

This approach works well for me. I certainly don't think there is a more elegant way of getting strongly typed references to Sitecore items.

1
votes

Personally, I prefer not to use constants files. What happens if your client or one of your developers deletes one of those items and creates a new one? One of the fundamental principles of Sitecore is that items can be added and removed by users who are not necessarily "technical" personnel.

Sitecore provides "Insert Options" so that you can specify what types of items can be added to each folder, and also grants the ability to protect certain items from deletion, via Roles and User Permissions. What does this mean conceptually? It means that Sitecore is set up such that the system architects/developers can create a structure that is not to be violated, while the content editors can add or remove content within that structure. In other words, Sitecore is designed to provide a framework in which the items can change but the location of each type of item is pre-determined.

As such, I suggest that you use the Custom Item Generator module available in the Sitecore Marketplace (free). CIG generates C# class representations (models) of your templates, and makes all fields into properties (I don't want to get too off-topic, but this is an awesome feature of CIG, especially when working with newer developers). You can add your own methods to your CIG classes for getting children of a particular type. For example, on a site in which the Profile Page is a direct child of the Homepage the following method could be added to the CIG HomepageItem.instance.cs file's HomepageItem partial class:

...
public partial class HomepageItem 
{
    public ProfilePageItem GetProfilePage() 
    {
        //note that .IsOfType(...) is pseudo-code and not a real method, but I do 
        //   suggest that you define an extension for it
        return InnerItem.Children.FirstOrDefault(i => i.IsOfType(ProfilePageItem.TemplateId));
    }
}


Be sure that you are assigning Insert Options to restrict the types of items that can be added as children to each item you make (add them on the Standard Values and not on the individual content items). I also suggest that you make a separate Template that inherits from Common/Folder for every folder that you use in the content tree. This way you can use CIG for your entire structure, via:

...in your Globals Item's CIG class...
public partial class GlobalsItem
{
    public SlidesFolderItem GetSlidesFolder() 
    {
        return InnerItem.Children.FirstOrDefault(i => i.IsOfType(SlidesFolderItem.TemplateId));
    }
}

...in your Slides Folder Item's CIG class...
public partial class SlidesFolderItem
{
    public IEnumerable<SlideItem> GetSlides() 
    {
        return InnerItem.Children.Where(i => i.IsOfType(SlideItem.TemplateId));
    }
}

and then you can get your Slide items via:

...
var slidesFolder = globals.GetSlidesFolder();
var slides = slidesFolder != null ? slidesFolder.GetSlides() : null;


Remember that CIG, Insert Options, and templates for each type of folder will enable you to create an iron-clad structure for your site that will not break if content editors make unexpected changes like replacing items.


Let me know if you have any questions on this. Good luck, and Happy Coding! :)