5

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
  • Are the changes committed to the database you expect? Try switching to either Web/Master to see if the changes are there. Commented Oct 28, 2013 at 0:44
  • I am trying to make changes to the master database only. Switching doesn't work either. Commented Oct 28, 2013 at 0:59
  • Have you tried to put everything in the try {} or test without the try/finally all-together? Maybe those scopes mess something up. Commented Oct 28, 2013 at 15:22
  • I'm having the same issue. Disabling cache doesn't help
    – Yuriy
    Commented Oct 4, 2019 at 13:05

2 Answers 2

2

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 .

5
  • I forgot to add that I had actually tried that too. And, it didn't work either. It loses the value on AcceptChanges() or EndEdit(). Commented Oct 28, 2013 at 6:01
  • You were right about the cache. When I cleared the cache and restarted the site, I could see the updated value in the content editor. Any idea of how I can force the value to be updating in the CE without clearing the cache? Commented Oct 28, 2013 at 22:36
  • I actually got it working by disabling caching before doing the edit and then enabling it once it was done. Thanks. Commented Oct 28, 2013 at 23:22
  • @NomadTraveler How did you disable the cache during the edit? Commented Apr 25, 2016 at 11:17
  • @MikkelNielsen I had updated my question with the working solution. Check the 'UPDATE' section Commented Apr 26, 2016 at 9:08
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.