-2

How to get the property value of the page which is requested by user in episerver cms 10...

public string GetContent(string pageType, string propertyName)
{
    Type type = Type.GetType(pageType); //target type
    object o = Activator.CreateInstance(type);
    var pageLink = new ContentReference();
    var contentLoader= ServiceLocator.Current.GetInstance<IContentLoader>();
    var content = contentLoader.Get<type>(pageLink);
    var vals = content.GetPropertyValue(propertyName);
    return vals;
}

In the above method i have get the page name and property name from the url .... so in this i have convert the variable pageType ( i.e. page name ) to class and use it in Get<> method..but it is not working...some body please tell me the solution... or else is there any other way to find property vakue of the user requested property in requeted page.....

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
Mohan Raj
  • 25
  • 1
  • 6
  • 1
    What is in the pageLink variable? – Eric Herlitz Nov 22 '17 at 08:46
  • Could you elaborate on what the method is supposed to do, i.e. what problem are you trying to solve? – Ted Nyberg Nov 22 '17 at 11:05
  • in episerver alloy site i have write a method called "Getcontent" in which the property value of the user requested property associated with that page – Mohan Raj Nov 23 '17 at 05:53
  • pageLink holds the page reference of the page.. the proper syntax is pageLink = new PageReference(//some page id) or pageLink = new ContentReference(//some page id) – Mohan Raj Nov 23 '17 at 05:54
  • @TedNyberg....could you tell me the corrections to get output..Or tell me some other solution for getting result of that case.. – Mohan Raj Nov 23 '17 at 05:56
  • Possible duplicate of [Getting particular property value from particular page in episerver](https://stackoverflow.com/questions/47472070/getting-particular-property-value-from-particular-page-in-episerver) – Henrik Fransas Dec 15 '17 at 14:55

2 Answers2

4

I think you're misunderstanding some core concepts.

You should do something like the following:

// Get object used to load Episerver content
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

// Some content reference
var contentLink = new ContentReference(123);

// Get content of a specific type
var somePage = contentLoader.Get<SomePageType>(contentLink);

// Strongly typed access to content property
var somePropertyValue = somePage.SomeProperty;

If you really have to get a value by its property name:

var someOtherProperty = somePage.GetPropertyValue("SomeOtherPropertyName");

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72
  • Thaks Ted....but this code gets the page by content reference...so if i need startpage so i use ContentReference(24)....it get the " ArticlePage" in alloy site rather than choosing "StartPage"....I need the correct page to get corresponding property value – Mohan Raj Nov 23 '17 at 12:04
  • 1
    If you are getting the startpage then you can use the ContentReference.StartPage variable. But else if your startpage is id 24 then getting it like Ted showed should give you the startpage. – Asthiss Nov 23 '17 at 14:37
  • @Asthiss.....no by using that method i can't get the startpage ....instead i got Article page...because 24 is a "PageTypeID" of start page and ContentReference(24) and get the page is give you the page with PageLink id 24......so i am unable to get some other page. – Mohan Raj Nov 24 '17 at 03:27
  • Get the page using base type PageData instead, and use the indexer to get the property by name. No need for strong typing if you're not getting the property strongly typed. – Ted Nyberg Nov 24 '17 at 06:13
-4

The Answer for this question is :

public string GetContent(string pageName, string propertyName)
    {
        string content = string.Empty;
        try
        {
            log.Info("GetContent Method is called for getting property value!!!!!");
            IContentTypeRepository contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
            IEnumerable<ContentType> allPageTypes = contentTypeRepository.List().OfType<PageType>();
            IContentModelUsage contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
            IList<ContentUsage> pageInstanceCollection = new List<ContentUsage>();
            foreach (ContentType item in allPageTypes)
            {
                IList<ContentUsage> pageInstance = contentModelUsage.ListContentOfContentType(item);
                foreach (ContentUsage i in pageInstance)
                {
                    pageInstanceCollection.Add(i);
                }
            }
            IEnumerable<ContentUsage> currentpage = pageInstanceCollection.Where(x => x.Name.ToLower() == pageName.ToLower());
            int Id = currentpage.First().ContentLink.ID;
            PageReference pagereference = new PageReference(Id);
            IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            PageData pageData = contentRepository.Get<PageData>(pagereference);
            content = pageData.GetPropertyValue(propertyName);

        }
        catch(Exception exception)
        {
            string errorMessage = string.Format("Error in Content Retrieval : {0}", exception.Message);
            log.Error(errorMessage, exception);
        }
        return content;
    }

Here I am passing CMS page name and property name to the method to get the corresponding property value..

Mohan Raj
  • 25
  • 1
  • 6
  • 1
    Future readers: This is extremely slow, and is certainly not the intended way to retrieve content in Episerver. Please refer to Ted Nyberg's answer for the correct method of content retrieval. – Andreas Christiansen Nov 29 '17 at 14:33
  • Hi Andreas, If you know the page id then you can proceed with Ted solution..But you don't know the page id,only you know page name think in that scenario ......for that my solution is good.....but i too agree with Ted.... – Mohan Raj Nov 30 '17 at 03:16
  • 1
    Hi Mohan, I realize my critique was a little harsh. I did not intend to offend your solution. If you don't have the page ID, I suppose this works, but keep in mind, that 1) editors can change page names and 2) page names are not unique. In addition, you should consider using IContentLoader instead of IContentRepository if you are not modifying the page, as it is faster. – Andreas Christiansen Nov 30 '17 at 08:06
  • Thanks Andreas. I will Change it to "IContentLoader"...And thanks for your feedback to notify my errors correctly....and Thanks for Ted also... – Mohan Raj Nov 30 '17 at 09:07