Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 13:21
    Mus'ab
    0

    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 ?

  • Kevin Jump 2136 posts 13510 karma points MVP 6x c-trib
    Feb 10, 2020 @ 13:49
    Kevin Jump
    0

    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:

    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.

  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 14:45
    Mus'ab
    0

    Hi Kevin

    is this code valid for umbraco 8 ?

  • Kevin Jump 2136 posts 13510 karma points MVP 6x c-trib
    Feb 10, 2020 @ 14:49
    Kevin Jump
    0

    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

  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 14:49
    Mus'ab
    0

    and how can i get the item ?

  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 14:56
    Mus'ab
    0

    Hi Kevin can you help me a little bit and tell me how can i get the item from a content service ?

  • Kevin Jump 2136 posts 13510 karma points MVP 6x c-trib
    Feb 10, 2020 @ 15:07
    Kevin Jump
    0

    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.

    // 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
    }
    
  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 15:18
    Mus'ab
    0

    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 ?

  • Kevin Jump 2136 posts 13510 karma points MVP 6x c-trib
    Feb 10, 2020 @ 15:30
    Kevin Jump
    0

    Hi

    Relation Alias is relateParentDocumentOnDelete You can use the Umbraco Constant of Constants.Conventions.RelationTypes.RelateParentDocumentOnDeleteAlias to be sure.

    i think you move it back:

            contentService.Move(item, parentId)
    
  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 15:40
    Mus'ab
    0

    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 ?

  • Mus'ab 157 posts 384 karma points notactivated
    Feb 10, 2020 @ 15:45
    Mus'ab
    0

    how can i get IUmbracoEntity of node ? i tried this but not working

    enter image description here

  • Kevin Jump 2136 posts 13510 karma points MVP 6x c-trib
    Feb 10, 2020 @ 16:08
    Kevin Jump
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft