I want to get the value for the Content Picker property on it. (this shouldn't be so hard!)
The node is strongly typed - returned from a Linq search:
var nodes = Umbraco.TypedContentAtRoot().DescendantsOrSelf("sPlan").Where( ... blah blah blah ...);
so nodes.First() will be of type IPublishedContent. I want to get the value of a contentPicker that has 1 item in it, but can only find examples for if this was dynamic.
So I am wondering how do I get that 2341 into some var so I can then get the node and on to the content I really want? Is there some 'easy' way or do I have to parse the XML in the string?
And here's a snapshot of that code snippet for your reference:
/// <summary>
/// Gets the ID's of the picked nodes.
/// </summary>
/// <param name="source">The node with the picker property.</param>
/// <param name="propertyAlias">The alias of the picker property.</param>
/// <param name="recursive">Recursively check ancestors (false by default)?</param>
/// <returns>
/// The picked node ID's.
/// </returns>
/// <remarks>
/// This is faster than LocalizedGetPickedNodes when you only need node ID's.
/// </remarks>
public static IEnumerable<int> LocalizedGetPickedNodeIds(this IPublishedContent source,
string propertyAlias, bool recursive = false)
{
if (recursive) {
while (source != null) {
if (source.HasValue(propertyAlias, false)) {
break;
}
source = source.Parent;
}
}
if (source != null) {
string pickerValue = source.LocalizedPropertyValue<string>(propertyAlias);
if (pickerValue != null)
{
int nodeId;
// Integer, CSV, or XML?
if (int.TryParse(pickerValue, out nodeId)) {
yield return nodeId;
}
else if (CsvRegex.IsMatch(pickerValue))
{
var pickedNodes = StringUtility.SplitCsv(pickerValue);
foreach (var nodeItem in pickedNodes)
{
nodeId = int.Parse(nodeItem.Trim());
yield return nodeId;
}
}
else if (!string.IsNullOrWhiteSpace(pickerValue as string)) {
var pickedNodes = new DynamicXml(pickerValue as string);
foreach (dynamic nodeItem in pickedNodes) {
nodeId = int.Parse(nodeItem.InnerText);
yield return nodeId;
}
}
}
}
}
ok, well still more complicated than I was hoping.
I trimmed down your example and wrote a very short method that works for me in this case.
/// <summary>
/// Gets the ID's from data type content picker stored as XML
/// </summary>
/// <param name="source">The node with the picker property.</param>
/// <param name="propertyAlias">The alias of the picker property.</param>
/// <returns>
/// The picked node ID's.
/// </returns>
public static IEnumerable<int> getContentPickerIDs(IPublishedContent source, string propertyAlias) {
if (source != null) {
var pickerValue = source.GetProperty(propertyAlias).DataValue.ToString();
if (pickerValue != null) {
var pickedNodes = new DynamicXml(pickerValue);
foreach (dynamic nodeItem in pickedNodes) {
var nodeId = int.Parse(nodeItem.InnerText);
yield return nodeId;
}
}
}
}
how do I get the ID from a ContentPicker ?
I want to get the value for the Content Picker property on it. (this shouldn't be so hard!)
The node is strongly typed - returned from a Linq search:
so nodes.First() will be of type IPublishedContent. I want to get the value of a contentPicker that has 1 item in it, but can only find examples for if this was dynamic.
From this page: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/content-picker I tried the sample code:
but does not work, node gives 'Object reference not set to an instance of an object' error.
This code:
will output this:
So I am wondering how do I get that 2341 into some var so I can then get the node and on to the content I really want? Is there some 'easy' way or do I have to parse the XML in the string?
Thank you
This is how I do it (IIRC, this is because different versions of Umbraco store things differently):
https://github.com/rhythmagency/rhythm.umbraco.extensions/blob/e3cce1803863d452dce76784ad554ddb6529dce0/trunk/Rhythm.Extensions/Rhythm.Extensions/ExtensionMethods/PublishedContentExtensionMethods.cs#L213
And here's a snapshot of that code snippet for your reference:
ok, well still more complicated than I was hoping.
I trimmed down your example and wrote a very short method that works for me in this case.
Thanks for the quick response!
is working on a reply...