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.
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: http://www.nibble.be/?p=86
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.";
}
[Setting(
"XPath",
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
.UmbracoContext
.Content
.GetByXPath(XPath.ParsePlaceHolders())
.Select(item => new PreValue
{
Id = Udi.Create(UmbConstants.UdiEntityType.Document, item.Key),
Value = item.Name,
SortOrder = item.SortOrder
})
.ToList();
}
public override List<Exception> ValidateSettings() => new List<Exception>();
}
}
Then for your XPath query, you could do something like this...
//*[@isDoc and contains(propertyAlias, 'propertyValue')]
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.
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.
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.
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: http://www.nibble.be/?p=86Interestingly, 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...
Then for your XPath query, you could do something like this...
(That's quite an inefficient XPath, eek)
I hope this helps?
Cheers,
- Lee
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.
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.
is working on a reply...