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.
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.
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.
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 :
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
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
is working on a reply...