Copied to clipboard

Flag this post as spam?

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


  • Connie DeCinko 931 posts 1160 karma points
    Aug 04, 2016 @ 17:50
    Connie DeCinko
    0

    How to get NodeId for content by name

    Using the Umbraco 6 api, assuming I know the nodeId of the current page. How do I get the nodeId of a page under the current page, with a name of "Unanswered". I cannot get the nodeId by content type as there are multiple nodes with this type.

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 04, 2016 @ 18:03
    Nicholas Westby
    0

    I wouldn't recommend it, but you could do this:

    var unanswered = Model.Content.Children
        // Or you could use "InvariantEquals".
        .Where(x => x.Name.InvariantContains("Unanswered"))
        .Select(x => x.Id).FirstOrDefault();
    

    That is assuming direct children rather than all descendants.

    Would be good to have more context about why you are trying to do this. You might consider, for example, adding an "Unanswered" (or "Answered") boolean property to each content type and use that to indicate whether to content node is unanswered.

  • Connie DeCinko 931 posts 1160 karma points
    Aug 04, 2016 @ 18:17
    Connie DeCinko
    0

    The page is a suggestion box form. I created two folders, of type Suggestions under that page. The idea was to make managing them easier. Users fill out the form, they get added to the Unanswered folder. A staff member handles it, adds and answer, and moves it to the Answered folder. Of course this would probably be easier in Umbraco 7, but alas, we are still on 6.

    Worst case I have to hard code the nodeId of the Unanswered folder, but I would much rather get it dynamically so I don't have to change it when I move this to stage and prod.

    enter image description here

    ContentService cs = new ContentService();
    var content = cs.CreateContent(suggestionTitle, 3063, "Suggestion", userId);
    content.SetValue("title", suggestionTitle);
    content.SetValue("suggestionDescription", suggestion);
    content.SetValue("suggestedDate", DateTime.Today);
    content.SetValue("suggesterName", name);
    content.SetValue("suggesterEmail", email);
    content.SetValue("suggesterPhone", extension);
    //cs.SaveAndPublishWithStatus(content);
    cs.SendToPublication(content);
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 04, 2016 @ 18:35
    Nicholas Westby
    0

    Should be easy enough on Umbraco 6. Could you create two different content types, "Answered Suggestions" and "Unanswered Suggestions"? Then you could get the child node by content type:

    var unanswered = Model.Content.Children
        .Where(x => "UnansweredSuggestions".InvariantEquals(x.DocumentTypeAlias))
        .Select(x => x.Id).FirstOrDefault();
    

    Users don't expect things to break when they rename things. As a matter of fact, I had a site go down yesterday because a user renamed something (of course, I subsequently added some defensive coding so that doesn't happen again).

  • Connie DeCinko 931 posts 1160 karma points
    Aug 04, 2016 @ 20:50
    Connie DeCinko
    0

    Just cannot grab the correct node. I was able to get the first child node but that's not the one I want.

    Can someone tell me why this won't work?

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var contentNode = umbracoHelper.TypedContentSingleAtXPath("[@isDoc and @nodeName='Unanswered']").Id;
    

    It must be a bad XPath as I get this error: Expression must evaluate to a node-set.

  • Connie DeCinko 931 posts 1160 karma points
    Aug 04, 2016 @ 21:00
    Connie DeCinko
    0

    I got it, finally!

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var contentNode = umbracoHelper.TypedContentSingleAtXPath("//Suggestions [@isDoc and @nodeName='Unanswered']").Id;
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 04, 2016 @ 21:25
    Nicholas Westby
    0

    Why would you want to use XPath? I have a strong suspicion this will be deprecated in future versions of Umbraco.

    Also, I suspect what you have is not what you want. The suspect the leading double forward slash will find every "Unanswered" node rather than the first one under the current page. I could be wrong about that, but it's worth a test.

    This seems to have some info regarding using XPath relative to a content node ID: https://our.umbraco.org/forum/developers/razor/43855-UmbracoTypedContentSingleAtXPath-Help

Please Sign in or register to post replies

Write your reply to:

Draft