Copied to clipboard

Flag this post as spam?

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


  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 12:17
    Enrico
    2

    From UDI to ID

    Hi everyone,

    is it possible get the document id or document id from the new property udi? What is the relation between id and udi?

    IpublishedContent does not contain the property 'key' (GUID) that is present in /App_Data/umbraco.config. Is there a way for getting it?

    Thanks in advance

    Enrico

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 12:51
    Alex Skrypnyk
    3

    Hi Enrico,

    The easiest way is to use UmbracoHelper like that:

    var id = Umbraco.GetIdForUdi(udi);
    

    Thanks,

    Alex

  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 13:16
    Enrico
    1

    Thanks Alex, for your help.

    I was trying to do also the other way around. Any ideas?

    my code was

    public class MenuController : UmbracoApiController
        {
    
            [HttpGet]
            public HttpResponseMessage GetNavigation()
            {
                var linkItems = new List<LinkItem>();
                var homepage = Umbraco.TypedContentAtRoot().First();
    
    
                var udi = homepage.GetKey();
    
                linkItems.Add(new LinkItem {Id = homepage.Id, Name = homepage.Name, ParentId = 0, Url = homepage.Url, Udi = udi.ToString()  });
    
                foreach (var item in homepage.Children.Where("Visible"))
                {
                    linkItems.AddRange(GetChildLinks(item));
                }
    
                var response = Request.CreateResponse(HttpStatusCode.OK, linkItems, new AngularJsonMediaTypeFormatter());
                response.Headers.Add("Access-Control-Allow-Origin", "*");
                return response;
            }
    }
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 13:17
    Alex Skrypnyk
    1

    What other ways for example?

  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 13:21
    Enrico
    0

    Hi Alex,

    thanks for getting back to me.

    As you can see from the code, in my API controller I just want to return an object containing the the Udi

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 15:26
    Alex Skrypnyk
    0

    Enrico, where in this code do you need udi to id?

  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 15:42
    Enrico
    0

    Hi Alex,

    I need to create a list of all the pages in the websites containing at least the Guid property called 'key'. It would be great to have the UDI.

     var linkItems = new List<LinkItem>();
    

    Then I select the homepage

    var homepage = Umbraco.TypedContentAtRoot().First();
    var key = homepage.GetKey();
    

    Then I add the data of the homepage to the list

    linkItems.Add(new LinkItem {Id = homepage.Id, Name = homepage.Name, ParentId = 0, Url = homepage.Url, Key = key.ToString()  });
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 15:49
    Alex Skrypnyk
    0

    Is it empty?

    var key = homepage.GetKey();
    
  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 15:51
    Enrico
    0

    this is always empty.

    I tried also to implement IPublishedContentWithKey, but the key is always empty.

    I checked also the file Umbraco.config in the folder App_Data and the key are ok for every content.

  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 15:54
    Enrico
    0

    This all the code from my controller

    public class MenuController : UmbracoApiController
        {
    
            [HttpGet]
            public HttpResponseMessage GetNavigation()
            {
                var linkItems = new List<LinkItem>();
                var homepage = Umbraco.ContentAtRoot().First();
    
    
                linkItems.Add(new LinkItem {Id = homepage.Id, Name = homepage.Name, ParentId = 0, Url = homepage.Url});
    
                foreach (var item in homepage.Children.Where("Visible"))
                {
                    linkItems.AddRange(GetChildLinks(item));
                }
    
                var response = Request.CreateResponse(HttpStatusCode.OK, linkItems, new AngularJsonMediaTypeFormatter());
                response.Headers.Add("Access-Control-Allow-Origin", "*");
                return response;
            }
    
    
            [HttpGet]
            public HttpResponseMessage GetNavigationTree()
            {
                var menuItems = new List<MenuItem>();
                var homepage = Umbraco.ContentAtRoot().First();
    
                // menuItems.Add(new MenuItem { Id = homepage.Id, Name = homepage.Name, ParentId = 0, Url = homepage.Url });
    
                foreach (var item in homepage.Children.Where("Visible"))
                {
                    menuItems.AddRange(GetTreeLinks(item));
                }
    
                var response = Request.CreateResponse(HttpStatusCode.OK, menuItems, new AngularJsonMediaTypeFormatter());
                response.Headers.Add("Access-Control-Allow-Origin", "*");
                return response;
            }
    
            private List<LinkItem> GetChildLinks(dynamic item)
            {
                var newItems = new List<LinkItem>();
    
                newItems.Add(new LinkItem {
                            Id = item.Id,
                            Name = item.Name,
                            ParentId = item.ParentId,
                            Url = item.Url
    
                });
    
    
                foreach (var sub in item.Children.Where("Visible"))
                {
                    newItems.AddRange(GetChildLinks(sub));
                }
    
                return newItems;
            }
    
            private List<MenuItem> GetTreeLinks(dynamic item)
            {
                var newItems = new List<MenuItem>();
                var tempItems = new List<MenuItem>();
    
                foreach (var sub in item.Children.Where("Visible"))
                {
                    tempItems.AddRange(GetTreeLinks(sub));
                }
    
                newItems.Add(new MenuItem
                {
                    name = item.Name,
                    link = tempItems.Any() ? "#" : item.id.ToString(),
                    url = item.Url,
                    subtree = tempItems.Any() ? tempItems : null
    
                });
    
                return newItems;
            }
    
    
    
        }
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 15:56
    Alex Skrypnyk
    0

    Enrico, did you try this method - https://github.com/Shazwazza/Articulate/blob/master/src/Articulate/Models/PublishedContentExtensions.cs#L52?

    public static IPublishedContentWithKey GetContentWithKey(IPublishedContent model)
            {
                var withKey = model as IPublishedContentWithKey;
                if (withKey != null) return withKey;
    
                var wrapped = model as PublishedContentWrapped;
                if (wrapped != null)
                {
                    return GetContentWithKey(wrapped.Unwrap());
                }
                return null;
            }
    
            public static Guid GetContentKey(this IMasterModel model)
            {
                var withKey = GetContentWithKey(model);
                if (withKey != null) return withKey.Key;
                return Guid.Empty;
            }
    
  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 16:04
    Enrico
    0

    I tried also this approach.

    I also tried to restore the obselete editors adding the key

    <showDeprecatedPropertyEditors>true</showDeprecatedPropertyEditors>
    

    in the file 'umbracoSettings.config' under the folder 'Config'

    Now it's two days that I stuck. I think I will give up and downgrade Umbraco to a previous version where the old editors were in use.

    The reason why i wanted to to do this is because I am working with the Umbraco Rest API, that only accepts ids to retrieve the content and does not accept routes or udi.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 16:11
    Alex Skrypnyk
    1

    Enrico, try to disable PropertyValueConverters.

    The Property value converters are controlled by an umbracoSetting.config setting: in section settings/content, setting EnablePropertyValueConverters set to false.

    And you will be able to use old school ids in your properties.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 16:16
    Alex Skrypnyk
    1

    will not help you

  • Enrico 47 posts 252 karma points
    Jul 12, 2017 @ 16:28
    Enrico
    0

    Thanks for your kind help.

    But also this setting doesn't work.

    I will try tomorrow with an older version of Umbraco. Hopefully the code I already wrote, will work.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 12, 2017 @ 16:34
    Alex Skrypnyk
    1

    what is the problem if you disable PropertyValueConverters?

  • Enrico 47 posts 252 karma points
    Jul 13, 2017 @ 07:47
    Enrico
    0

    Hi Alex,

    thanks for getting back to me.

    I changed the key to false in the umbracoSettings.config, cleared the plugin cache and all the other cache folders, stopped evey instance of iis express in memory and restarted umbraco, but the content picker never changed.

    I wonder why the mapped queries in the CMS and IPublishedContent do not store to the property key. The key is present both in the database and in the file umbraco.config under App_Data/Temp and it's used to generate the udi.

    I had a look at the class UDI in the Umbraco Core and it does not seem to have a method to reverse the Udi

  • Enrico 47 posts 252 karma points
    Jul 13, 2017 @ 08:44
    Enrico
    103

    To get the key the only way was to use this code:

    var node = ApplicationContext.Services.ContentService.GetRootContent().First();
    var key = node.GetUdi().Guid;
    

    Now I can manage the Udi property

    private GuidUdi getUdi(int id)
     {
       var node = ApplicationContext.Services.ContentService.GetById(id);
                return node?.GetUdi();
     }
    

    Thanks Alex for your help

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 13, 2017 @ 08:49
    Alex Skrypnyk
    2

    Hi Enrico

    You are welcome, glad that you found a solution. ApplicationContext.Services.ContentService.GetRootContent().First() will call database, but it works.

    Thanks,

    Alex

  • Enrico 47 posts 252 karma points
    Jul 13, 2017 @ 09:43
    Enrico
    0

    Hi Alex,

    is Examine the best way to query the cahe?

  • Martina R 9 posts 90 karma points
    Mar 25, 2019 @ 18:19
    Martina R
    0

    Sorry for the necropost but I stumbled upon this page while searching for a way to get the node udi from the node id.

    In the end I solved the problem with this code (which should not hit the database), maybe it can help someone else:

            IPublishedContent node = Umbraco.TypedContent(nodeId);
            Guid key = node.GetKey();
            var udi = Udi.Create("document", key);
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jul 13, 2017 @ 10:04
    Alex Skrypnyk
    1

    Hi Enrico

    Yes, Examine is the fastest way to query data from Examine index, it's not the same as cache little bit. But it's the fastest way to query data.

    Did you read this article: https://our.umbraco.org/documentation/reference/Common-Pitfalls/

    Thanks,

    Alex

  • Enrico 47 posts 252 karma points
    Jul 19, 2017 @ 08:29
    Enrico
    0

    Here is a JavaScript function that I wrote to match and replace udi. The input is the html string to parse and the sitelinks is a list of the Umbraco pages.

        function filterByUdi() {
        return function(input, siteLinks) {
    
            if (input) {
    
                var regex2 = /href=\"\/{localLink:umb:\/\/document([^\"]*)"/g;
                var links = input.match(regex2);
    
                if (links && siteLinks) {
    
                    var output = input.replace(/data-udi=\"([^\"]*)\"/g, "");
    
                    for (var j = 0; j < siteLinks.length; j++) {
                        for (var k = 0; k < links.length; k++) {
                            if (links[k].includes(siteLinks[j].NodeUdi)) {
                                if (siteLinks[j].Url === "/") {
                                    output = output.replace(links[k], 'href=\"#!home\"');
                                } else {
                                output = output.replace(links[k], 'href=\"#!' + siteLinks[j].Url + '\"');
                                }
                            }
                        }
                    }
    
                    return output;
                }
            }
    
            return input;
        };
    }
    

    I hope it could be helpful

  • Sumesh KP 35 posts 108 karma points c-trib
    Apr 03, 2019 @ 04:39
    Sumesh KP
    1

    Thanks @Enrico for the solution.

    After the Umbraco 8 Code clean up the accessing methods got changed compared to Umbraco 7

    So, I have updated the code for Get UID from ID in Umbraco 8 as follows

        using Umbraco.Core;
    
        private GuidUdi GetUdi(int id)
        {
            var node = Services.ContentService.GetById(id);
            if (node != null)
            {
                return node.GetUdi();
            }
            return null;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft