Copied to clipboard

Flag this post as spam?

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


  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 05:44
    Manish
    0

    Not able to get Url of the page

    I have created a class and using this class to fetch node information. I am able to get node name and node it but not able to get url or nice url of the node.

    var node = new Node(item.Id);
    pageName = item.Name;
    item.Id url = node.Url;
    

    Fetching node url giving me exception. enter image description here

    Please suggest me how can i get the url

    Thanks

  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 06:13
    Manish
    0

    Also tried this

    var aa = umbraco.library.NiceUrl(Id);

    But giving me null exception.

  • Ali Z 31 posts 173 karma points
    Nov 08, 2017 @ 07:17
  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 07:20
    Manish
    0

    I published node many times

  • Ali Z 31 posts 173 karma points
    Nov 08, 2017 @ 07:24
    Ali Z
    0

    An other clue could be the answer validated on this post: https://our.umbraco.org/forum/developers/api-questions/28787-NiceUrl-returns-a-instead-of-the-correct-URL

    Maybe your UmbracoContext.Current is null ?

  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 08:11
    Manish
    0

    Yes Ali Z UmbracoContext.Current is null How can i resolve this, any clue?

  • Ali Z 31 posts 173 karma points
    Nov 08, 2017 @ 08:36
    Ali Z
    0

    The solution proposed doesn't work ?

    if (UmbracoContext.Current == null)
        UmbracoContext.Current = new UmbracoContext(HttpContext.Current);
    
  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 08:49
    Manish
    0

    Giving me this error enter image description here

  • Kristoffer 10 posts 80 karma points c-trib
    Nov 08, 2017 @ 07:33
    Kristoffer
    0

    Context would help in understanding where it all goes wrong for you.

    Could you try debugging the foreach loop, with these two lines on top:

    var pageName = item.Name;
    var pageUrl = item.Url;
    

    Before you init a new node. Check their Values.

  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 08:05
    Manish
    0

    Hi Kristoffer

    I have addedscreen shot above while debug,

  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 08:17
    Manish
    0

    And this code works fine but does not provide url of the page

     IContent content = `ApplicationContext.Current.Services.ContentService.GetById(item.Id);`
    
  • Kristoffer 10 posts 80 karma points c-trib
    Nov 08, 2017 @ 08:19
    Kristoffer
    0

    You are using NodeFactory, and I think that might be tripping you up. Try using an Enumerable of PublishedContent instead, and see if you have access to the Url property on those. This is how you would

    var root = Umbraco.TypedContentAtRoot().First();
    var children = root.Children;
    
    foreach (var child in children)
    {
        var pageName = child.Name;
        var pageUrl = child.Url;
    }
    
  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 08:22
    Manish
    0

    I am trying this but

    enter image description here

    why it is showing namespace error

  • Kristoffer 10 posts 80 karma points c-trib
    Nov 08, 2017 @ 08:27
    Kristoffer
    0

    Try initilaizing a umbracohelper if you dont have access to one.

    var helper = new UmbracoHelper();
    var root = helper.TypedContentAtRoot();
    
  • Manish 373 posts 932 karma points
    Nov 08, 2017 @ 08:33
    Manish
    0

    Hi Kistoffer

    Umbraco.TypedContentAtRoot() is not recognized. Am I missing something added namespace umbraco too.

    enter image description here

    Thanks

  • Kevin Jump 2341 posts 14868 karma points MVP 8x c-trib
    Nov 08, 2017 @ 09:21
    Kevin Jump
    1

    Hi Manish

    It depends a bit on where you are calling your class from, but i would be tempted to make this an extension method of the UmbracoHelper class and then you will have all the references you need to make these calls. so if you make it a static class and change the function to look like this:

    public static GetPendingItemList(this UmbracoHelper umbraco)
    {
       ....
       var rootNodes =  umbraco.TypedContentAtRoot();
    }
    

    you can get the root but again if this is called from a template or partial for a page, adding an Extension to IPublishedContent might be better as you can then reliably get the root node for a page (you site might have multiple roots, so you can't reliy on .First() on the TypedContentAtRoot() call.

    public static GetPendingItemList(this IPublishedContent item)
    {
    
       var root = content.Site();
    }
    

    this will get you the root node of a site.

Please Sign in or register to post replies

Write your reply to:

Draft