Copied to clipboard

Flag this post as spam?

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


  • jacob phillips 130 posts 372 karma points
    Jan 07, 2015 @ 07:07
    jacob phillips
    0

    Adding Server Side Data To A Property Editor

    I'm trying to get "Step 4" in this tutorial to work on an Umbraco 7 site:

    http://belle-lab.thecogworks.co.uk/assets/belle-workbook.pdf

    I have the following in ~/App_Code/PersonApiController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Web.WebApi;
    using Umbraco.Web.Editors;
    using Umbraco.Core.Persistence;
    namespace My.Controllers
    {
        [Umbraco.Web.Mvc.PluginController("My")]
        public class PersonApiController : UmbracoAuthorizedJsonController
        {
    
            public class Person
            {   
                public int Id { get; set; }
                public string Name { get; set; }
                public string Town { get; set; }
                public string Country { get; set; }
            }
    
            public IEnumerable<Person> GetAll()
            {
                //get the database
                var db = UmbracoContext.Application.DatabaseContext.Database;
                //build a query to select everything from the people table
                var query = new Sql().Select("*").From("people");
                //fetch data from DB with the query and map to Person object
                return db.Fetch<Person>(query);
    
            }
    
    
        }
    
    }
    

    I have set up the table "people" in the Umbraco database and put data in it per the example. When I go to http://mydomain.com/umbraco/My/PersonApi/GetAll

    I just get a 404

    What ma I missing?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 07, 2015 @ 07:49
    Jan Skovgaard
    0

    Hi Jacob

    Well I'm sure you're not that far away from the goal since it's just a 404 message you're getting. So I suspect it's the path to your method that is wrong.

    You should be able to learn more about webapi here http://our.umbraco.org/documentation/Reference/WebApi/ and I think you can probably also benefit from having a look at this angular workbook, which has some more updated info since the belle tutorial was released https://github.com/umbraco/AngularWorkbook

    Try reading this piece in particular https://github.com/umbraco/AngularWorkbook/blob/master/Exercises/Exercise5.md

    Hope this helps.

    /Jan

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 07, 2015 @ 08:28
    Dave Woestenborghs
    2

    The url to your api is wrong :

    You need /umbraco/backoffice/My/PersonApi/GetAll (not sure if /umbraco is needed).

    This is because you inherit your API controller from UmbracoAuthorizedJsonController. This provides some extra security so that your API can only be called by users who are logged in to the backend. So make sure you are logged in in the backend.

    Dave

  • jacob phillips 130 posts 372 karma points
    Jan 07, 2015 @ 09:22
    jacob phillips
    0

    Thanks for the references Jan.

    Dave, that URL (and being logged in) gets me closer. But I get a response of status code 417 - missing token.

    I saw this issue: http://issues.umbraco.org/issue/U4-4149

    I'm on Umbraco 7.1.8 though.

    I tried inheriting from UmbracoAuthorizedApiController instead of UmbracoAuthorizedJsonController and I get the proper response as XML.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jan 07, 2015 @ 09:27
    Jan Skovgaard
    0

    Hi Jacob

    So if you get a 417 response now the path is correct.

    Could you perhaps share some of the code from your controller? Maybe you need to change something more places as well.

    Cheers, Jan

  • jacob phillips 130 posts 372 karma points
    Jan 07, 2015 @ 19:18
    jacob phillips
    0

    Jan,

    The controller is the same code in the original post that I made above in ~/App_Code/PersonApiController.cs, then I'm trying to verify I get back the response using browser tools at this point.

  • jacob phillips 130 posts 372 karma points
    Jan 09, 2015 @ 07:25
    jacob phillips
    0

    So I thought I'd go ahead and set up the package manifest and angular .js files based on this statement by Shannon in: http://issues.umbraco.org/issue/U4-4149

    "When you are using angular's approach it will automatically set the correct request header based on the cookie values"

    I've made a little progress, but am still having issues. Using browser tools, it is shown when loading the backoffice document containing my custom property, the url requested is:

    My/PersonApi/GetAll

    Which generates a 404. However, editing the api url in the browser tools to:

    backoffice/My/PersonApi/GetAll

    succeeds...well, sort of.

    I get a status code of 200, but the response is somehow messed up...the response is exactly this, (well, w/o the '>' on the start of each line):

    > )]}', [{"Id":1,"Name":"Myles A.
    > Pearson","Town":"Tailles","Country":"United
    > Kingdom"},{"Id":2,"Name":"Cora Y.
    > Kelly","Town":"Froidchapelle","Country":"Latvia"},{"Id":3,"Name":"Brooke
    > Baxter","Town":"Mogi
    > dasCruzes","Country":"Grenada"},{"Id":4,"Name":"Illiana T.
    > Strong","Town":"Bevel","Country":"Bhutan"},{"Id":5,"Name":"Kaye
    > Frederick","Town":"Rothesay","Country":"Turkmenistan"},{"Id":6,"Name":"Erasmus
    > Camacho","Town":"Sint-Pieters-Kapelle","Country":"Saint Vincent and
    > The Grenadines"},{"Id":7,"Name":"Aimee
    > Sampson","Town":"Hawera","Country":"Antigua and Barbuda"}]
    

    With an accompanying error message:

    "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

    ...which kinda makes sense I guess based on that initial:

    )]}',

    Well, I thought at least I could change the api url to:

    backoffice/My/PersonApi/GetAll

    in the person.resource.js file, like so:

    > //adds the resource to umbraco.resources module:
    > angular.module('umbraco.resources').factory('personResource', 
    >     function($q, $http) {
    >         //the factory object returned
    >         return {
    >             //this cals the Api Controller we setup earlier
    >             getAll: function () {
    >                 return  $http.get("backoffice/My/PersonApi/GetAll");
    >             }
    >         };
    >     } );
    

    But no matter what I do the change doesn't seem to take hold in Umbraco; that is, when I load a backoffice page, I just get a red error message flashing telling me that "My/PersonApi/GetAll is a 404"

    I've tried the following

    1) Save the data type in the Developer section 2) Delete everything and start over. 3) Delete everything again, change the names and recreate 4) Clear browser cache, delete everything again, change the names again...

    All with the same result. Here's a picture:

    enter image description here

    So I guess I have two issues still

    1) Even when I manually edit the api url in the browser tools, I get a strange JSON response (outlined above) 2) If I edit, delete and recreate, clear cache, Umbraco appears to just be holding onto the same api url.

    For the record, here are the contents of all my files:

    App_Code/Person/personpicker.controller.js

    angular.module("umbraco")
        .controller("My.PersonPickerController", function($scope, personResource){
            personResource.getAll().then(function(response){
                $scope.people = response.data;
            });
        });
    

    App_code/Person/package.manifest

    {   
        //you can define multiple editors   
        propertyEditors: [      
            {
                /*this must be a unique alias*/ 
                alias: "My.Person",
                /*the name*/
                name: "Person Lister",
                /*the html file we will load for the editor*/
                editor: {
                    view: "~/App_Plugins/Person/person.html"
                }
            }
        ]
        ,
        //array of files we want to inject into the application on app_start
        javascript: [
            '~/App_Plugins/ThirdPerson/personpicker.controller.js', 
            '~/App_Plugins/ThirdPerson/person.resource.js'
        ]
    }
    

    ..and App_code/Person/person.html

    <div ng-controller="My.PersonPickerController">
        <a href="#">can I get an amen</a>
        <ul>
            <li ng-repeat="person in people">
                <a href ng-click="model.value = person.Name">{{person.Name}}</a>
            </li>
        </ul>
    </div>
    

    These steps have come from the following:

    http://umbraco.github.io/Belle/#/tutorials/Add-ServerSide-Data

    http://our.umbraco.org/documentation/extending-umbraco/property-editors/creating-tutorial1-v7

    If anyone has any suggestions, I'd love to hear them.

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 09, 2015 @ 08:46
    Sören Deger
    0

    Hi Jacob,

    I had the same issue with cache and wrong url in earlier times. 

    The incorrect URL is probably really still in a cache-trouble. Have you already tested it with another browser?

    Try this:

    - Recycle application pool in IIS
    - Clear Cache in Browser AND Hard-Reload: i.e. in Google Chrome: press F12, click with right on reload icon in url bar of browser and choose "Empty cache & hard-reload"

     

    Hope this helps.

     

    Best 

    Sören

  • jacob phillips 130 posts 372 karma points
    Jan 14, 2015 @ 05:04
    jacob phillips
    100

    Thanks Soren. I had always recycled and cleared cache, but not in these exact steps.

    I think I finally found the problem, and I'm not even sure, but I finally have this working. The issue was in the package.manifest. I think that valueType:"JSON" needs to be specified.

        {   
        //you can define multiple editors   
        propertyEditors: [      
            {
                /*this must be a unique alias*/ 
                alias: "My.Person",
                /*the name*/
                name: "Person Lister",
                /*the html file we will load for the editor*/
                editor: {
                    view: "~/App_Plugins/Person/person.html", 
                    valueType: "JSON"
                }
            }
        ]
        ,
        //array of files we want to inject into the application on app_start
        javascript: [
            '~/App_Plugins/Person/personpicker.controller.js', 
            '~/App_Plugins/Person/person.resource.js'
        ]
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft