Feed on
Posts
Comments

Next to the Util-classes in the root-namespace of Sitecore. There are also lots of constants defined in different classes. It are to many of them and describing would definitelly take to much time, so I’ve just capy and pasted the full list with the documentation written by the Sitecore developer self:

  • ArchivedItemFieldIDs – Fields of the Tasks/Command template.
  • CommandFieldIDs – Fields of the Tasks/Command template.
  • CommandsCommandFieldIDs - Fields of the Commands/Command template.
  • DeviceFieldIDs – Fields of the Layout/Device template.
  • FieldIDs – Implements a static class for holding well-known GUIDs relating to fields.
  • ItemIDs – Implements a static class for holding assorted well-known GUID values.
  • LayoutFieldIDs – Fields of the Layout/Layout template.
  • PackageRegistrationFieldIDs – Fields of the ‘Packages/Package registration’ template.
  • RoleFieldIDs – Fields of the Security/Role template.
  • ScheduleFieldIDs – Fields of the Tasks/Schedule template.
  • SecurityDatabaseTemplateIDs – Implements a static class for holding well-known GUIDs relating to templates.
  • ShellFieldIDs – Implements a static class for holding assorted well-known GUID values.
  • SystemFieldIDs – Implements a static class for holding well-known GUIDs relating to template fields.
  • TemplateFieldIDs – Implements a static class for holding well-known GUIDs relating to template fields.
  • TemplateIDs – Implements a static class for holding well-known GUIDs relating to templates.
  • TemplateSectionFieldIDs – Implements a static class for holding well-known GUIDs relating to template sections.
  • UserFieldIDs – Fields of the Security/User template.
  • WorkflowFieldIDs – Implements a static class for holding assorted well-known GUID values.

So how are these FieldIDs used? Well they are actually the core of the system. I’ll show you how you can implement your own simple ’ContextItem’.
First of all we create a ContextItem constructor with 1 argument: an innerItem where we can retrieve the data from.
And after that, we introduce the 2 fields, Created and Published with some dummy values.

    1 using System;

    2 using Sitecore;

    3 using Sitecore.Data.Items;

    4 namespace SitecorePlayground.Examples

    5 {

    6     class ContextItem

    7     {

    8         private Item _innerItem;

    9         public ContextItem(Item innerItem)

   10         {

   11             this._innerItem = innerItem;

   12         }

   13 

   14         public DateTime Created

   15         {

   16             get { return DateTime.Now;  }  

   17         }

   18         public DateTime Published

   19         {

   20             get { return DateTime.Now; }

   21         }

   22     }

   23 }

After that, we’ve to retrieve the fields from Sitecore. We’ll use our innerItem for data. But now, the fieldnames… Names? No we are going to use Sitecore.FieldIDs. The code will look like this:

    1         public DateTime Created

    2         {

    3             get { return this._innerItem.Fields[Sitecore.FieldIDs.Created];  }  

    4         }

But this will not compile, we have to convert the Value of the field to a DateTime:

    1         public DateTime Created

    2         {

    3             get {

    4                 string fieldValue = this._innerItem.Fields[Sitecore.FieldIDs.Created].Value;

    5                 return Sitecore.DateUtil.ParseDateTime(fieldValue, DateTime.Now);

    6             }  

    7         }

Finally the code will look like this, but can be highly improved by checking if the field do actually exist, checking if the innerItem is not null, etc:

    1 using System;

    2 using Sitecore;

    3 using Sitecore.Data.Items;

    4 namespace SitecorePlayground.Examples

    5 {

    6     class ContextItem

    7     {

    8         private Item _innerItem;

    9         public ContextItem(Item innerItem)

   10         {

   11             this._innerItem = innerItem;

   12         }

   13 

   14         public DateTime Created

   15         {

   16             get {

   17                 string fieldValue = this._innerItem.Fields[Sitecore.FieldIDs.Created].Value;

   18                 return Sitecore.DateUtil.ParseDateTime(fieldValue, DateTime.Now);

   19             }  

   20         }

   21         public DateTime Published

   22         {

   23             get

   24             {

   25                 string fieldValue = this._innerItem.Fields[Sitecore.FieldIDs.PublishDate].Value;

   26                 return Sitecore.DateUtil.ParseDateTime(fieldValue, DateTime.Now);

   27             }

   28         }

   29     }

   30 }

Explore the classes metioned above using on SDN or download a CHM-file and take advantage of this provided functionality!

Leave a Reply