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.
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.
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.
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).
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.
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var contentNode = umbracoHelper.TypedContentSingleAtXPath("//Suggestions [@isDoc and @nodeName='Unanswered']").Id;
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.
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.
I wouldn't recommend it, but you could do this:
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.
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.
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:
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).
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?
It must be a bad XPath as I get this error: Expression must evaluate to a node-set.
I got it, finally!
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
is working on a reply...