Monday, September 1, 2014

Working With Sitecore Simple Type Fields

Checkbox field:

Checkbox field stores the one character (“1”) when selected.

Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase ("master"); Sitecore.Data.Items.Item home = master.GetItem ("/sitecore/content/home"); Sitecore.Data.Fields.CheckboxField checkboxField = home.Fields ["checkboxfield"]; 
if (checkboxField != null && checkboxField.Checked)
 //TODO
}

Date and Date time fields:

You can use the Sitecore.Data.Fields.DateField class to access data template fields of type Date and Datetime. The Sitecore.Data.Fields.DateField.Value property contains the date and time as a string in the ISO format used by Sitecore (yyyyMMddTHHmmss). You can convert a value in the ISO format to a System.DateTime structure using the Sitecore.DateUtil.IsoDateToDateTime() method. Alternatively, you can access the Sitecore.Data.Fields.DateField.DateTime property.

Sitecore.Data.Fields.DateField dateField = current.Fields ["Date Time"];
if(dateField != null)
{
    DateTime dt1 = Sitecore.DateUtil.IsoDateToDateTime(dateField.Value);
    DateTime dt2 = dateField.DateTime;
}

Image field:

Sitecore.Data.Fields.ImageField imgField = current.Fields ["Image"];
if (imgField != null)
{
     Image1.ImageUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl (imgField.MediaItem);
 }

File field:

Sitecore.Data.Fields.FileField fileField = current.Fields ["File"];
if (fileField! = null)
{
     Image1.ImageUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl (fileField.MediaItem);
 }

Word Document field:

Sitecore.Data.Fields.WordDocumentField class is used to access the value of the Word Document field. Sitecore.Data.Fields.WordDcocumentField.Html property is used to access the HTML representation of the field value. Sitecore.Data.Fields.WordDocumentField.PlainText property is used to access the plain text representation of the field value. Sitecore.Data.Fields.WordDocumentField.Styles property is used to access the cascading style sheet code associated with the HTML.

Sitecore.Data.Fields.WordDocumentField wordField = current.Fields ["Word Document"];
if (wordField! = null)
{
      // ltPageData.Text = wordField.Html;
         ltPageData.Text = wordField.PlainText;

 }

No comments:

Post a Comment