Friday, August 29, 2014

Working With Sitecore Fields

We have 4 different ways to work with sitecore fields

  •   Simple Text Value: Excluding the Attachment system field type used to store binary data for media items, Sitecore stores all field values as text. You can access any field as a simple text value.

Database master = Sitecore.Configuration.Factory.GetDatabase ("master");
Sitecore.Data.Items.Item current = master.GetItem (Sitecore.Context.Item.ID);
              lblData.Text =  current["Page Title"] ;
  •  Fields property: You can access any field as an instance of the Sitecore.Data.Fields.Field class using the Sitecore.Data.Items.Item.Fields property.

Database master = Sitecore.Configuration.Factory.GetDatabase ("master");
Sitecore.Data.Items.Item current = master.GetItem (Sitecore.Context.Item.ID);
Sitecore.Data.Fields.Field titleField = current. Fields [“Page Title”];
If (titleField! = null)
lblData.Text = titleField. Value;

  • Using FieldRender Render Method: In presentation components we can use the Field Renderer Render method to output field values.

          lblData.Text = Sitecore.Web.UI.WebControls.FieldRenderer.Render (current, "Page Title"); 
      
  • Using FieldRender web control:

           Add a FieldRenderer web control to a layout or sublayout

           <sc:FieldRenderer runat="server" ID="fieldControl" />

            In the code behind for the layout or sublayout, set properties of the control.

            fieldControl.DataSource = current.Paths.FullPath;     

            fieldControl.FieldName = "title";

No comments:

Post a Comment