0
votes

I created a custom part using the content picker field.

public int UpdateFrom1()
    {
        ContentDefinitionManager.AlterPartDefinition("BackgroundPart",
            builder => builder.WithField("BackgroundImage",
                fieldBuilder => fieldBuilder
                    .OfType("MediaPickerField")
                    .WithDisplayName("Background Image")));

        return 2;
    }

    public int UpdateFrom2()
    {
        ContentDefinitionManager.AlterTypeDefinition("Background", cfg => cfg             
          .WithPart("BackgroundPart")
          .Creatable()
          .Indexed());
        return 3;
    }

The service code for getting the data:

public class BackgroundService : IBackgroundService
{       
    private readonly IRepository<BackgroundPartRecord> _repository;

    public BackgroundService(
        IRepository<BackgroundPartRecord> repository,
        ISignals signals)
    {
        _repository = repository;           
    }

    public IEnumerable<BackgroundPartRecord> Get()
    {
        return _repository.Table;
    }       
}

This works (i can pick content when I create an new item of this type).

Now I want to get a list of all items of my type. I created a service for that and I get a list of my created items. But the items in the list don't have the media picker field. How do I get this content? I want to use this in OnResultExecuting method in a FilterProvider class in my module.

1
You didn't show the query code. Is it querying for BackGroundPart?Bertrand Le Roy
I updated my question with the code to get the background.Willem de Jong

1 Answers

1
votes

That can't work because you're using the repository API. Repository is a low-level API that is used internally, but should rarely, if ever be used by modules. One of the reasons is that it won't get content items, just part records.

Instead, you need to use one of the querying APIs from ContentManager. That will give you real content items that you can do As on, that will give you access to the content item's fields (those are stored on the Infoset, which is on the content item record), etc.

This or one of the overloads and extension methods should do the trick:

_contentManager.Query<BackgroundPart>()