Developer forum

Forum » Templates » Razor - Get array of pages

Razor - Get array of pages

Denys Ubizskyy
Reply

Hi.

 

I want to implement a list of recently published news on my site. To do this I need an array (or dictionary) with key as url and value as creation date.

Can anybody suggest some workaround.
 

Thank you in advance.


Replies

 
Nicolai Høeg Pedersen
Reply

Something like this:

For Each p As Dynamicweb.Frontend.Page In Dynamicweb.Frontend.PageView.Current.PageCollection

Dim createdDate = p.Value("PageCreatedDate")

Dim link = String.Format("Default.aspx?ID={0}", p.ID)

Next

You should always use a link in the format Default.aspx?ID=xx and let Dynamicweb handle the rewrite.

On a page you can write &debug=true in the URL and you will get a list of possible keys to pass to the p.Value("") method.

 
Denys Ubizskyy
Reply

Nicolai, thanks for quick answer. I've tried to use your code but faced an error:

'Dynamicweb.Frontend.PageView.Current()' is a 'method', which is not valid in the given context

Maybe I missed some reference?

 
Mikkel Ricky
Reply
This post has been marked as an answer

In Razor, i.e. in C#, you have to write

Dynamicweb.Frontend.PageView.Current()

with parenthesis after Current, that is (PageView.Current is a "property with parameters", see http://stackoverflow.com/questions/8597517/property-with-parameter). You can also just write Pageview (note lowercase v).

Furthermore, in stead of writing p.Value(…) you have to write p.get_Value(…) or alternatively use p.Values[…] with an lowercased key, e.g. @p.Values["pagecreateddate"].

Below is a full, yet incomplete, example of how you can do it:

@{
var sortedPages = new SortedDictionary< DateTime, Dynamicweb.Frontend.Page >();

foreach (Dynamicweb.Frontend.Page p in Pageview.Current().PageCollection.Values) {
	var createdDate = (DateTime)p.get_Value("PageCreatedDate");
	var url = String.Format("Default.aspx?ID={0}", p.ID);
	sortedPages[createdDate] = p;
}
}

@foreach (var p in sortedPages.Values) {
	<a href="/Default.aspx?Id=@p.ID">@p.Values["pagecreateddate"]: @p.Values["pagemenutext"]</a>
}

Best regards,
Mikkel

Votes for this answer: 1
 
Denys Ubizskyy
Reply

Thanks Mikkel, it works!

One adjustment. I think you did it on purpose wink

Line #4 should be:

    foreach (Dynamicweb.Frontend.Page p in PageView.Current().PageCollection.Values){

PageView in CamelCase

 
Mikkel Ricky
Reply

I was actually trying to write Pageview.PageCollection.Values, but I failed …

 
Denys Ubizskyy
Reply

One more question. I need to select pages from a specific folder and not from the whole site. Can you tell me how to do it?

 
Mikkel Ricky
Reply

You can add a check

var parentPageID = 87;
…
if ((int)p.Values["pageparentpageid"] == parentPageId) {
  sortedPages[createdDate] = p;
}

A thought just crossed my mind: If your pages are items based, then you can use an item publisher to render to newest pages. It's much easier than getting the pages like we've done here.

You can render an item publisher in Razor like this

@RenderItemList(new {
// See http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/Module-tags/Item-publisher/Item-Render.aspx
  ItemType = "News",
  ListSourceType = "Page",
  ListSourcePage = 87,
  ItemFieldsList = "*",
  ListPageSize = 10,
  ListOrderBy = "Created",
  ListTemplate = "ItemPublisher/List/LatestNews.html"
})

 

 
Denys Ubizskyy
Reply

Thanks a lot! Mikkel!

This is exactly what I was looking for.

But with the item publisher I get an error: "The name 'RenderItemList' does not exist in the current context"

I think it's because of the version of DynamicWeb. I use Version 8.3.1.18

 
Mikkel Ricky
Reply

Yes, prior to Dynamicweb 8.4 you have to use the syntax shown on http://templates.dynamicweb-cms.com/TemplateTags/Dynamicweb-template-tags/Module-tags/Item-publisher/Item-Render.aspx, i.e.

<!--@Item.RenderList(ItemType:News;ListSourceType:Page;…)-->

 

You must be logged in to post in the forum