How To Get An IPublishedContent from a Uid In Umbraco

When working with document types and properties within Umbraco 99% of the time you will be dealing with IPublishedContent and ids.  If you use the Model Builder and you work with properties like a content picker, multi-node tree picker, nest content, related link media, or third-party packages like Vorto, you may sometime bump into Umbraco Uid.  Umbraco ID's that look similar to this:


In this tutorial, you will learn about the different API's and techniques you can use with Umbraco in order to get the IPublishedContent

Using Udi 

If you want to convert the Umbraco UID in code, you can use the Udi class, like so:

            var id = "umb://media/ghjfdgj5a5704c2d8117bae2c5447eab";
            Udi udi;
            if (Udi.TryParse(id, out udi))
                return udi.ToPublishedContent();

You could also use a slightly more dangerous approach (it's riskier as if the UDI is in the wrong format, it will throw an exception):

    var udi = "umb://media/ghjfdgj5a5704c2d8117bae2c5447eab";

Using UmbracoHelper

You could also pass the UDI into some of the more traditional Umbraco helpers, like UmbracoHelper passing in a string:

     var udi = "umb://media/ghjfdgj5a5704c2d8117bae2c5447eab";
     var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
     var content = umbracoHelper.TypedContent(udi);

Or with a UDI class like:

     Udi udi = Udi.Parse("umb://media/ghjfdgj5a5704c2d8117bae2c5447eab");
     var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
     var content = umbracoHelper.TypedContent(udi);

As you can see, there are a number of ways to get an IPublishedContent from aUid, enjoy!


submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top