Frage

I have a use case in which users need to select a field value from a droplist of items. The problem with this is that the droplist needs to be dynamically built on each item (all with the same template) to only show items in a folder that have a field value equal to that of the current item's ID. In case you're already lost, here's an example of the structure:

- sitecore
  - content
    - Home
      - ContentItem1 (with droplist)
    - Site Data
      - SelectableItem1(ContentItem1 selected in 'itemid' field)
      - SelectableItem2(ContentItem1 selected in 'itemid' field)
      - SelectableItem3(ContentItem1 not selected in 'itemid' field)
      - SelectableItem4(ContentItem1 not selected in 'itemid' field)
  - templates
    - ContentItem1Template
      - Droplist field (source set to below query)

I want my query to assign the ContentItem1's droplist field source dynamically by getting a list of items that have ContentItem1's id as their 'itemid' field's value, but by comparing the field value to that of the ContentItem1 id. I have tried doing this by comparing the field's value to the id token, like so:

query:/sitecore/content/Site Data/*[@#itemid#=$id]

No matter what value I try for id ('$id', $id, @id, '@id', @@id, '@@id', etc.) it does not want to resolve on the item level. Is there some way to do this so that I can reuse this ContentItem1Template for all of my items that need the same functionality?

War es hilfreich?

Lösung

If you are using Sitecore 7 then you can use coded field datasources. This will allow you to use any custom logic you like to specify the items which should appear in your lists.

Create a class that implements IDataSource and the ListQuery() method that returns a list of Items as the source of your field. Then set the source of your field to your method with the code: prefix, e.g. code:MyProject.Custom.FieldDataSource,MyProject.Custom

using System;
using Sitecore.Buckets.FieldTypes;
using Sitecore.Data.Items;

namespace MyProject.Custom
{
  public class FieldDataSource : IDataSource
  {
    public Item[] ListQuery(Item item)
    {
      var root = item.Database.GetItem("/sitecore/content/my-item");
      // some other logic to filter your item set
      return root.Children.ToArray();
    }
  }
}

These articles should help you:

Andere Tipps

You may need to wrap the ID in single quotes like so:

query:/sitecore/content/#Site Data#/*[@itemid='$id']

That said, this seems like a good fit for using the Sitecore Link Database. Whenever you associate a SelectableItem to a ContentItem, Sitecore will store that relationship in the Link database (as long as you reference it using a field that supports it, such as a DropLink, DropTree, GeneralLink, etc.).

From there, you can use Globals.LinkDatabase.GetReferrers(contentItem) or contentItem.Links.GetValidLinks() to get a list of all referring items to the content item. This is where you can filter down the list by template ID to ensure that you only return SelectableItems.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top