Nom*_*ler 5 c# editing sitecore
我正在尝试从后端动态更改字段值,但看起来更改没有被保存。
代码
item是从主数据库中获取的。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
更新
正如@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;
Run Code Online (Sandbox Code Playgroud)
小智 2
请添加:(new Sitecore.SecurityModel.SecurityDisabler())
EditContext 包含下一行代码:
public EditContext(Item item)
{
Assert.ArgumentNotNull((object) item, "item");
this._item = item;
this._item.Editing.BeginEdit();
}
Run Code Online (Sandbox Code Playgroud)
所以如果你的代码中有 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我更新了我的答案,您检查内容编辑器是否有任何更改?能不能清一下缓存,再检查一下。这真的很奇怪,为什么不起作用我想可能是缓存问题。