Quantcast
Channel: ActiveTopics
Viewing all articles
Browse latest Browse all 72689

WebApi return JSON from Examine search

$
0
0

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


Viewing all articles
Browse latest Browse all 72689


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>