Setting Dates

Setting Dates

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.DateField
  • Sitecore.Data.Items.Item
  • Sitecore.MainUtil

Sitecore.Configuration.Factory

  • Class that encapsulates the general information available in the web.config file
  • Contains methods that provide access to information in web.config and the means to override it

Setting Dates C# Web Control Code

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Id, "snip-output-heading");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Code Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Id, "snip-output");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);

//get Simple Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item item = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}");
if (item == null || item.Fields["example date"] == null) return;

string beforeUpdate = item.Fields["example date"].Value;
string afterUpdate = string.Empty;
using (new Sitecore.SecurityModel.SecurityDisabler())
{
    item.Editing.BeginEdit();
    try
    {
        item.Fields["example date"].Value = System.DateTime.Now.ToString();
        item.Editing.EndEdit();
        afterUpdate = item.Fields["example date"].Value;
    }
    catch (System.Exception ex)
    {
        Sitecore.Diagnostics.Log.Error("Date update failed: " + ex.Message, this);
        item.Editing.CancelEdit();
        afterUpdate = "Date update failed.";
    }
}
//using : for colon and   for space
output.Write("{0}&#58;&#160;{1}", "<br/>Date Value Before Update", "<b>" + beforeUpdate + "</b>");
output.Write("{0}&#58;&#160;{1}", "<br/>Date Value After Update", "<b>" + afterUpdate + "</b>");
output.RenderEndTag(); //Div


Setting Dates C# Web Control Output

Date Value Before Update: 8/22/2014 2:33:26 PM
Date Value After Update: 10/6/2014 8:22:46 AM

Notes

The Sitecore.Configuration.Factory class provides access to the information available in the web.config file

  1. Create an object for the database where the item to be updated is located - “master”
  2. Create an object for the target item in the database- replace itempath with the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist - Sitecore does compare case when evaluating item paths
  3. Verify the date field exists by checking that the date object is not null
  4. Open the edit state for the item
  5. Assign the current timestamp to the date object
  6. Close the edit state for the item

For example:

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item item = db.GetItem("itempath");
Sitecore.Data.Fields.DateField date = item.Fields["datetimefield"];
if (date != null)
{
      using (new Sitecore.SecurityModel.SecurityDisabler())
             {
                   item. ItemEditing.BeginEdit();
                   date.Value = Sitecore.DateUtil.ToIsoDate(DateTime.Now);
                   item. ItemEditing.EndEdit();
             }
 
}