Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • George Phillipson 108 posts 287 karma points
    Mar 23, 2019 @ 01:22
    George Phillipson
    0

    Get Id or Guid of another doctype in controller

    Hi How do you go about getting the ID or Guid of another doctype when in a controller?

    I'm currently playing around and cannot figure out how to get the data unless I hard code the value which is not really appropriate.

    I have the following

    using System;
    using System.Web.Mvc;
    using Umbraco.Web;
    using Umbraco.Web.Mvc;
    
    namespace Web.Core.Controllers
    {
        public class HomeController : RenderMvcController
        {
            private readonly IUmbracoContextFactory _contextFactory;
    
            public HomeController(IUmbracoContextFactory contextFactory)
            {
                _contextFactory = contextFactory;
            }
            public ActionResult Home()
            {
                var node            = Umbraco.AssignedContentItem;
                var page            = node.Value("bodyText");
                var contact         = node.Descendant("contact");
                var test            = contact.Value("bodyText");
                var underContact    = node.Children.DescendantsOrSelfOfType("underContact");
    
                foreach (var item in underContact)
                {
                    var test2 = item.Value("bodyText");
                }
    
                using (var cf = _contextFactory.EnsureUmbracoContext())
                {
                    var cache = cf.UmbracoContext.ContentCache;
                    var node1 = cache.GetById(Guid.Parse("f1e8b338-605f-41b2-9595-a8c8f9a63a1d"));
                    var bodyText = node1.Url;
                }
    
                    var data = Umbraco.Content(Guid.Parse("f1e8b338-605f-41b2-9595-a8c8f9a63a1d"));
    
                var test3 = data.Value("bodyText");
                return View("~/Views/Home.cshtml");
            }
    
        }
    }
    

    So if I'm in the home controller and I want to get the ID or Guid of say the contact doctype, how do I do that.

    Thanks

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Mar 23, 2019 @ 08:28
    Sebastiaan Janssen
    0

    First of all, you don't need to do this:

            using (var cf = _contextFactory.EnsureUmbracoContext())
            {
                var cache = cf.UmbracoContext.ContentCache;
                var node1 = cache.GetById(Guid.Parse("f1e8b338-605f-41b2-9595-a8c8f9a63a1d"));
                var bodyText = node1.Url;
            }
    

    You can do Umbraco.Content("f1e8b338-605f-41b2-9595-a8c8f9a63a1d"); instead.

    If you know where the Contact page exists in the node tree you can find it, for example if you know it is a child page of the home page you can do something like:

            // "contact" is the alias of the document type of your Content page
            var contactPage = home.ChildrenOfType("contact").FirstOrDefault();
            if (contactPage != null)
            {
                var url = contactPage.Url;
                var guidId = contactPage.Key;
                var bodyText = contactPage.Value<IHtmlString>("bodyText");
                // ..etc
            }
    

    If the contact page is somewhere else you'll need to play with Descendants instead of Children. This might get expensive as you'll need to look through the whole site structure.

    To make it easier, people often put a content picker on the home page so that you or the editor can point to the contact page. Then you could do something like:

            // "contentPicker" is the alias you gave the property to pick the contact page
            // I believe most content pickers store an `int` id but I'm not sure
            var contactPagedId = home.Value<int>("contentPicker");
            var contactPage = Umbraco.Content(contactPagedId);
            if (contactPage != null)
            {
                var url = contactPage.Url;
                var guidId = contactPage.Key;
                var bodyText = contactPage.Value<IHtmlString>("bodyText");
                // ..etc
            }
    

    That last option will give you the best performance.

  • George Phillipson 108 posts 287 karma points
    Mar 23, 2019 @ 13:19
    George Phillipson
    0

    Hi Sebastiaan Janssen

    Thanks for the reply, In V7 I use ContentPicker but as V8 has so many changes I was playing around to see if there was any new way.

    I'll keep using the ContentPicker

    Cheers

  • George Phillipson 108 posts 287 karma points
    Apr 21, 2019 @ 11:07
    George Phillipson
    1

    Keep meaning to reply to this, finally got round to it

    var contactPagedId = home.Value<int>("contentPicker");  should be  var
    contactPagedId = home.Value<IPublishedContent>("contentPicker").Id;
    
Please Sign in or register to post replies

Write your reply to:

Draft