Get all content items of a specific document type in Umbraco 13

254 Views Asked by At

I am creating an API Controller for fetching content items in Umbraco. We have several document types already created and have a controller class for each document type. Each controller should have a method for getting all content items for that document type and another method for getting a single content item based on its Umbraco node id.

My current solution involves accessing the Umbraco context for the document type and then using the content service for getting the content items. However I am unsure which method(s) is best for the job. GetPagedOfType works in that it fetches the correct content items, but preferably i would like to simply return all, without needing to specify page index and page size. What is the correct way to go about this?

    using (UmbracoContextReference context = _contextFactory.EnsureUmbracoContext())
    {
    using (_scopeProvider.CreateScope())
    {
        var aktivitetContentType =
            context.UmbracoContext.Content.GetContentType("aktivitet")
            ?? throw new Exception("Umbraco content type is null.");

        var aktiviteter = _contentService.GetPagedOfType(
            aktivitetContentType.Id,
            0,
            100,
            out var totalAktivitetRecords,
            null
        );

        var aktivitetMapper = new AktivitetMapper(_contentService);

        var aktiviteterDto = aktiviteter.Select(aktivitetMapper.Map).ToList();

        return aktiviteterDto;
    }
}
1

There are 1 best solutions below

1
Sarath S On

You can do that by using GetContentOfContentType method it does not need page index/page size just replace this part

        var aktiviteter = _contentService.GetPagedOfType(
        aktivitetContentType.Id,
        0,
        100,
        out var totalAktivitetRecords,
        null
    );

with

  var aktiviteter = _contentService.GetContentOfContentType(aktivitetContentType.Id);