0

I try to set field in template of type "Date" programmatically,

string postedValue = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals(fieldName)));
DateField dateField = newItem.Fields[fieldName];
var ss  = Sitecore.DateUtil.ToIsoDate(DateTime.Parse(postedValue));
dateField.Value = ss; 

When I debug the field is set with ISO value but in Sitecore the field is empty. any idea ??

3 Answers 3

4

You don't set the field value, you set the field.

newItem.Editing.BeginEdit();

string postedValue = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals(fieldName)));
var ss  = Sitecore.DateUtil.ToIsoDate(DateTime.Parse(postedValue));
newItem[fieldName] = ss; 

newItem.Editing.EndEdit();
0

you should use this construct:

item.Editing.BeginEdit();
make your changes
item.Editing.EndEdit();

or use the Editingcontext

using(new EditContext(item))
{
    item["FieldName"] = "A new value";
}

keep in mind there is also BulkEdititngContext(if you dont want to exp editor to reload)

3
  • His question is specifically about date field. Please update your answer for dates. This is just anything.
    – Chris Auer
    Feb 25, 2019 at 2:05
  • 1
    @Chris Auer: Regardless that the OP asked about "date field", his apparent problem, from his description, is that he doesn't know how to edit any kind of field. Ghodrat correctly surmised this and responded to his actual apparent problem. Feb 25, 2019 at 16:45
  • 1
    I am pretty sure the title says "How do you programmatically update a date field?". Not much gray area for you to read into.
    – Chris Auer
    Feb 25, 2019 at 17:04
-1

You can update field as per sample code here we are using Editing class.

Note: Don't use EditContext if you have multiple operations and anyone failed then also it partially save data.Where Editing class will work just like a transaction, so if any operation will fail it'll rollback all operations.

Item item;
item.Editing.BeginEdit();
{
    //update field value ex- date value
    item["FieldA"] = "";    
}
item.Editing.EndEdit();
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.