Copied to clipboard

Flag this post as spam?

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


  • zib95b 14 posts 93 karma points
    Sep 04, 2017 @ 15:47
    zib95b
    0

    Creating JS list of Umbraco pages

    Say I wanted to create an accordion styled bookmarks list in a sidebar beside main content. When you visit a page, you have the ability to bookmark it.

    How can I reference current Umbraco page in a JS file to be able to bookmark it? There's a specific reason why I need to do it this way, and not use browser bookmarks.

    I did consider resolving the current page URL for < A > tag and page title for text, and appending it to the list that way, but surely the bookmark will remain in the list, if the page gets deleted in back office?

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Sep 06, 2017 @ 08:21
    Kevin Jump
    0

    Hi

    i think there are two problems you will need to solve. hopefully the first one is easy.

    on the link that you would click to put the page into your javascript bookmarks you can add the page id to the list. (you have probibly done this bit ?

    <a href="#somelink" data-pageid="@Model.Content.Id">bookmarkme</a>
    

    for the second bit, i think you need to write a ApiController that will return the page information as JSON to your pages.

    if you look at the Api Controller documentation- you can see how this works but briefly (and this code isn't really tested in anyway)

    if you have an API controller like...

    public class MyPageApiController : UmbracoApiController
    {
        [HttpPost]
        public IEnumerable<PageInfo> GetPages(IEnumerable<int> ids)
        {
    
            var pages = Umbraco.TypedContent(ids);
    
            return pages.Select(x => new PageInfo()
            {
                title = x.GetPropertyValue<string>("title", x.Name),
                Url = x.Url
            });
    
        }
    
        public class PageInfo
        {
            public string title { get; set; }
            public string Url { get; set; }
        }
    
    }
    

    then using jquery (or you prefered javascript thing) you could get your page info.

    ids = [array, of, page ids];
    
    $.post( 'umbraco/api/myPageApi/myPages', ids)
        .done(function(data ) {
           // data will have a list of your page ids in it.
        });
    

    this way you will know when pages are deleted because while they might still be in your javascript list, they won't comeback from the API call, so you could remove them at that point.

  • Mladen B. 12 posts 35 karma points
    Sep 17, 2017 @ 03:02
    Mladen B.
    0

    Just to add something to this solution. I'm not sure if you are aware of the security implications of such an approach, but if you are just blindly serving the content, based on the node ids provided from the client side (browser), without checking if the client has an access to those pages, you're effectively circumventing any kind of security that might be set in place for those pages. It would be more secure if you could always first check if the user/client has an access to that node, before you serve it back.

Please Sign in or register to post replies

Write your reply to:

Draft