Field Value change not being saved

2019-06-24 00:46发布

问题:

I am trying to change a field value dynamically from back-end, but looks like the changes are not being saved.

Code

item is fetched from the master database.

   using (new EditContext(item))
   {
        item.Editing.BeginEdit();
        try
        {
            //Value is updated here from "" to Test
            item.Fields["Content"].Value = "Test";
        }
        finally
        {
            //item.Fields["Content"].Value is "" again. 
            item.Editing.AcceptChanges();
            item.Editing.EndEdit();
        }                 
    }

UPDATE

As @sitecore climber said, I did change my code back to use -

new Sitecore.SecurityModel.SecurityDisabler()

However, the issue was caching. The updated value was displayed in the content editor, only after I had cleared the cache and restarted the browser.

To get around that, I disabled caching before making the edit and turned it back on once the editing was done.

CacheManager.Enabled = false;

       using (new Sitecore.SecurityModel.SecurityDisabler())
       {
            item.Editing.BeginEdit();
            try
            {
                item.Fields["Content"].Value = "Test";
            }
            finally
            {
                item.Editing.EndEdit();
            }                 
        }
CacheManager.Enabled = true;

回答1:

Please add : (new Sitecore.SecurityModel.SecurityDisabler())

EditContext containts next lines of code :

 public EditContext(Item item)
{
  Assert.ArgumentNotNull((object) item, "item");
  this._item = item;
  this._item.Editing.BeginEdit();
}

so you don't need here if you have in your code item.Editing.BeginEdit();

your code must be :

  using (new Sitecore.SecurityModel.SecurityDisabler())
 {
    item.Editing.BeginEdit();
    try
    {
        //Value is updated here from "" to Test
        item.Fields["Content"].Value = "Test";
    }
    finally
    {
        //item.Fields["Content"].Value is "" again. 
       // Remove AcceptChanges I never use it , for editing . 
      //  item.Editing.AcceptChanges();
        item.Editing.EndEdit();
    }                 
}

I updated my answer, did you check on content editor if are any changes ? Can you clear cache, and check again. It's really strange why is not working I guess can be a caching problem .



回答2:

Try using SecurityDisabler if it helps..

 using (new Sitecore.SecurityModel.SecurityDisabler())
{
  item.Editing.BeginEdit();
    try
    {
        //Value is updated here from "" to Test
        item.Fields["Content"].Value = "Test";
    }
    finally
    {
        //item.Fields["Content"].Value is "" again. 
        item.Editing.AcceptChanges();
        item.Editing.EndEdit();
    }
}