From 46b12132bb9116ea2716a527e5ad295415323dad Mon Sep 17 00:00:00 2001 From: Esha Noronha Date: Mon, 3 Feb 2025 12:48:55 +0100 Subject: [PATCH] Updated article --- .../reference/querying/udi-identifiers.md | 81 +++++++++++++++---- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/15/umbraco-cms/reference/querying/udi-identifiers.md b/15/umbraco-cms/reference/querying/udi-identifiers.md index 458ffe48a91..16f17b82de4 100644 --- a/15/umbraco-cms/reference/querying/udi-identifiers.md +++ b/15/umbraco-cms/reference/querying/udi-identifiers.md @@ -1,33 +1,80 @@ # UDI Identifiers -## Introduction +Umbraco uses Unique Document Identifiers (UDIs) to reference most object types, such as content, media, and members. A UDI contains all the metadata needed to retrieve an Umbraco object and is readable within text. -Umbraco stores identifiers in UDI format for most Umbraco object types. This identifier stores all of the metadata required to retrieve an Umbraco object and is parse-able within text. Example: `umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2`. UDI's can be used in many of the querying APIs. +Example: -{% hint style="info" %} -UDI is currently not an acronym for something. There is no official definition of what it's short for. Therefore it's called *UDI* -{% endhint %} +```text +umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2. +``` -## Format +UDIs are commonly used in Umbraco’s querying and management APIs. -An Umbraco UDI consists of three parts: the scheme, the type and a GUID Identifier. For example: `umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2`. +## Format -Breaking it down: +A UDI consists of three parts: -1. The scheme is `umb://` - this is always the same and makes it identifiable as an Umbraco UDI -2. The type is `document` - so in this is an Umbraco node, but it could also be `media`, `member`, etc. -3. The GUID Id is `4fed18d8c5e34d5e88cfff3a5b457bf2` - this is a GUID (dashes removed) which is randomly generated when the item is being created +1. Scheme: `umb://` – Identifies as an Umbraco UDI. +2. Type: `document`– Specifies the object type (for example, media, member, Data Type, and so on). +3. GUID Identifier: `4fed18d8c5e34d5e88cfff3a5b457bf2` – A unique identifier for the object (a GUID without dashes). ## Usage -You can use UDIs in some of the Querying and Management/Service APIs. +UDIs are useful for retrieving content, media, or other Umbraco objects through the API. Below are examples of how to use a UDI in C# to get content or media. + +### Retrieving Content by UDI + +You can retrieve a content item using `IContentService`: + +```cs +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Models + +@inject IContentService contentService + +@{ + // Define the UDI string here + var udiString = "umb://document/334cadfa62dd49049aad86b6e4c02aac"; // Example UDI string + + if (udiString.StartsWith("umb://document/")) + { + // Extract the GUID from the UDI string + var guidString = udiString.Replace("umb://document/", ""); + if (Guid.TryParse(guidString, out var guid)) + { + // Retrieve the content by GUID + var content = contentService.GetById(guid); + if (content != null) + { + // Access the body text field + var bodyText = content.GetValue("bodyText"); // Replace 'bodyText' with the alias of your body text field +

@bodyText

// Output the body text + } + else + { +

Content not found.

+ } + } + else + { +

Invalid GUID in the UDI string.

+ } + } +} +``` + +## UDI Types + +There are two types of UDIs in Umbraco: + +### GUID UDI -There are 2 types of UDIs: +Used for objects that have a GUID identifier, such as content and media. -## GUID UDI +* [API Reference: GuidUdi](https://apidocs.umbraco.com/v15/csharp/api/Umbraco.Cms.Core.GuidUdi.html) -* [API Reference](https://apidocs.umbraco.com/v15/csharp/api/Umbraco.Cms.Core.GuidUdi.html) +### String UDI -## String UDI +Used for objects that are not GUID-based, such as dictionary items. -* [API Reference](https://apidocs.umbraco.com/v15/csharp/api/Umbraco.Cms.Core.StringUdi.html) +* [API Reference: StringUdi](https://apidocs.umbraco.com/v15/csharp/api/Umbraco.Cms.Core.StringUdi.html)