简体   繁体   中英

how can I verify the existence of a field item and change its value?

I have to check that the item has the field "In training", if that field exists and is different from 1, I have to set it to 1.

var field = "In Allenamento";
        var value = "0";
        var edited = (field = "1");

       if (field != null && field == value)
        {
            field = "1";

        }

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                item.Editing.BeginEdit();
                item.Fields[field].Value = field;
                item.Editing.EndEdit();
                log.AppendLine(item.ID + "edited");
            }
       
    }

I am aware that I have written nonsense, so I ask for support

  1. Take the field by its name ("In Allenamento" or "In Training")
  2. If field is null it means that it does not exist on that item
  3. Check if its value is not "1"
  4. Start editing
  5. Update value
  6. End editing
using (new Sitecore.SecurityModel.SecurityDisabler())
{
    var inTrainingField = item.Fields["In Allenamento"];
    if (inTrainingField != null && inTrainingField.Value != "1")
    {
        item.Editing.BeginEdit();
        item.Fields["In Allenamento"].Value = "1";
        item.Editing.EndEdit();
        log.AppendLine(item.ID + "edited");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM