Copied to clipboard

Flag this post as spam?

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


  • Mark Roffey 31 posts 184 karma points
    Oct 19, 2017 @ 11:27
    Mark Roffey
    0

    Getting all content with specific document type

    Is it possible using the Umbraco API (Angular) to get all content with a specific document type?

    Thanks in advance.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 19, 2017 @ 11:40
    Dan Diplo
    101

    There doesn't seem to be anything in the contentResource module to do this that I can see. But you could write your own API controller (that inherits from UmbracoAuthorizedJsonController) to return this data as JSON that you could then consume with Angular.

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Oct 19, 2017 @ 12:58
    Steve Morgan
    1

    Hi Mark,

    Here's a simple API I posted on a different thread that should get you started - I've just hacked it to do what you want, it's untested but should just about work!

    Change the "Home" doc type alias to match your root node and the "SomeDocTypeAlias" to be your doc type. If you have a lot of nodes (I mean thousands) to traverse this could be a little slow.

    using System.Collections.Generic;
    using Umbraco.Web;
    using Umbraco.Core.Models;
    using Umbraco.Web.WebApi;
    using System.Linq;
    
    namespace MyNameSpace.ApiControllers
    {
        public class TestApiController : UmbracoApiController
        {
            public string GetHelloWorld()
            {
                return "Hello World";
            }
    
            public List<SomeApiResponseModel> GetTestContentItems(SomeApiRequestModel request)
            {
                var response = new List<SomeApiResponseModel>();
                var _umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
                var homeNode = _umbracoHelper.TypedContentAtRoot().FirstOrDefault();
    
                if(homeNode != null)
                {
                    // here you might add filtering from your request
                    foreach(var contentNode in homeNode.Descendants("SomeDocTypeAlias"))
                    {
                        response.Add(new SomeApiResponseModel
                        {
                            SomeOtherProperty = contentNode.Id,
                            SomeTestProperty = contentNode.Name
                        });
                    }
                }
    
                return response;
            }
        }
    
        public class SomeApiResponseModel
        {
            public string SomeTestProperty { get; set; }
            public int SomeOtherProperty { get; set; }
        }
    
    
        public class SomeApiRequestModel
        {
            public int PageNumber { get; set; }
            public int Year { get; set; }
        }
    
    }
    
  • Mark Roffey 31 posts 184 karma points
    Oct 19, 2017 @ 15:02
    Mark Roffey
    0

    Both of you really helped with your answers, and both deserve to be marked as "solved". Alas I can only do one.

    Thank you both. You've set me on a good path.

Please Sign in or register to post replies

Write your reply to:

Draft