Hi
I'm having a problem with saving content programmatically, I click a link on an email message and I'm taken to the site, on this occasion, the CMS is updated. But if I click the link again I get the following error:
Cannot save a non-current version
" at Umbraco.Core.Persistence.Repositories.Implement.DocumentRepository.PersistUpdatedItem(IContent entity)\r\n
at Umbraco.Core.Cache.DefaultRepositoryCachePolicy`2.Update(TEntity entity, Action`1 persistUpdated)\r\n
at Umbraco.Core.Persistence.Repositories.Implement.RepositoryBase`2.Save(TEntity entity)\r\n
at Umbraco.Core.Services.Implement.ContentService.<>c__DisplayClass57_0.<CommitDocumentChangesInternal>g__SaveDocument|2(IContent c)\r\n
at Umbraco.Core.Services.Implement.ContentService.CommitDocumentChangesInternal(IScope scope, IContent content, ContentSavingEventArgs saveEventArgs,
IReadOnlyCollection`1 allLangs, Int32 userId, Boolean raiseEvents, Boolean branchOne, Boolean branchRoot)\r\n
at Umbraco.Core.Services.Implement.ContentService.SaveAndPublish(IContent content, String culture, Int32 userId, Boolean raiseEvents)\r\n
at Web.Controller.Controllers.ContactController.CoachConfirmed(String id)
In V7, I use to be able to do ApplicationContext.Current.Services.ContentService.RePublishAll();, but V8 does not seem to have that option anymore
I assume that the id being passed to your controller is a version ID, and you're calling ContentService.GetVersion(id) to get the IContent that you're passing in to ContentService.SaveAndPublish?
The first time you click the link, that version will be published and a new version will be created as the unpublished version which can be edited. After that, Umbraco expects that the published version won't change, and any edits will be made to the new unpublished version.
You could check the Published property to see that the version you're trying to publish has already been published. In that case, you could do nothing and return a "this has already been published" message.
The real problems come if the page has been edited and published since the link was first clicked. Now the version you're trying to publish isn't published, but it's also not the current version. I think you'd have to use ContentService.Rollback to make your chosen version the current version, then you could publish it. However, it's probably better to show the user a message saying that the link is out-of-date rather than quietly overwrite the newer changes.
The Id > var content = contentService.GetById(new Guid(coachDocType)); is the nodeID, all I'm doing is updated two textboxes in the doctype which match the Guid Id passed in
Ah, you'll be running into this bug. The result from GetById(guid) is cached, and the cache doesn't get cleared properly when a new version is published. So the second time you hit the controller you get the same version as you did the first time, but it's no longer the current version so you can't change it.
The caching for GetById(int) works properly, so this should be a possible workaround until the fix for that bug is released:
var content = contentService.GetById(new Guid(coachDocType));
content = contentService.GetById(content.Id);
The second call to GetById will make sure that you've definitely got the current version, and not an out-of-date cached version.
Cannot save a non-current version
Hi I'm having a problem with saving content programmatically, I click a link on an email message and I'm taken to the site, on this occasion, the CMS is updated. But if I click the link again I get the following error:
Cannot save a non-current version
In V7, I use to be able to do ApplicationContext.Current.Services.ContentService.RePublishAll();, but V8 does not seem to have that option anymore
Any help would be appreciated George
I assume that the
id
being passed to your controller is a version ID, and you're callingContentService.GetVersion(id)
to get the IContent that you're passing in toContentService.SaveAndPublish
?The first time you click the link, that version will be published and a new version will be created as the unpublished version which can be edited. After that, Umbraco expects that the published version won't change, and any edits will be made to the new unpublished version.
You could check the
Published
property to see that the version you're trying to publish has already been published. In that case, you could do nothing and return a "this has already been published" message.The real problems come if the page has been edited and published since the link was first clicked. Now the version you're trying to publish isn't published, but it's also not the current version. I think you'd have to use
ContentService.Rollback
to make your chosen version the current version, then you could publish it. However, it's probably better to show the user a message saying that the link is out-of-date rather than quietly overwrite the newer changes.Hi Steve
The Id > var content = contentService.GetById(new Guid(coachDocType)); is the nodeID, all I'm doing is updated two textboxes in the doctype which match the Guid Id passed in
Regards George
Ah, you'll be running into this bug. The result from
GetById(guid)
is cached, and the cache doesn't get cleared properly when a new version is published. So the second time you hit the controller you get the same version as you did the first time, but it's no longer the current version so you can't change it.The caching for
GetById(int)
works properly, so this should be a possible workaround until the fix for that bug is released:The second call to GetById will make sure that you've definitely got the current version, and not an out-of-date cached version.
Hi Steve
Thanks for the reply, I'll fix the bug with your suggestion.
Regards George
I ran into this problem also, the second call to GetById "solved" it
is working on a reply...