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 01, 2013 @ 22:33
    Travis
    0

    WebApi return JSON from Examine search

    Umbraco version 6.1.6. I am new to WebApi and Examine and I've spent all day trying to return JSON using WebApi to a clientside jquery ajax call. Despite loads of googling and searhing this forum I can't quite find what I need.

    I want to find all instances of a pages created of a particular type "PageType". I then want to return this 'List' of pages back as JSON to the client.

    I've cobbled together the following using various snippets found on Google but I am clearly missing something fundamental.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Web.WebApi;
    using System.Collections;
    using Umbraco.Web;
    using Examine;
    using Newtonsoft.Json;
     
        public class ThisIsMyApiController : UmbracoApiController
        {
            public IEnumerable GetPages()
            {
                UmbracoHelper help = new UmbracoHelper(UmbracoContext);
    var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
    var filter = criteria.NodeTypeAlias("PageType");
    var result = Examine.ExamineManager.Instance.Search(filter.Compile()).ToList();
    return result; }
        }

     

    If I then go to http://localhost/umbraco/api/ThisIsMyApi/GetPages

    Fiddler returns this:

    HTTP/1.1 500 Internal Server Error
    Cache-Control: private
    Content-Type: application/xml; charset=utf-8
    Date: Sun, 01 Dec 2013 21:15:34 GMT
    Content-Length: 3374
    
    <Error><Message>An error has occurred.</Message><ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage>

     

    If I change the return line to serialize the object myself using Newtonsoft.json then I get a bit further:

    return JsonConvert.SerializeObject(result);

    then it does complete. But it doesn't return JSON, it returns an XML document with a separate node for every single character in the document as follows:

    <anyType xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/" i:type="d2p1:char">91</anyType>

    As well as getting the above working, I'm also keen to know is it is the best way of achieving what I need to do?

    Thanks in advance

  • sami 22 posts 104 karma points
    Jan 20, 2014 @ 17:08
    sami
    0
       public List<SearchResult> GetPages()
            {
                UmbracoHelper help = new UmbracoHelper(UmbracoContext);
                var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
                var filter = criteria.NodeTypeAlias("PageType");
                var result = Examine.ExamineManager.Instance.Search(filter.Compile()).ToList();
                return result;
            }
        }
    
  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 20, 2014 @ 17:27
    Ismail Mayat
    0

    Travis,

    When you hit the web api directly with browser you get xml but when you use jquery and the like you do get back json. I think your issue at the moment is that the search is not returning anything. In your code when you return result before the return can you do criteria.ToString() and write that out or look at it using debugger and get a copy of the generated examine query. Then try this using examine inspector or luke and see if you get any results.

    Regards

    Ismail

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Feb 10, 2014 @ 11:48
    Ismail Mayat
    1

    Travis,

    Just an update on this. If you implement IApplicationEventHandler and in OnApplicationStarting do

            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    
        }
    

    This will ensure json is returned by default. Just had to do this and it works a treat.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Feb 10, 2014 @ 14:29
    Andy Butland
    2

    I quite like this option too.  Doesn't completely remove the XML response should you want it (and request text/xml), but makes the browser default JSON.

    Andy

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Feb 10, 2014 @ 16:17
    Ismail Mayat
    0

    Andy,

    Yup saw that but xml is dead json is the future lol joking of course!!!

Please Sign in or register to post replies

Write your reply to:

Draft