Search code examples
umbracoumbraco7

Get property value instead of just Id


I have this line i an Umbraco solution, which gets me the ids of the nodes selected, comma seperated.

But what i want to get, in a oneliner, is a value of the node(s) selected. So currently it is just returning the node id like "1001, 1000, 1003" or just "1003" if only one is selected.

What i want it to return, is a value of the selected node. Lets say its a menu title on the property i want. So instead of the above output, i want "Frontpage, Contact, About Us" etc.

How would that look?

var mynode = Page.Cache.GetById(id);
mynode.GetPropertyString("pages")

Solution

  • The simplest way would be to install the Umbraco Core Property Value Converters nuget package. This would then provide you with an extension method that you can use to retrieve a list of nodes in one go. Then you could use LINQ to select the menuTitle property of each page.

    var menuTitles = mynode.GetPropertyValue<IEnumerable<IPublishedContent>>("pages")
        .Select(x => x.GetPropertyValue<string>("menuTitle"));
    

    If you need to you can then use string.Join() to combine the menu titles to get a comma-separated list.

    Here's a list of using statements that you might need to add:

    using System.Collections.Generic;
    using System.Linq;
    using Our.Umbraco.PropertyConverters;
    using Umbraco.Web;
    using Umbraco.Core.Models;