I am using Umbraco 6.1.6. I have built a WebApi extending UmbracoApiController. I can access it and return basic data.
I want this simple ApiController to just return a list of content items that all use the document type "MyType" - or id1184.
I cannot find an example and despite relentless googling I cannot find which libraries I should be using to carry out such a simple operation. All of the examples I can find are for partial views and hence use @Model - which I can't access from the WebApi
if you want all (unpublished / published ) content use the content Service.
// get the id of the type (by alias)
IContentTypeService contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
IContentType mytype = contentTypeService.GetContentType("mytype") ;
// get all content by type
IContentService contentService = ApplicationContext.Current.Services.ContentService;
IEnumerable<IContent> items = contentService.GetContentOfContentType(mytype.Id);
this gives you everything (published or not) but hits the database, if you just want published, you should i beleive use IPublishedContent, this blog post details how to get content from examine. http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco
bit you will care about
public ActionResult Product(string id)
{
var criteria = ExamineManager.Instance.DefaultSearchProvider
.CreateSearchCriteria("content");
var filter = criteria.NodeTypeAlias("Product").And().NodeName(id);
var result = Umbraco.TypedSearch(filter.Compile()).ToArray();
if (!result.Any())
{
throw new HttpException(404, "No product");
}
return View("Product", CreateRenderModel(result.First()));
}
in the example product is the type. so if you want them all just remove the And().NodeName(id) from the query
Thanks a lot for your response. I have tried to do what you have outlined, I can get it working using the ContentService but as you say it will hit the database hard.
The other example you sent looks great - but I am struggling because it returns a View, whereas the WebApi needs to return a typed object.
Any idea how I would return "result" through the WebApi?
Return list of content items by document type through WebAPI
Hi
I am using Umbraco 6.1.6. I have built a WebApi extending UmbracoApiController. I can access it and return basic data.
I want this simple ApiController to just return a list of content items that all use the document type "MyType" - or id1184.
I cannot find an example and despite relentless googling I cannot find which libraries I should be using to carry out such a simple operation. All of the examples I can find are for partial views and hence use @Model - which I can't access from the WebApi
Thanks
Travis
if you want all (unpublished / published ) content use the content Service.
this gives you everything (published or not) but hits the database, if you just want published, you should i beleive use IPublishedContent, this blog post details how to get content from examine. http://shazwazza.com/post/Custom-MVC-routing-in-Umbraco
bit you will care about
in the example product is the type. so if you want them all just remove the
And().NodeName(id)
from the queryHi Kevin
Thanks a lot for your response. I have tried to do what you have outlined, I can get it working using the ContentService but as you say it will hit the database hard.
The other example you sent looks great - but I am struggling because it returns a View, whereas the WebApi needs to return a typed object.
Any idea how I would return "result" through the WebApi?
Thanks
is working on a reply...