Following is an example of a WebApi controller that serves as an OData service. The controller will respond to OData requests to return blogs stored in Orchard as BlogParts.

The ODataBlog Model contains our resulting model that is going to be serialized
using ODataBlog.Models;
using Orchard;
using Orchard.Blogs.Models;
using Orchard.ContentManagement;
using Orchard.OData;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ODataBlog.Controllers
{
    public class BlogController : ApiController
    {

        private readonly IOrchardServices _orchardServices;

        public BlogController(
                            IOrchardServices orchardServices
                            )
        {
            _orchardServices = orchardServices;
        }

// Default action to return a list of our Blogs. It uses a server paging of 5 items. 
// Notice the ExtendedQueryable that will override the default message, 
// that WebApi OData does not support the $format query field.
// example: /v1/blogs

        [ExtendedQueryable(ResultLimit = 5)]
        [ActionName("DefaultAction")]
        public IQueryable<Blog> Get()
        {
            return _orchardServices.ContentManager
                            .Query<BlogPart, BlogPartRecord>(VersionOptions.Published)
                            .List()                            
                            .Select(c => new Blog(c))
                            .AsQueryable();
        }
// Default action with Id to return information about a specific Blog.
// example: /v1/blogs/33
        [ActionName("DefaultActionById")]
        public HttpResponseMessage GetById(int id)
        {
            Blog blog = _orchardServices.ContentManager.Query<BlogPart, BlogPartRecord>(VersionOptions.Published)
                .Where(p => p.Id == id)
                .List()
                .Select(c => new Blog(c))
                .SingleOrDefault();

            if (blog == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.OK, blog);
            }
        }

// Action that returns the description of a specific Blog.
// It is very easy to implement any action, or create an action that returns the blogposts of a specific blog.
// example: /v1/blogs/33/description

        [HttpGet]
        public HttpResponseMessage Description(int id)
        {
            Blog blog = _orchardServices.ContentManager.Query<BlogPart, BlogPartRecord>(VersionOptions.Published)
                .Where(p => p.Id == id)
                .List()
                .Select(c => new Blog(c))
                .SingleOrDefault();

            if (blog == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);
            else
                return Request.CreateResponse(HttpStatusCode.OK, blog.Description);
        }

// This is the responce of a request like /v1/blogs/$metadata
// WebApi OData does not support automatic creation of metadata info to comply with OData protocol
// but it is possible to create a correct responce here.

        [HttpGet]
        public HttpResponseMessage GetMetaData()
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
    }
}

Last edited Dec 30, 2012 at 9:05 PM by fotisgpap, version 2

Comments

No comments yet.