字段值更改未保存

时间:2013-10-28 00:27:20

标签: c# sitecore editing

我正在尝试从后端动态更改字段值,但看起来没有保存更改。

代码

item从master数据库中获取。

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

更新

正如@sitecore登山者所说,我确实将我的代码改回使用 -

new Sitecore.SecurityModel.SecurityDisabler()

然而,问题是缓存。只有在我清除了缓存并重新启动浏览器后,才会在内容编辑器中显示更新的值。

为了解决这个问题,我在进行编辑之前禁用了缓存,并在编辑完成后将其重新打开。

CacheManager.Enabled = false;

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

2 个答案:

答案 0 :(得分: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();
    }                 
}

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

答案 1 :(得分: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();
    }
}
相关问题