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.....
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.