Getting particular property value from particular page in episerver

瘦欲@ 提交于 2019-12-23 02:57:28

问题


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


回答1:


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.




回答2:


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;
    }


来源:https://stackoverflow.com/questions/47472070/getting-particular-property-value-from-particular-page-in-episerver

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!