Copied to clipboard

Flag this post as spam?

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


  • Parvez 1 post 71 karma points
    May 17, 2018 @ 04:37
    Parvez
    0

    How to set custom paging in Umbraco (server site Paging)

    I have 100000 blog data and I want to set paging in Umbraco. But every time Umbraco fetch all 100000 than use LINQ query to filter out base on page setting. (how much you want to display on the page)

    Anyone know how to set custom paging in Umbraco.

    Get some of the data instead of getting all 100000 data in single time.

  • Dave Woestenborghs 2847 posts 8817 karma points MVP 3x admin c-trib
    May 17, 2018 @ 07:15
    Dave Woestenborghs
    0

    Hi Parvez,

    100000 is a lot of blog items. In this the API using LINQ is not the best option performance wise. But there are other options like XpathNavigator or Examine.

    Using a XpathNavigator you can do the following :

    var pageSize = 10;
    var currentPage = 2;
    var itemsToSkip = (currentPage -1) * pageSize;
    
    // very greedy xpath, can be made more specific for better performance
    var xpath = "//BlogDocTypeAlias[@isDoc]";
    
    var navigator = UmbracoContext.ContentCache.GetXPathNavigator();
    var expression = navigator.Compile(xpath);
    expression.AddSort("blogDateProp", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Text);
    
    var iterator = navigator.Select(expression).Cast<XPathNavigator>().Skip(itemsToSkip).Take(pageSize).ToList();
    
    var blogs = new List<IPublisedContent>();
    
    foreach(var item in iterator) 
    {
        var contentId = int.Parse(item.GetAttribute("id",""));
    
        blogs.Add(UmbracoContext.ContentCache.GetById(id));
    }
    

    This works directly on the Umbraco XML cache. It will look up all blog items, sort them by date property, and take just the ones you need without loading them all in to memory like with linq

    Another option is to use Examine

    var pageSize = 10;
    var currentPage = 2;
    var itemsToSkip = (currentPage -1) * pageSize;
    var blogs = new List<IPublisedContent>();
    
    var searchCriteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria(IndexTypes.Content);
    
    var query = searchCriteria.Field("nodeTypeAlias","BlogDocTypeAlias");
    
    var results = ExamineManager.Instance.DefaultSearchProvider.Search(query.Compile())
                    .OrderByDescending(x => x.Fields["blogDate"]).Skip(itemsToSkip).Take(pageSize);
    
    foreach(var item in results) 
    {
        blogs.Add(UmbracoContext.ContentCache.GetById(item.Id));
    }
    

    Both options will be a lot faster than a Linq query.

    More info can be found here :

    https://www.slideshare.net/dawoe/the-need-for-speed-uk-fest

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft