Copied to clipboard

Flag this post as spam?

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


  • Kasper 14 posts 49 karma points
    Aug 10, 2017 @ 14:47
    Kasper
    0

    Umbraco 7.6 - Move Node programmatically c#

    hi all

    I am trying to move a node to another folder, is checkbox is checked or unchecked. But getting a server error, I can't figure out.

    Code:

    public UmbracoEventHandlers()
        {
    
            ContentService.Saving += ContentService_Saving;
    
            ContentService.Saved += ContentService_Saved;
    
            ContentService.Publishing += ContentService_Publishing;
    
    
        }
    
        void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)       {
    
            foreach (IContent item in e.SavedEntities)
            {
                if (item.HasProperty("messageArchived"))
                {
                    if (item.Properties["messageArchived"].IsDirty())
                    {
                        var isArchived = Convert.ToBoolean(item.Properties["messageArchived"].Value);
                        var contentService = ApplicationContext.Current.Services.ContentService;
    
                        if (isArchived)
                        {
    
                            var archiveContent = item.Parent();
                            IContent archiveMessageFolderContent = null;
    
                            foreach (IContent child in archiveContent.Children())
                            {
                                if (child.ContentType.Alias == "messageArchive")
                                {
                                    archiveMessageFolderContent = child;
                                    break;
                                }
                            }
    
                            contentService.Move(item, archiveMessageFolderContent.Id);
                            library.RefreshContent();
                        }
                        else
                        {
                            var archiveContent = item.Parent().Parent();
                            contentService.Move(item, archiveContent.Id);
                        }
    
                    }
                }
            }
        }
        void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
        {
    
        }
        void ContentService_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
    
        }
    

    Error:

    Received an error from the server
    

    An error occurred

    Object reference not set to an instance of an object.

    Exception Details:

    System.NullReferenceException: Object reference not set to an instance of an object. Stacktrace:

    at websiteumbracoext.UmbracoEventHandlers.ContentServiceSaving(IContentService sender, SaveEventArgs1 e) in classes\UmbracoEventHandlers.cs:line 59 at Umbraco.Core.Events.ScopeEventDispatcherBase.DispatchCancelable[TSender,TArgs](TypedEventHandler2 eventHandler, TSender sender, TArgs args, String eventName) at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Core.Services.ContentService.PerformMove(IContent content, Int32 parentId, Int32 userId, ICollection1 moveInfo) at Umbraco.Core.Services.ContentService.Move(IContent content, Int32 parentId, Int32 userId) at website_umbracoext.UmbracoEventHandlers.ContentService_Saving(IContentService sender, SaveEventArgs1 e) in classes\UmbracoEventHandlers.cs:line 59 at Umbraco.Core.Events.ScopeEventDispatcherBase.DispatchCancelable[TSender,TArgs](TypedEventHandler2 eventHandler, TSender sender, TArgs args, String eventName) at Umbraco.Core.Services.ContentService.SaveAndPublishDo(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Core.Services.ContentService.Umbraco.Core.Services.IContentServiceOperations.SaveAndPublish(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Core.Services.ContentService.SaveAndPublishWithStatus(IContent content, Int32 userId, Boolean raiseEvents) at Umbraco.Web.Editors.ContentController.PostSave(ContentItemSave contentItem) at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.

  • Carlos Casalicchio 176 posts 736 karma points
    Apr 12, 2019 @ 23:17
    Carlos Casalicchio
    0

    Crazy, I'm also getting the same error, and there's no response! :(

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Apr 12, 2019 @ 23:25
    Søren Gregersen
    0

    no response?

    The error above may be that the archiveMessageFolderContent is null?

    @Carlos do you have any code to share? OP is from 2017....

  • Carlos Casalicchio 176 posts 736 karma points
    Apr 13, 2019 @ 00:18
    Carlos Casalicchio
    0

    I got it to work using the following code

            var typeService = ApplicationContext.Current.Services.ContentTypeService;
            var contentService = ApplicationContext.Current.Services.ContentService;
    
            ContentType home = (ContentType)typeService.GetContentType("home");
            IContent homePage = contentService.GetContentOfContentType(home.Id).FirstOrDefault();
    
            if (homePage != null)
            {
                var content = typeService.GetContentType("child");
                IContent childPage = contentService.GetContentOfContentType(content.Id).FirstOrDefault();
    
                if (childPage != null)
                {
                    contentService.Move(childPage, homePage.Id);
                }
            }
    

    Just to give some context to the code above, I'm creating a code-first approach to Umbraco (i know that has been frowned upon by the community, but it's working for me) and I wanted to move pages after installing and creating the content nodes . This makes it easier for me to deploy multiple sites using the same definitions, without having to duplicate work.

    And, in this case, I only have one content node of each document type, so no need for further checks to verify if I got the right content node, and I get the content Type first, to make sure I don't get the wrong page.

Please Sign in or register to post replies

Write your reply to:

Draft