Copied to clipboard

Flag this post as spam?

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


  • AnandBhopale 33 posts 79 karma points
    20 days ago
    AnandBhopale
    0

    FieldPreValueSourceType with custom web api service

    I am trying to develop custom prevalue source type which will read data from web service or web api.

    Is there any good example available as reference ?

  • AnandBhopale 33 posts 79 karma points
    20 days ago
    AnandBhopale
    0

    Hi

    I wrote following code. Not sure whether it is correct way or not.

    1. This code works good under Forms -> Prevalue Sources
    2. It is showing all values from web api 3.Then I am able to add dropdown on form also. No error
    3. When I am try to add form on the page. I am getting following exception

    Any idea, how to solve this issue. I am using Clean Starter Kit with umbraco 8

    public class ExperimentDataSource : FieldPreValueSourceType {

        [Setting("Url", Description = "Enter the url to post to", View = "TextField")]
        public string Url
        {
            get;
            set;
        }
    
        [Setting("Value to Display", Description = "Enter the url to post to", View = "TextField")]
        public string ListOfValues
        {
            get;
            set;
        }
    
        public ExperimentDataSource()
        {
            Id = new Guid("74557737-FE48-4911-9FC2-952315B85B63");
            Name = "Read From Web Service";
            Description = "This is experiement";
    
        }
    
        public override List<PreValue> GetPreValues(Field field, Form form)
        {
            List<PreValue> result = new List<PreValue>();
    
    
            using (WebClient http = new WebClient())
            {
                http.Headers.Add(HttpRequestHeader.ContentType, "application/json");
    
                try
                {
                    var json = new WebClient().DownloadString(Url);
                    dynamic dynJson = JsonConvert.DeserializeObject(json);
                    foreach (var item in dynJson)
                    {
                        PreValue pv = new PreValue();
                        pv.Id = item.ID;
                        pv.Value = item[ListOfValues].ToString();
                        result.Add(pv);
                    }
    
                }
                catch (Exception ex)
                {
                    string err = String.Format("There was a problem sending the Record with unique id '{0}' from the Form '{1}' with id '{2}' to the URL Endpoint '{3}' with the method 'POST'");
    
                }
            }
    
    
    
            return result;
        }
    
        public override List<Exception> ValidateSettings()
        {
    
            List<Exception> exceptions = new List<Exception>();
            if (Url.Trim().Length < 1)
            {
                exceptions.Add(new Exception("Url value must be supplied."));
            }
            return exceptions;
        }
    }
    

    exception System.Web.HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Umbraco.Forms.Web.Models.FormViewModel.<>c.

Please Sign in or register to post replies

Write your reply to:

Draft