Reputation: 4100
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!!
Upvotes: 1
Views: 407
Reputation: 1170
Most standard fields are prefixed with __
(double underscore), so the field name is actually "__Sortorder"
.
weekDay.Fields["__Sortorder"].Value = "1";
// Or use the field ID from Sitecore.FieldIDs class
weekDay.Fields[Sitecore.FieldIDs.Sortorder].Value = "1";
The Sitecore.FieldIDs
class contains the field ID of many, if not all, standard fields.
Upvotes: 1
Reputation: 4118
Can you check if weekDay is null ? I guess weekDay item is null
To edit an weekDay item you will have :
if (weekDay!=null)
{
using (new EditContext(weekDay))
{
switch (weekDay.Name.ToLower())
{
case "monday":
weekDay.Fields["__Sortorder"].Value = "1";
break;
}
}
}
Upvotes: 0
Reputation: 27132
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.
Upvotes: 1