November Happy Hour will be moved to Thursday December 5th.

Pagereference doesnt work?

Vote:
 

hi!

Im having trouble when using the following code:

if (IsValue("RssSource"))
            {

if (IsValue("RssSource"))            

{

 PageReference listingContainer = (PageReference)DataFactory.Instance.CurrentPage["RssSource"]; 

PageDataCollection children= DataFactory.Instance.GetChildren(listingContainer, LanguageSelector.AutoDetect(true));

...

}

the value of listingcontainer is null dispite ive set an internal pageUrl in the edit settings. so when im getting the children it says:

 System.NullReferenceException: Object reference not set to an instance of an object. and points to listingcontainer. now im no einstein but basicly it says that the pageref=null. But the page got the property...

ive looked all over the place to see whats wrong or what the pageref is depended on but without luck finding any answers.

Someone who can point me in the right direction?

#45499
Nov 09, 2010 15:03
Vote:
 

Hi

I think the problem with your code is DataFactory.Instance.CurrentPage. You should never get the current page form the data factory singelton. In fact, I was surprised the property even existed, but it seems like it needs to be there to fulfil the IPageSource interface.

If this code is a template I guess that you inherite from the PageBase class. If so, you should have access to a CurrentPage property directly on your class, which is what you should use.

Regards

Per Gunsarfs
EPiServer Development Team

#45503
Nov 09, 2010 15:55
Vote:
 

Thnx for your reply!

I did use the CurrentPage property direclty at the beginning but i got the same problem. I decided to try to change it to datafactory instead but with the same result.

 

i accessed the page property earlier by :

PageReference listingContainer = (PageReference)CurrentPage["RssSource"]

same with getchildren 

It does inherit from pageBase as it should. Perhaps i missinterpreted what you meant by accessing the currentpage property directly on my class?

After a discussion with a friend that mentioned it should state Datafactory instead i decided to try that instead. But i get the same error with both of them. This is strange because we use pageref on more pages than this one and it works like a charm. 

#45506
Edited, Nov 09, 2010 16:16
Vote:
 

You seem to have understood me perfectly, PageReference listingContainer = (PageReference)CurrentPage["RssSource"] is the correct way to write the code. So if listingContainer is null that means that there is no value set for "RssSource" on the page.

#45507
Nov 09, 2010 16:28
Vote:
 

Think you need to access it like CurrentPage.Property["RssSource"]

#45516
Nov 09, 2010 22:59
Vote:
 

What property type is your RssSource property? The property value can only be directly cast to PageReference if it is a property of type Page. If you are using Url to page/external address, the property value will be a string. You can however parse that string to a page reference.

PageReference link = PageReference.Parse(CurrentPage["RssSource"] as string);

That won't work for non-local pages of course, so check if you got a pagereference out by using PageReference.IsNullOrEmpty(link);

 

#45519
Nov 10, 2010 7:27
Vote:
 

First of all thnx for the replies!

1. I know that there is a value it just seems like the value cant bee read or it really doesnt save the value to the property thus telling me its null.

2. The CurrentPage.property[index] is index based and i need to rewrite the function. No problem but it should work as it is now?

3. I think the right answer lies in what magnus Paulsson is describing about Url to page/external address property since its what im using. So i will test what is suggested and report back ASAP...

Thnx again for all input!

#45520
Nov 10, 2010 8:31
Vote:
 

The CurrentPage.Property["Prop"] indexer returns the PropertyData object for the property value. CurrentPage["Prop"] returns the value of the property, that is to say CurrentPage["Prop"] will return the same as CurrentPage.Property["Prop"].Value (but watch out, Property["Prop"] could be null here).

#45521
Nov 10, 2010 8:39
Vote:
 

Aha ok, thats correct. My mistake, was in a rush.

Tried the example above and it worked, however now i get :

code:

PageReference link = PageReference.Parse(CurrentPage["RssSource"] as string);

error:

Invalid PageReference format: /FirstFolder/SecondFolder/ThirdFolder/Page.aspx?id=24904 <--Its the internal link (not the real btw in order to not compromise security).

Possible solution?

Im guessing i have to parse the string into a complete url or something or is it sufficient with the string above?

#45523
Nov 10, 2010 9:14
Vote:
 

Ah, my mistake! You should use ParseUrl, not Parse.

#45585
Nov 10, 2010 9:54
Vote:
 

Whoho nice. Works perfectly.

Thnx all for helping a Epi newbie out...

#45586
Nov 10, 2010 10:10
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.