How to restore nodes from recycle bin using content service ?
Hi
is there any way to restore a node from recycle bin using content service
for example some thing like the opposite of move to recycle bin method
or using ordinary move if there is any way to know the original parent id ?
When an item is deleted the old parent is stored in a Umbraco Relation of type relateParentDocumentOnDelete(Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias)
to restore an item you need to get the relation for it out of this service.
so if you have a reference to the relations service.
you can get the parent relation:
var parents = relationService.GetByChild(item, relationAlias);
and if everything is ok and the data isn't corrupted the first one will be a relation to the old parent
if (parents != null && parents.Any())
{
return parents.FirstOrDefault().ParentId;
}
you can then use this parentId to restore the content to the place it was.
Yeah, depending on where you are calling it from, you will either need to inject the RelationsService, or access it via Services.RelationsService. but this code is from uSync for v8.
yeah, it does depend a bit on where you are getting the reference to the item from?
if your code is in a Controller then the content service is available at Services.ContentService - if its standalone code, you need to look at Composers / Components and inject the ContentService into the class.
// content by id (e.g. 1029)
var item = contentService.GetById(id);
// content by GUID
var item = contentService.GetById(key);
// content by the UDI (e.g udi://document/some-guid-value )
if (udi is GuidUdi guidUdi)
{
var item = contentService.GetById(guidUdi.Guid);
}
/// items in the recylce bin
/// you would want to loop this propertly!
long total = 0;
var recycledContent = contentService.GetPagedContentInRecycleBin(0, 100, out total);
foreach(var item in recycledContent)
{
// content it.
// item.Id
}
yeah my code is in umbraco surface controller
i am looping in the recycledContent now
after i found the node that i want to restore what should i do ?
get the original parent id and move it ?
if yes how can i get it ?
using
var parents = relationService.GetByChild(item, relationAlias);
right ? what is the relationAlias value ?
Relation Alias is relateParentDocumentOnDeleteYou can use the Umbraco Constant of Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias to be sure.
How to restore nodes from recycle bin using content service ?
Hi
is there any way to restore a node from recycle bin using content service for example some thing like the opposite of move to recycle bin method or using ordinary move if there is any way to know the original parent id ?
Hi,
When an item is deleted the old parent is stored in a Umbraco Relation of type
relateParentDocumentOnDelete
(Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias)to restore an item you need to get the relation for it out of this service. so if you have a reference to the relations service.
you can get the parent relation:
and if everything is ok and the data isn't corrupted the first one will be a relation to the old parent
you can then use this parentId to restore the content to the place it was.
Hi Kevin
is this code valid for umbraco 8 ?
Hi
Yeah, depending on where you are calling it from, you will either need to inject the RelationsService, or access it via Services.RelationsService. but this code is from uSync for v8.
https://github.com/KevinJump/uSync8/blob/v8/8.2/uSync8.ContentEdition/Serializers/ContentSerializerBase.cs#L75
and how can i get the item ?
Hi Kevin can you help me a little bit and tell me how can i get the item from a content service ?
Hi,
yeah, it does depend a bit on where you are getting the reference to the item from?
if your code is in a Controller then the content service is available at
Services.ContentService
- if its standalone code, you need to look at Composers / Components and inject the ContentService into the class.Hi
yeah my code is in umbraco surface controller i am looping in the recycledContent now after i found the node that i want to restore what should i do ? get the original parent id and move it ? if yes how can i get it ?
using var parents = relationService.GetByChild(item, relationAlias); right ? what is the relationAlias value ?
Hi
Relation Alias is
relateParentDocumentOnDelete
You can use the Umbraco Constant ofConstants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias
to be sure.i think you move it back:
Hi kevin
one more question please what about item can i pass it as Icontent or i have to pass it in a different way ?
how can i get IUmbracoEntity of node ? i tried this but not working
I believe you can cast a content item to an IUmbracoEntity. so you should be able to pass the content in without using the entity service.
is working on a reply...