如何获取用户在episerver cms中请求的页面的属性值

时间:2017-11-22 07:29:05

标签: c# episerver

如何获取用户在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;
}

在上面的方法中,我从网址获取页面名称和属性名称.... 所以在这里我已经将变量pageType(即页面名称)转换为类并在Get&lt;&gt;中使用它方法..但它不工作......有些人请告诉我解决方案...... 或者是否有任何其他方法可以在requeted page .....中找到用户请求的属性的属性vakue。

2 个答案:

答案 0 :(得分:4)

我认为你误解了一些核心概念。

您应该执行以下操作:

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

如果你真的必须通过其属性名称获取值:

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

答案 1 :(得分:-4)

这个问题的答案是:

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

这里我将CMS页面名称和属性名称传递给方法以获取相应的属性值..