Get a typed object from the uComponents Url Picker
Posted by trikks on October 12, 2012
This snippet is based on Umbraco 4.9 and uComponents 5 but should work almost any verison.
uComponents for Umbraco is possibly one of the best extensions out there for any CMS and the Url Picker is one of my favorites. The data returned to the
As the documentation states (UrlPicker documentation) you get the data as xml or json.
<url-picker mode="Content"> <new-window>False</new-window> <node-id>1061</node-id> <url>/homeorawaytest4.aspx</url> <link-title>Home Or Away Quiz</link-title> </url-picker>
Snippet
I created an object that will help those of who uses c# or vb.net. Use it like this
Node CurrentNode = Node.GetCurrent(); var buyOnlineProperty = CurrentNode.GetProperty("buyOnline"); var buyOnline = new Trikks.Umbraco.UrlPicker(buyOnlineProperty);
And include this object
using System; using System.Xml; using umbraco.interfaces; namespace Trikks.Umbraco { public class UrlPicker { public string Url { get; set; } public int NodeId { get; set; } public string LinkTitle { get; set; } public bool NewWindow { get; set; } public UrlPicker() { } public UrlPicker(object property) { string propertyValue = string.Empty; // If IProperty if (property is IProperty) { propertyValue = ((IProperty) property).Value; } // If string if (property is string) { propertyValue = property.ToString(); } if (!string.IsNullOrEmpty(propertyValue)) { // Get url from xml XmlDocument doc = new XmlDocument(); doc.LoadXml(propertyValue); var url = doc.SelectSingleNode("//url"); if (url != null) Url = url.InnerText; var newWindow = doc.SelectSingleNode("//new-window"); if (newWindow != null) NewWindow = Convert.ToBoolean(newWindow.InnerText); var nodeId = doc.SelectSingleNode("//node-id"); if (nodeId != null && !string.IsNullOrEmpty(nodeId.InnerText)) NodeId = Convert.ToInt32(nodeId.InnerText); var linkTitle = doc.SelectSingleNode("//link-title"); if (linkTitle != null) LinkTitle = linkTitle.InnerText; } } } }




diachedelic said
You can also use “uComponents.Core.DataTypes.UrlPicker.Dto.UrlPickerState.Deserialize()” which is how UrlPicker does it under the hood (you get a statically typed object).