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.
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:
Error:
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, SaveEventArgs
1 e) in classes\UmbracoEventHandlers.cs:line 59 at Umbraco.Core.Events.ScopeEventDispatcherBase.DispatchCancelable[TSender,TArgs](TypedEventHandler
2 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, SaveEventArgs
1 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, IDictionary
2 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.Crazy, I'm also getting the same error, and there's no response! :(
no response?
The error above may be that the
archiveMessageFolderContent
is null?@Carlos do you have any code to share? OP is from 2017....
I got it to work using the following code
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.
is working on a reply...