问题
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.....
回答1:
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");
回答2:
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..
来源:https://stackoverflow.com/questions/47429028/how-to-get-the-property-value-of-the-page-which-is-requested-by-user-in-episerve