The Umbraco ContentService has a method to get all previous versions of a specified piece of content eg.
var versions = Services.ContentService.GetVersions(1234);
would return all the previous versions for a piece of content as IEnumerable<IContent>
So far, so good.
There is also a ContentService method called DeleteVersion(int Id, int versionId) which you pass the ID of the content and the versionID to delete. But where do you get his version ID from?
The IEnumerable<IContent> returned by GetVersions() all have the exact same ID. Example:
var versions = Services.ContentService.GetVersions(1234);
foreach (var v in versions)
{
<p>@v.Name - @v.Id | @v.PublishedVersionId</p>
}
For an example page it would return:
Example Article - 1149 | 130
Example Article - 1149 | 130
Example Article - 1149 | 130
Example Article - 1149 | 130
Each one is the same and has the same ID, published version ID etc. same create date etc. So how do you distinguish between them to target a specific version? My ultimate goal is to delete all versions except the most recent.
I think PublishedVersionId is always the version that is published,
while v.VersionId should return the specific version of the item you are looking at ?
Thanks, Kevin, that seems to be correct. Rasmus on Twitter also tweeted that, too.
So, based on that info and a few experiments I've determined that you can do something like this to delete all previous versions of a node except one, which was my original intent:
int pageId = 1234; // the Id of the node in question
var version = cs.GetById(pageId);
cs.DeleteVersion(pageId, version.PublishedVersionId, true);
Assuming cs is a reference to the current ContentService.
For anyone who might also stumble on this and care you can effectively do this for all content in your site like this:
var cs = Services.ContentService;
var siteContent = new List<IContent>();
var rootContent = cs.GetRootContent();
foreach (var content in rootContent)
{
siteContent.AddRange(cs.GetPagedDescendants(content.Id, 0, int.MaxValue, out long total));
}
foreach (var content in siteContent)
{
cs.DeleteVersion(content.Id, content.PublishedVersionId, true);
}
So this fetches all site content and then loops through it, deleting all the old versions. I'd always take a backup of your DB before doing this, of course, and take no responsibility for any mistakes that might arise from doing this :)
Using Content Service to delete specific versions
The Umbraco ContentService has a method to get all previous versions of a specified piece of content eg.
would return all the previous versions for a piece of content as
IEnumerable<IContent>
So far, so good.
There is also a
ContentService
method calledDeleteVersion(int Id, int versionId)
which you pass the ID of the content and the versionID to delete. But where do you get his version ID from?The
IEnumerable<IContent>
returned byGetVersions()
all have the exact same ID. Example:For an example page it would return:
Each one is the same and has the same ID, published version ID etc. same create date etc. So how do you distinguish between them to target a specific version? My ultimate goal is to delete all versions except the most recent.
Hi
I think
PublishedVersionId
is always the version that is published, whilev.VersionId
should return the specific version of the item you are looking at ?Thanks, Kevin, that seems to be correct. Rasmus on Twitter also tweeted that, too.
So, based on that info and a few experiments I've determined that you can do something like this to delete all previous versions of a node except one, which was my original intent:
Assuming
cs
is a reference to the currentContentService
.For anyone who might also stumble on this and care you can effectively do this for all content in your site like this:
So this fetches all site content and then loops through it, deleting all the old versions. I'd always take a backup of your DB before doing this, of course, and take no responsibility for any mistakes that might arise from doing this :)
is working on a reply...