I'm trying to get a node ID by a property value, basically to cut a long story short - I get passed an ID by an external system
thepagename?id=654654
I have a property on each node filled with these unique values, but I need to be able to lookup the NodeID from this property value - As I need to get the node URL so I can redirect the user to the correct page (Hope that makes sense..lol)
All these nodes are a specific DocType, so was wondering if anyone new a nice and easy way of doing this?
Me thinks GetXmlNodeByXPath() which is part of the umbraco.library class should do the trick. Just need to assemble the correct xpath expression... Returns an XPathNodeIterator object
If you did want to go the XSLT extension route, then you could use the following SQL query?
SELECT DISTINCT
d.contentNodeId
FROM
cmsPropertyData AS d
INNER JOIN cmsPropertyType AS t ON d.propertytypeid = t.id
WHERE
t.Alias = 'externalPageId'
AND
d.dataNvarchar = '654654'
Where the 'externalPageId' is the property type alias.
Personally, I'd go with the XPath option... that is if you are using XSLT! ;-)
TBH I had the XSLT to do it already, but the reason I was trying to use the API and not XSLT is I need to redirect the user to the nodes URL after I get it - I didn't think that was possible with XSLT?
@Lee - Is this type of call not possible using the nodefactory?
I've been referring to this a lot lately, (but haven't used it myself) ... take a look at Hendy's Umbraco Helper Class, there is a method called "GetNodesFromXpath", which will return you a nodeFactory class.
Quick snippet:
public static List<Node> GetNodesFromXpath(string xPath)
{
List<Node> nodes = new List<Node>();
XPathNavigator xPathNavigator = umbraco.content.Instance.XmlContent.CreateNavigator(); //get all umbraco xml
XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPath); //TODO: check xpath string is valid
int id;
Node node;
while (xPathNodeIterator.MoveNext())
{
if (int.TryParse(xPathNodeIterator.Current.Evaluate("string(@id)").ToString(), out id)) //check id is a numberic
{
node = new Node(id);
if (node != null) { nodes.Add(node); }
}
}
return nodes;
}
Find Out NodeID/URL By Property Value
I'm trying to get a node ID by a property value, basically to cut a long story short - I get passed an ID by an external system
thepagename?id=654654
I have a property on each node filled with these unique values, but I need to be able to lookup the NodeID from this property value - As I need to get the node URL so I can redirect the user to the correct page (Hope that makes sense..lol)
All these nodes are a specific DocType, so was wondering if anyone new a nice and easy way of doing this?
Hi Lee
I think you should be able to use the umbraco.library in C# so I think you can get the corresponding nodes by using the "getXmlNodeById" extension.
/Jan
Hi Lee,
Me thinks GetXmlNodeByXPath() which is part of the umbraco.library class should do the trick. Just need to assemble the correct xpath expression... Returns an XPathNodeIterator object
(may need to tweak the xpath a bit...)
Hope this helps.
Regards,
/Dirk
Or, use Linq2umbraco if you're using v4.1
/Dirk
Dirk beat me to it with the XPath query! :-)
If you did want to go the XSLT extension route, then you could use the following SQL query?
Where the 'externalPageId' is the property type alias.
Personally, I'd go with the XPath option... that is if you are using XSLT! ;-)
Cheers, Lee.
Replies very appreciated chaps - Thanks :)
TBH I had the XSLT to do it already, but the reason I was trying to use the API and not XSLT is I need to redirect the user to the nodes URL after I get it - I didn't think that was possible with XSLT?
@Lee - Is this type of call not possible using the nodefactory?
@Dirk - Its 4.03 :(
Hi Lee,
I've been referring to this a lot lately, (but haven't used it myself) ... take a look at Hendy's Umbraco Helper Class, there is a method called "GetNodesFromXpath", which will return you a nodeFactory class.
Quick snippet:
Cheers, (the other) Lee.
Man that class is awesome :) Cheers dude!
Hi, just thought I'd mention that the class has been updated, as per the suggestion that GetNodesFromXpath method should parse for "$currentPage".
is working on a reply...