Copied to clipboard

Flag this post as spam?

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


  • Nguyen Dung Tri 106 posts 606 karma points
    Jul 26, 2016 @ 08:19
    Nguyen Dung Tri
    0

    Is there Web API "HelloWorld" plugin for Umbraco CMS?

    Hi Everyone,

    I would like to ask a tutorial to create a sample Web API plugin for Umbraco CMS. It is just like a plugin that can call Web Api controller to print out a Hello World.

    Sincerely,

    Tri

  • David Peck 690 posts 1896 karma points c-trib
    Jul 26, 2016 @ 08:35
    David Peck
    0

    Not just this?

    using System.Web.Http;
    using Umbraco.Web.WebApi;
    
    public class HelloWorldApiController : UmbracoApiController
    {
        // /Umbraco/Api/HelloWorldApi/Print
        [HttpGet]
        public string Print()
        {
            return "Hello World";
        }
    }
    
  • David Peck 690 posts 1896 karma points c-trib
    Jul 26, 2016 @ 08:37
    David Peck
    0

    I also assume you've seen the documentation?

  • Nguyen Dung Tri 106 posts 606 karma points
    Jul 26, 2016 @ 08:51
    Nguyen Dung Tri
    0

    I have seen the document but it doesn't show how to start with. Maybe I need to start create a new project along with Umbraco Site or start to create an installable package and add some cs files to existing Umbraco Site. Or something else...?

  • David Peck 690 posts 1896 karma points c-trib
    Jul 27, 2016 @ 08:04
    David Peck
    0

    Yes. I think that's best.

  • Nguyen Dung Tri 106 posts 606 karma points
    Jul 28, 2016 @ 01:53
    Nguyen Dung Tri
    100

    Actually, all the guild for how to use Umbraco Api is here. But the key is to create a controller in folder App_Code of deployed version of Umbraco CMS; the controller then must be inherited from UmbracoApiController.

    There is an example:

    public class ProductsController : UmbracoApiController
    {
        Product[] products = new Product[]
        {
                new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
        };
    
        public IEnumerable<Product> GetAllProducts()
        {
            //Connect to the Umbraco DB
            var db = ApplicationContext.DatabaseContext.Database;
    
            //Get an IENumberable of Product objects to iterate over
            var products = db.Fetch<Product>(new Sql().Select("*").From("Products"));
    
            return products;
        }
    
        public IHttpActionResult GetProduct(int id)
        {
            //Connect to the Umbraco DB
            var db = ApplicationContext.DatabaseContext.Database;
    
            //Get an IENumberable of Products objects to iterate over
            var product = db.Fetch<Product>("SELECT * FROM Products WHERE Id=@0", id).FirstOrDefault();
    
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
    }
    

    All api url from Umbraco should start with “api” and then Controller Name “Products” and then Method Name “GetAllProducts” Example: “api/Products/GetAllProducts” Example Ajax:

    var uri = 'api/Products/GetAllProducts';
    
    $(document).ready(function () {
          // Send an AJAX request
          $.getJSON(uri)
              .done(function (data) {
                // On success, 'data' contains a list of products.
                $.each(data, function (key, item) {
                  // Add a list item for the product.
                  $('<li>', { text: formatItem(item) }).appendTo($('#products'));
                });
              });
        });
    

    Note: For how to create a helloworld plugin for Umbraco can be seen here.

Please Sign in or register to post replies

Write your reply to:

Draft