Skip to content

Conversation

@bjarnef
Copy link
Contributor

@bjarnef bjarnef commented Nov 23, 2023

Prerequisites

  • I have added steps to test this contribution in the description below

Description

In Angular it is possible to fetch content by int, Guid and Udi id using contentResource.getById(), but often it is better fetching multiple items in a single request instead of multiple requests.

and since most pickers stores udi references, then Guid and Udi overload methods are preferred. However contentResource.getByIds() only supported int ids.

Currently the ids are from querystring, but it may be better it post ids in body like entityResource.getByIds() method does.

I also updated this for media.

I found it is bit inconsistency that ContentService GetByIds(IEnumerable<Udi> ids) extension returns nullable, while MediaService GetByIds(IEnumerable<Udi> ids) extension doesn't

public static IEnumerable<IContent>? GetByIds(this IContentService contentService, IEnumerable<Udi> ids)
{
var guids = new List<GuidUdi>();
foreach (Udi udi in ids)
{
if (udi is not GuidUdi guidUdi)
{
throw new InvalidOperationException("The UDI provided isn't of type " + typeof(GuidUdi) +
" which is required by content");
}
guids.Add(guidUdi);
}
return contentService.GetByIds(guids.Select(x => x.Guid));
}

public static IEnumerable<IMedia> GetByIds(this IMediaService mediaService, IEnumerable<Udi> ids)
{
var guids = new List<GuidUdi>();
foreach (Udi udi in ids)
{
if (udi is not GuidUdi guidUdi)
{
throw new InvalidOperationException("The UDI provided isn't of type " + typeof(GuidUdi) +
" which is required by media");
}
guids.Add(guidUdi);
}
return mediaService.GetByIds(guids.Select(x => x.Guid));
}

@github-actions
Copy link

github-actions bot commented Nov 23, 2023

Hi there @bjarnef, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@bjarnef bjarnef changed the title Allow fetching content by Guid and Udi Allow fetching content by Guid and Udi Nov 23, 2023
@bjarnef bjarnef changed the title Allow fetching content by Guid and Udi Allow fetching multiple content and media by Guid and Udi Nov 23, 2023
@bjarnef
Copy link
Contributor Author

bjarnef commented Nov 23, 2023

@Zeegaan speaking about Guid and entities 😁

Co-authored-by: Ronald Barendse <[email protected]>
@bjarnef
Copy link
Contributor Author

bjarnef commented Feb 19, 2024

@nul800sebastiaan this one shouldn't have any impact on existing behavior, but allow GetByIds to be fetched by int[] (as now) + Guid[] and Udi[] - just like GetById accepting int, Guid and Udi.

The extensions could be streamlined in v14 (if not already).

@bjarnef
Copy link
Contributor Author

bjarnef commented Sep 20, 2025

@AndyButland would it still make sense the include this in latest v13?

I recall in some custom views, where it would have been useful with a single lookup, but I needed to make a lookup per item (or create a custom API method to handle that).

Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is there's not too much demand given the time this PR has been pending, but I don't see any harm in it and can see it might allow some optimisations for packages doing these look-ups. Will merge in and schedule for 13.12 (13.11 is already built and internally tested, so I don't want to add more to that).

@AndyButland AndyButland merged commit 67f7d99 into umbraco:v13/dev Sep 20, 2025
18 of 19 checks passed
@bjarnef bjarnef deleted the feature/content-by-guid-udi branch September 20, 2025 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants