sitecoresitecore7sitecore7.5

Sitecore how to edit an Items's Standard Field Value


In Sitecore I have an Item. Lets say its name is weekDay. It has a default field (Standard Fields) Sortorder. I want to edit this field and put a string inside it. But following code give me System.NullReferenceException: Object reference not set to an instance of an object.

switch (weekDay.Name.ToLower())
{
    case "monday":
        weekDay.Editing.BeginEdit();
        weekDay.Fields["Sortorder"].Value = "1";
        weekDay.Editing.EndEdit();
        break;
}

I am getting exception on this line weekDay.Fields["Sortorder"].Value = "1";in above code.

Any help would be really appreciated. Thanks!!


Solution

  • Your code is ok. The only issue is that the field is not called "Sortorder", it's called "__Sortorder". Try:

    weekDay.Fields["__Sortorder"].Value = "1";
    

    Most of the Sitecore standard fields is prefixed with double underscore, e.g. __Sortorder, __Hidden, __Display Name, __Read Only, etc.