Copied to clipboard

Flag this post as spam?

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


  • Galina 44 posts 258 karma points
    Oct 21, 2014 @ 07:56
    Galina
    0

    Get IPublishedContent by part of Url from service

    Hello.

    I have a part of URL , like 'book/audiobook/nestedaudioboook/teststructure/' (book - is not root, it's the secont level under the root). I need to find node id to get IPublishedContent. I'he written the method

    public static IContent GetChildContentByName(IEnumerable<IContent> rootContentNodes, string param)
            {
                if (string.IsNullOrEmpty(param)) return null;
                string[] names = param.Split('/').Where(p=> !string.IsNullOrEmpty(p)).ToArray();
                if (names == null || names.Length == 0) return null;
                var key = param.Replace('/', '-');
                HttpContext context = HttpContext.Current;
                if (context.Cache[key] == null)
                {
                    try
                    {
                        IContent content =GetContent(names, rootContentNodes.SelectMany(cnt => cnt.Children()).ToList(), 0);
                        var expTime = DateTime.Now.AddMinutes(1);
                        if (content!=null && Ozon.Shop.Common.SiteSettings.Cache)
                        {
                            expTime = DateTime.Now.AddMinutes(15);
                        }
                        context.Cache.Insert(key, content, null, expTime,Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                        return content;
                    }
                    catch (Exception)
                    {
                        return null;
                    }
                }
                return (IContent) context.Cache[key];
            }
            private static IContent GetContent(string[] names,List<IContent> contentNodes, int index)
            {
                var content = contentNodes.Single(ch => ch.Name.ToLower() == names[index]);
                return (names.Length <= ++index) ? content : GetContent(names, content.Children().ToList(), index);
            }

    Is there any methods to do that?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Oct 21, 2014 @ 08:01
    Dave Woestenborghs
    100

    You can use uQuery for that. There is a method to get a node by Url.

     

    http://our.umbraco.org/documentation/Reference/Querying/uQuery/Content/Nodes

    Dave

  • Galina 44 posts 258 karma points
    Oct 21, 2014 @ 08:29
    Galina
    0

    Thanks, Dave. It's wonderful.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies