From 8eb0d254da84811b26df92f582ac7c4082c4b701 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Sun, 21 May 2017 13:35:29 +0200 Subject: [PATCH] U4-9940 Make it easy to get the integer Id of an item from it's Udi --- src/Umbraco.Web/Extensions/UdiExtensions.cs | 24 ++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Web/Extensions/UdiExtensions.cs b/src/Umbraco.Web/Extensions/UdiExtensions.cs index c814d63b8c..9b07359ee8 100644 --- a/src/Umbraco.Web/Extensions/UdiExtensions.cs +++ b/src/Umbraco.Web/Extensions/UdiExtensions.cs @@ -36,8 +36,30 @@ namespace Umbraco.Web.Extensions return umbracoHelper.TypedMember(memberAttempt.Result); break; } - + return null; } + + /// + /// An extension method to easily acquire the integer Id for a given udi + /// + /// + /// An identifier if the item is found, -1 otherwise + public static int ToIntId(this Udi udi) + { + Udi identifier; + if (Udi.TryParse(udi.ToString(), out identifier) == false) + return -1; + + var guidUdi = GuidUdi.Parse(udi.ToString()); + var umbracoType = Constants.UdiEntityType.ToUmbracoObjectType(identifier.EntityType); + + var entityService = ApplicationContext.Current.Services.EntityService; + var entity = entityService.GetByKey(guidUdi.Guid, umbracoType); + if (entity == null) + return -1; + + return entity.Id; + } } }