1

I need to get the property value for user given property of user given page in episerver... for that i write a method..

 public string GetContent(string pageName, string propertyName)
    {
        var contentTypeRepo = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
        IEnumerable<ContentType> allPageTypes = contentTypeRepo.List();
        var currentpage = allPageTypes.Where(x => x.Name.ToLower() == pageName);
        var pageId = currentpage.First().ID;
        var pageRef = new PageReference(pageId);
        var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
        var page = contentRepository.Get<PageData>(pageRef);
        var content = page.GetPropertyValue(propertyName);
        return content;
    }

But I can not get the correct page by pageType ID...it is get some other page ....so this is what my requirement... user given page name and property name and the get method will return corresponding property value... Thanks.....

Mohan Raj
  • 25
  • 1
  • 6
  • Possible duplicate of [How to get the property value of the page which is requested by user in episerver cms](https://stackoverflow.com/questions/47429028/how-to-get-the-property-value-of-the-page-which-is-requested-by-user-in-episerve) – Ted Nyberg Nov 24 '17 at 11:15
  • thats fine @TedNyberg..but still i didn't get any write solution so that i raised this question – Mohan Raj Nov 24 '17 at 11:26
  • could you please read it once more and tell me the appropriate answer @TedNyberg – Mohan Raj Nov 24 '17 at 11:26

2 Answers2

1

This question has also been asked on Episerver World and I also write my answer here. _pageCriteriaQueryService is injected through constructor injection in the class.

Even though this will get you the property value from a page by its pagename and propertyname, it is not recommended to code like this.

First I would have gone back to find out why this demand exist, where and how are you going to use your function?

public string GetPropertyValueByPageNameAndPropertyName(string pageName, string propertyName)
    {
        var criteria = new PropertyCriteriaCollection
        {
            new PropertyCriteria()
            {
                Name = "PageName",
                Type = PropertyDataType.String,
                Condition = CompareCondition.Equal,
                Value = pageName
            }
        };

        var pages = _pageCriteriaQueryService.FindPagesWithCriteria(ContentReference.StartPage, criteria);

        if (pages != null && pages.Count > 0)
        {
            return pages[0].GetPropertyValue(propertyName);
        }

        return string.Empty;
    }
Henrik Fransas
  • 1,067
  • 1
  • 10
  • 16
0

That's because your getting a page with the ID of a page type. It is just a coincidence that there is a page with the same ID as the page type you resolve.

You don't need to resolve the page type in your method, though. Instead, pass a ContentReference object as an argument to your method to specify which page to get.

Refactored version of your method:

public static object GetContentProperty(ContentReference contentLink, string propertyName)
{
   var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

   var content = contentLoader.Get<IContent>(contentLink);

   return content.GetPropertyValue(propertyName);
}

Also, you should use IContentLoader for getting content, unless you also need to modify/save content.

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72