4

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;
4

2 回答 2

2

请添加:(新的 Sitecore.SecurityModel.SecurityDisabler())

EditContext 包含下一行代码:

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

因此,如果您的代码中有 item.Editing.BeginEdit(); 则不需要此处

您的代码必须是:

  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();
    }                 
}

我更新了我的答案,您是否检查了内容编辑器是否有任何更改?你能清除缓存,然后再检查一次。真的很奇怪为什么不工作我猜可能是一个缓存问题。

于 2013-10-28T04:34:26.493 回答
0

如果有帮助,请尝试使用 SecurityDisabler..

 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();
    }
}
于 2013-10-28T06:46:27.073 回答