Last active
November 11, 2017 17:17
-
-
Save pedroadaodev/63744210d455d45deb27f16283e03138 to your computer and use it in GitHub Desktop.
Umbraco Content Guid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for document | |
var udi = new GuidUdi("document", myCurrentNode.GetGuid()); | |
// for media | |
var image = UmbracoHelperContext.TypedMedia(1100); | |
var udi = new GuidUdi("media", image.GetGuid()); | |
// using content service to update images field | |
product.SetValue("images", udi.ToString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Models.PublishedContent; | |
using Umbraco.Web; | |
public static Guid GetGuid(this IPublishedContent content) | |
{ | |
var contentWithKey = content as IPublishedContentWithKey; | |
if (contentWithKey != null) | |
return contentWithKey.Key; | |
if (content is PublishedContentWrapped) | |
{ | |
contentWithKey = ((PublishedContentWrapped)content).Unwrap() as IPublishedContentWithKey; | |
if (contentWithKey != null) | |
return contentWithKey.Key; | |
} | |
return Guid.Empty; | |
} | |
/// <summary> | |
/// Gets the published content with a guaranteed key | |
/// </summary> | |
/// <param name="content">The published content</param> | |
/// <returns>A published content with key</returns> | |
public static IPublishedContentWithKey GetContentWithKey(this IPublishedContent content) | |
{ | |
var withKey = content as IPublishedContentWithKey; | |
if (withKey != null) | |
return withKey; | |
var wrapped = content as PublishedContentWrapped; | |
if (wrapped != null) | |
return GetContentWithKey(wrapped.Unwrap()); | |
return null; | |
} | |
/// <summary> | |
/// Guaranteed to return a GUID | |
/// </summary> | |
/// <param name="content">The published content</param> | |
/// <returns>A Guid</returns> | |
public static Guid GetKeyGuaranteed(this IPublishedContent content) | |
{ | |
var contentWithKey = content.GetContentWithKey(); | |
return contentWithKey != null ? contentWithKey.GetKey() : Guid.Empty; | |
} | |
/// <summary> | |
/// Gets an Umbraco UDI | |
/// </summary> | |
/// <param name="content">The published content</param> | |
/// <param name="objectType">The type of UDI to create</param> | |
/// <returns>An Umbraco Document Identifier</returns> | |
public static Udi GetUdi(this IPublishedContent content, UmbracoObjectTypes objectType) | |
{ | |
string type = Constants.UdiEntityType.FromUmbracoObjectType(objectType); | |
return Udi.Create(type, content.GetKeyGuaranteed()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment