Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Travis 7 posts 58 karma points
    Dec 02, 2013 @ 12:32
    Travis
    0

    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

  • Kevin Jump 2311 posts 14697 karma points MVP 7x c-trib
    Dec 02, 2013 @ 15:01
    Kevin Jump
    0

    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

  • Travis 7 posts 58 karma points
    Dec 02, 2013 @ 16:18
    Travis
    0

    Hi 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

Please Sign in or register to post replies

Write your reply to:

Draft