Copied to clipboard

Flag this post as spam?

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


  • jonok 297 posts 658 karma points
    Mar 20, 2013 @ 04:22
    jonok
    1

    ContentService.Move not moving a node

    Before using V6 I had some C# code (in the Document.AfterSave event handler)  to move a node to a parent node according to the date month. This is the code that I used:

    Document documentObject = sender;
    documentObject.Move(MonthDocument.Id);

    Now with V6 I'm trying to do the same thing:

    Document documentObject = sender;
    Umbraco.Core.Models.IContent ct = ApplicationContext.Current.Services.ContentService.GetById(documentObject.Id);
    ApplicationContext.Current.Services.ContentService.Move(ct, MonthDocument.Id);

    But this doesn't seem to move the node. Am I missing something?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 20, 2013 @ 12:50
    Jeroen Breuer
    0

    Perhaps there is a .Save() method you need to call before it's done? Otherwise you can report an issue here: http://issues.umbraco.org/issues#newissue=yes

    Jeroen

  • Andreas Iseli 150 posts 427 karma points
    Mar 20, 2013 @ 16:37
    Andreas Iseli
    100

    Hmm I'm doing the exactly same thing but that works fine for me. Apart your code I've attached to the new event handlers of the ContentService. Thats the only difference... there is no need to call the .Save() method.

    Is it perhaps only an UI problem and you need to reload the nodes? There is a new client script for this purpose:

    BasePage.Current.ClientTools.MoveNode(ct.Id.ToString(CultureInfo.CurrentCulture), monthFolder.Path);
  • jonok 297 posts 658 karma points
    Mar 20, 2013 @ 23:34
    jonok
    0

    Thanks for the replies, I've got it working now, I think the problem may have been that the event handler was using legacy code:

    Document.AfterSave += new Document.SaveEventHandler(Document_Save);

    So I've now completely updated all code to use the new API, eg:

    ContentService.Saved += SaveEventHandler;

    Now I'm creating a month folder node, and I'm having trouble setting the SortOrder. Here's the code:

    monthNode = ApplicationContext.Current.Services.ContentService.CreateContent(ItemMonth, yearNode, "DateFolder");
    monthNode.SortOrder = 0 - int.Parse(monthNode.Name);
    ApplicationContext.Current.Services.ContentService.SaveAndPublish(monthNode);
    library.UpdateDocumentCache(monthNode.Id);

    ...but the SortOrder isn't being set, it just defaults to the index of the node - but I want to order it according to it's negative month number (eg. August = -8). Do I need to add any other code to set the SortOrder?

     

     

  • Andreas Iseli 150 posts 427 karma points
    Mar 21, 2013 @ 14:17
    Andreas Iseli
    1

    I think the problem is that you only set the sort numberof your current month node which has no relation to the sort numbers of any other month nodes. I'm using the following snippet to re-order all month nodes after a new one was created:

    // Reorder.
    IEnumerable orderedContent = contentService.GetChildren(monthFolder.ParentId).OrderByDescending(f => f.Name);
    int sortOrder = 0;
    foreach (IContent content in orderedContent)
    {
    content.SortOrder = sortOrder;
    contentService.SaveAndPublish(content);
    sortOrder++;
    }

    That works fine :) 

    Just for your information: my month nodes all starts with 01 - january, 02 february and so on.

  • jonok 297 posts 658 karma points
    Mar 21, 2013 @ 23:22
    jonok
    0

    Thanks for that, I was originally trying to order them in reverse direction, but I don't think you can use negative sort numbers anymore? Anyway it's probably better having them order ascending (the .OrderByDescending(f => f.Name) method doesn't exist by the looks of it?), and your code works perfectly so thanks for your help.

Please Sign in or register to post replies

Write your reply to:

Draft