I have Blog Repository and it contains blog posts list. I am accessing all Blog Posts by using this code
var contentType = ApplicationContext.Services.ContentTypeService.GetContentType("BlogPost");
var blogPostList = ApplicationContext.Services.ContentService.GetContentOfContentType(contentType.Id);
Now I am accessing custom data type properties by using
foreach (var blog in blogPostList)
{
foreach ( var property in blog.Properties)
{
}
}
Now I can access properties and get its value but for few properties I get json String and that is no use for me as I would need to create models to properly parse json string into proper json.
Is there any way to use GetPropertyValue in this situation or some other way to get properly formatted Json.
Stop right there!!! If this is for the front end DO NOT use the ContentService. This is specifically for the back end of the site, and is VERY database intensive.
For all front end code, you should be using IPublishedContent. This queries the content cache, and is MUCH faster, as there is no database access.
If you have an UmbracoHelper (which you will in Umbraco controllers), you have access to it out of the box, otherwise you need to spin up a helper. Here's an example:
var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var blogPosts = umbracoHelper. TypedContentAtXPath("//BlogPost [@isDoc]");
You will then have a list of pages, and can use the standard .GetPropertyValue methods to get the values out of the fields. Note, the XPath query here isn't super efficient, you could make it a lot more specific if you wanted to.