retrieving a specific page content from a class

Topics: Developer Forum, User Forum
May 29, 2007 at 1:12 PM
Hi Christian,
I think N2 is very interesting,
I would like to ask you if it is possible, and how, to access a specific content in a page saved in the cms from an external class. I'd like to reuse your interfaces, in order not to deal with the db directly. If you could help me that would be great.
Thank you,

Michele

Coordinator
May 29, 2007 at 6:34 PM
Edited May 30, 2007 at 7:37 PM

sm670298 wrote:
I would like to ask you if it is possible, and how, to access a specific content in a page saved in the cms from an external class. I'd like to reuse your interfaces, in order not to deal with the db directly. If you could help me that would be great.


There are some options using the N2 API. If you want to do more advanced stuff you could look into NHibernate queries. Here's some examples using the N2 API:

If you know the id of the content item you're looking for you can get it like this:
// gets a page given a known identifier
ContentItem myItem = N2.Factory.Persister.Get(myItemId); //oops, updated error

Alternatively, given that the item is placed in the tree structure you could walk the item graph like this:
// gets page with a certain "Name" below the start page
N2.ContentItem startPage = N2.Factory.UrlParser.StartPage;
N2.ContentItem myItem = startPage.GetChild("theNameOfMyItem");
// enumerate children (this could be done recursively)
N2.ContentItem startPage = N2.Factory.UrlParser.StartPage;
foreach(N2.ContentItem item in startPage.GetChildren())
	if(item["myProperty"] == true)
		// do something

Another options would be storing a reference to your item (I would recommend "hiding" the reference using a property):
// create reference
N2.ContentItem startPage = N2.Factory.UrlParser.StartPage;
startPage["MyItem"] = myItem;
N2.Factory.Persister.Save(startPage);
// get item from reference
ContentItem startPage = N2.Factory.UrlParser.StartPage;
N2.ContentItem = (N2.ContentItem)startPage["MyItem"];

Yet another option would be searching for your content using the finder implementation.
// find the items based on a property
N2.Factory.Persister.Finder.SetExpression("MyProperty", N2.Persistence.Comparison.Equal, "MyValue").List();
May 30, 2007 at 5:47 PM
Thank you very much for your help,

I just had to correct the first snippet you gave in.
ContentItem myItem = N2.Factory.Persister.Get(myItemId);
But that was easy enough for me :-) .



Michele
Coordinator
May 30, 2007 at 7:39 PM
Oh, my bad =) I updated the post for future reference.