Copied to clipboard

Flag this post as spam?

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


  • Pedro Marinho 10 posts 80 karma points
    Oct 10, 2017 @ 10:12
    Pedro Marinho
    0

    How to refresh content tree in backoffice using code from server

    Hello,

    I have implemented a feature that copies nodes from a location to another. If the destination already has the nodes I have to delete them and then create the new ones. This part is working, but the nodes in the content tree are not updating, so when the user tries to see the new nodes he's presented with an error message saying "Request error: The URL returned a 404 (not found): /umbraco/backoffice/UmbracoApi/Content/GetById". The user has to "Reload" the nodes manually.

    I'm implementing this feature using the event ContentService.Saved (ApplicationEventHandler)

    I've tried to use the following functions but without luck.

    new Umbraco.Web.UI.Pages.ClientTools(new Umbraco.Web.UI.Pages.UmbracoEnsuredPage()).SyncTree(parentContent.Path, true).RefreshTree();
    

    Another away

    umbraco.library.RefreshContent();
    umbraco.BasePages.BasePage.Current.ClientTools.ClearClientTreeCache();
    

    The content tree does not show the new content. What I'm doing wrong?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 10, 2017 @ 14:32
    Alex Skrypnyk
    0

    Hi Pedro

    Can you show the code that moves nodes?

    Thanks,

    Alex

  • Pedro Marinho 10 posts 80 karma points
    Oct 10, 2017 @ 14:56
    Pedro Marinho
    0

    Hi Alex

    The code is something like this:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationStarted(umbracoApplication, applicationContext);
            ContentService.Saved += CopyNodes;
        }
    
        private static void CopyNodes(IContentService cs, SaveEventArgs<IContent> e)
        {
            foreach (var content in e.SavedEntities)
            {
               CopyNodes(content, true);
            }
        }
    
        private static bool CopyNodes(dynamic content, bool copyChildreen)
        {
            string PROPERTY_NAME = "copyTo";
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    
            if (content.HasProperty(PROPERTY_NAME))
            {
                var copyToValue = content.GetValue<string>(PROPERTY_NAME);
                if (copyToValue != null)
                {
                    Udi udiValue = null;
                    if (!Udi.TryParse(copyToValue, out udiValue))
                    {
                        return false;
                    }
                    var destId = umbracoHelper.GetIdForUdi(udiValue);
                    if(destId < 0)
                    {
                        return false;
                    }
    
                    var parentContent = ApplicationContext.Current.Services.ContentService.GetById(destId);
                    var sameNodes = parentContent.Children().Where(x => x.ContentType.Alias.IndexOf(content.ContentType.Alias, StringComparison.InvariantCultureIgnoreCase) > -1 && x.Name == content.Name);
    
                    if (sameNodes.Count() > 0)
                    {
                        foreach (var node in sameNodes)
                        {
                            ApplicationContext.Current.Services.ContentService.UnPublish(node);
                            ApplicationContext.Current.Services.ContentService.Delete(node);
                        }
                    }
    
                    var contentTypeAlias = parentContent.ContentType.AllowedContentTypes.Where(x => x.Alias.IndexOf(content.ContentType.Alias, StringComparison.InvariantCultureIgnoreCase) > -1).FirstOrDefault();
    
                    if (contentTypeAlias == null)
                    {
                        return false;
                    }
    
                    var destContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(contentTypeAlias.Alias);
    
                    var template = destContentType.AllowedTemplates.Where(x => x.Alias.IndexOf(content.Template.Alias, StringComparison.InvariantCultureIgnoreCase) > -1).FirstOrDefault();
    
                    Content cloneDocument = new Content(content.Name, destId, destContentType);
                    cloneDocument.CreateDate = content.CreateDate;
                    cloneDocument.CreatorId = content.CreatorId;
                    cloneDocument.WriterId = content.WriterId;
                    cloneDocument.Template = template;
    
                    foreach (var property in content.Properties)
                    {
                        if (cloneDocument.HasProperty(property.Alias))
                        {
                            cloneDocument.SetValue(property.Alias, property.Value);
                        }
                    }
    
                    ApplicationContext.Current.Services.ContentService.SaveAndPublishWithStatus(cloneDocument, cloneDocument.CreatorId, false); 
    new Umbraco.Web.UI.Pages.ClientTools(new Umbraco.Web.UI.Pages.UmbracoEnsuredPage())
                        .SyncTree(parentContent.Path, true)
                        .RefreshTree();
    
                    return true;
                }
            }
    
            return false;
        }
    

    The code is for copying not moving and I try to match the properties of each node because the document types are not the same.

  • Pedro Marinho 10 posts 80 karma points
    Oct 11, 2017 @ 16:44
    Pedro Marinho
    0

    Any clue?!

  • Pedro Marinho 10 posts 80 karma points
    Oct 19, 2017 @ 13:59
    Pedro Marinho
    0

    Any tips?

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Oct 19, 2017 @ 15:18
    Alex Skrypnyk
    0

    Hi Pedro

    you need to call angular method that will refresh the tree after server side changes

  • Pedro Marinho 10 posts 80 karma points
    Oct 25, 2017 @ 16:10
    Pedro Marinho
    0

    Hi Alex,

    how can I do that? Can you provide me more details please?

  • David Parr 48 posts 206 karma points
    Oct 26, 2017 @ 10:46
    David Parr
    1

    Hi Pedro

    In Angular, you need to use the navigation service and call the sync tree method.

    navigationService.syncTree({tree: 'content', path: ["-1","123d"], forceReload: true}); 
    

    Read more at http://umbraco.github.io/Belle/#/api/umbraco.services.navigationService

    Thanks

  • Pedro Marinho 10 posts 80 karma points
    Nov 08, 2017 @ 09:53
    Pedro Marinho
    0

    Hi David,

    thank you for your answer. I won't mark your reply as a solution because I wanted to refresh the content tree from the server side and I thought I could achieve this by using one of this functions:

    new Umbraco.Web.UI.Pages.ClientTools(new Umbraco.Web.UI.Pages.UmbracoEnsuredPage()).SyncTree(parentContent.Path, true).RefreshTree();
    

    or

      umbraco.library.RefreshContent();
        umbraco.BasePages.BasePage.Current.ClientTools.ClearClientTreeCache();
    

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft