Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • suzyb 469 posts 907 karma points
    Mar 08, 2022 @ 21:09

    Filtering Umbraco documents prevalue source

    I would like to include a dropdown with a list of umbraco documents, filtered depending on a property on the document.

    It doesn't look like this is possible with Umbraco forms by default but is it possible to create a custom prevalue source and if so, is there any documentation or blog posts outlining how to go about this.

  • Lee Kelleher 4010 posts 15743 karma points MVP 12x admin c-trib
    Mar 09, 2022 @ 12:03
    Lee Kelleher

    Hi Suzy,

    I vaguely recall there being a "Umbraco Docs from xpath" prevalue source that came with Umbraco Forms, but that could have been back in the days when it was called Contour. Not sure if it got removed or it was a package, anyway I couldn't see it today.

    The C# class to extend from is FieldPreValueSourceType. There's a blog post by Tim Geyssens in 2010 with an example on how to do this:

    Interestingly, the code example from Tim's post - aside from a few namespace changes - would still work on Umbraco Forms for v8. (I've not tried this on Forms for v9 yet).


    With your question, I'm unsure what the UI would look like to select the property alias(es) and define the values to filter the nodes on. I'm wondering if an XPath query would be better suited?


    I found a couple of old forum posts that have a code example of getting Umbraco nodes using an XPath query...

    I also dug out some code that I had for an Umbraco v8 project...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Forms.Core;
    using Umbraco.Forms.Core.Attributes;
    using Umbraco.Forms.Core.Extensions;
    using Umbraco.Forms.Core.Models;
    using Umbraco.Web;
    using UmbConstants = Umbraco.Core.Constants;
    namespace Our.Umbraco.Web
        public class UmbracoContentByXPath : FieldPreValueSourceType
            private readonly IUmbracoContextAccessor _umbracoContextAccessor;
            public UmbracoContentByXPath(IUmbracoContextAccessor umbracoContextAccessor)
                : base()
                _umbracoContextAccessor = umbracoContextAccessor;
                Id = Guid.Parse("57F03527-9758-4926-9216-FE4C4BD00791");
                Name = "Umbraco Content by XPath";
                Description = "Populates the prevalues with Umbraco content filtered by an XPath query.";
                Alias = "xpath",
                Description = "Enter the XPath query to select the Umbraco content items.",
                SupportsPlaceholders = true,
                View = "textstring")]
            public string XPath { get; set; }
            public override List<PreValue> GetPreValues(Field field, Form form)
                if (string.IsNullOrWhiteSpace(XPath) == true)
                    return new List<PreValue>();
                return _umbracoContextAccessor
                    .Select(item => new PreValue
                        Id = Udi.Create(UmbConstants.UdiEntityType.Document, item.Key),
                        Value = item.Name,
                        SortOrder = item.SortOrder
            public override List<Exception> ValidateSettings() => new List<Exception>();

    Then for your XPath query, you could do something like this...

    //*[@isDoc and contains(propertyAlias, 'propertyValue')]

    (That's quite an inefficient XPath, eek)

    I hope this helps?

    - Lee

  • suzyb 469 posts 907 karma points
    Mar 09, 2022 @ 19:25

    Thank you that has been a big help.

    I've got the xPath one working (with a slightly more efficient xPath query...I think) which should do for now.

    We're working working for a very demanding client at the moment and I feel at some point they'll complain at having to manually enter options in a dropdown after adding the related pages to the site. Even if we need to refine how this works, it's at least a start.

  • Carlos Mosqueda 239 posts 429 karma points
    Mar 09, 2022 @ 19:46
    Carlos Mosqueda

    What the end goal of the need for the client?
    Is it to use a page picker in which they choose the pages that will show up on a certain page?
    There might be another way to do this, possibly with a Multinode picker? If that is the need.

Please Sign in or register to post replies

Write your reply to: