Copied to clipboard

Flag this post as spam?

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


  • Naveed Ali 161 posts 426 karma points
    May 19, 2016 @ 09:03
    Naveed Ali
    0

    Controllers

    How do I call my API controller in my Surface Controller???

    I want to call my API method so I can access the URL for it

    Thanks

    Nav

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    May 19, 2016 @ 09:25
    Nik
    0

    Hi Naveed,

    I think you'll probably need to give a bit more of an explanation about your current set up. What type of API controller have you set up? Is it part of the same project? Or is it an API controller in a different website?

    If you are using an Authorised API Controller you probably won't be able to call it from your surface controller as these are backoffice controllers (Someone might correct me there though).

    The more information you can provide, the easier it will be for someone to help you :-)

    Nik

  • Naveed Ali 161 posts 426 karma points
    May 19, 2016 @ 09:47
    Naveed Ali
    0

    Hi Nik,

    yes sorry that would have helped.

    The api controller is in the same solution and not it is not authorized.

    When I go onto the API url I get a generated CSV file which Is what i need but all it has is the headings.

    I believe I need to reference this into my surface controller somehow so I can call that action method in a view.

    here is my API controller:

    {
        public class GenerateDocumentController : UmbracoApiController
        {
            [HttpGet]
            public HttpResponseMessage CreateDocument()
            {
    
                var formGuid = "D6A2C406-CF89-11DE-B075-55B055D89600";
                var records = Library.GetRecordsFromForm(formGuid);
    
                //var members = Services.MemberService.GetMembersByMemberType("Member");           
                var sw = new StringWriter();
                string columnnames = "State,Created,Firstname,Lastname,Jobtitle,Email,Phonenumber,Mobilenumber,Street,City,County,Postcode,Country,Businesstype,Dietaryrequirments,Will you be attending the course,Hiddenfield,Vouchercode,Additionalcomments,Confidentiality";
                sw.WriteLine(columnnames);
    
                var id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
                var date = Convert.ToDateTime(HttpContext.Current.Request.QueryString["date"]);
                var helper = new UmbracoHelper(UmbracoContext.Current);
                //var training = helper.TypedContent(id);
                var training = (TrainingDetail)helper.TypedContent(id);
                var attendeesJson = string.Empty;
                var bookerAttending = string.Empty;
                var voucher = string.Empty;
                var firstName = string.Empty;
                var lastName = string.Empty;
                var email = string.Empty;
                var phoneNumber = string.Empty;
                var jobTitle = string.Empty;
                var dietaryRequirements = string.Empty;
    
                foreach (DynamicRecord record in records)
                {             
                    foreach (var rf in record.RecordFields.Values)
                    {
                        sw.WriteLine(rf.ValuesAsString());
                           var str =
                            string.Format(
                                "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}",
    
                                 record.Created.ToString("dd MMMM YYY"),
    
                                 record.RecordFields.Values.Equals("hiddenfieldField"),                                   
                                 record.RecordFields.Values.Equals("email"),
                                 record.RecordFields.Values.Equals("firstName"),
                                 record.RecordFields.Values.Equals("lastName"),
                                 record.RecordFields.Values.Equals("phoneNumber"),
                                 record.RecordFields.Values.Equals("mobileNumber"),
                                 record.RecordFields.Values.Equals("street"),
                                 record.RecordFields.Values.Equals("city"),
                                 record.RecordFields.Values.Equals("county"),
                                 record.RecordFields.Values.Equals("postCode"),
                                 record.RecordFields.Values.Equals("country"),
                                 record.RecordFields.Values.Equals("businessType"),
                                 record.RecordFields.Values.Equals("dietaryRequirements"),
                                 record.RecordFields.Values.Equals("doYouHaveAVoucherCode"),
                                 record.RecordFields.Values.Equals("company"),
                                 record.RecordFields.Values.Equals("willYouBeAttendingTheCourse"),
                                 record.RecordFields.Values.Equals("jobTitle")                                                
                                 );                            
                        sw.WriteLine(str);                  
                    }
                }         
                var res = Request.CreateResponse(HttpStatusCode.OK);
                res.Content = new StringContent(sw.ToString(), Encoding.UTF8, "text/csv");
                res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "export.csv"
                };
                return res;        
            }
        }
    

    }

    Thanks

    Nav

  • Naveed Ali 161 posts 426 karma points
    May 19, 2016 @ 09:59
    Naveed Ali
    0

    The data is not getting through on my CSV file. All I get is the headings

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    May 19, 2016 @ 10:05
    Nik
    0

    Hey Nav,

    Thanks for the update, have you tried stepping through the API code and ensuring that it is getting results? The fact it is getting the headers makes me think the issue lies in how you are generating the remaining content for file.

  • Naveed Ali 161 posts 426 karma points
    May 19, 2016 @ 10:06
    Naveed Ali
    0

    Hi Nik,

    Yes it was the GUID was wrong. I am now getting data which is awesome.

    BUT!..

    How so I convert this data to display as plain text not as html or json??

    Thanks

    Nav

  • Naveed Ali 161 posts 426 karma points
    May 19, 2016 @ 10:26
    Naveed Ali
    0

    Hi Nik,

    I am trying to get form sumbited data and export it to a CSV, This is my code to extract the form data:

     foreach (DynamicRecord record in records)
                {             
                    foreach (var rf in record.RecordFields.Values)
                    {
                        sw.WriteLine(rf.ValuesAsString());
                           var str =
                            string.Format(
                                "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}",
    
                                 record.State.ToString(),
                                 record.Created.ToString(),
    
                                 record.RecordFields.Values.Equals("hiddenfieldField"),                                   
                                 record.RecordFields.Values.Equals("email"),
                                 record.RecordFields.Values.Equals("firstName"),
                                 record.RecordFields.Values.Equals("lastName"),
                                 record.RecordFields.Values.Equals("phoneNumber"),
                                 record.RecordFields.Values.Equals("mobileNumber"),
                                 record.RecordFields.Values.Equals("street"),
                                 record.RecordFields.Values.Equals("city"),
                                 record.RecordFields.Values.Equals("county"),
                                 record.RecordFields.Values.Equals("postCode"),
                                 record.RecordFields.Values.Equals("country"),
                                 record.RecordFields.Values.Equals("businessType"),
                                 record.RecordFields.Values.Equals("dietaryRequirements"),
                                 record.RecordFields.Values.Equals("doYouHaveAVoucherCode"),
                                 record.RecordFields.Values.Equals("company"),
                                 record.RecordFields.Values.Equals("willYouBeAttendingTheCourse"),
                                 record.RecordFields.Values.Equals("jobTitle")                                                
                                 );                            
                        sw.WriteLine(str);                  
                    }
                }         
    

    The only two fields it generates is the State and created the rest are not pulling through.

    I presume this is because I am outputting it incorrectly.

    What is the correct way to get form data and output it in plain text?

    Thanks for the help

    Nav

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    May 19, 2016 @ 11:20
    Nik
    0

    Hi Nav,

    What is the "DynamicRecord" class, it's not something I've seen before?

    Saying that, your loops don't make sense to me I'm afraid.

    You are looping around the records, then you are looping around the values of each record.

  • Naveed Ali 161 posts 426 karma points
    May 19, 2016 @ 12:03
    Naveed Ali
    0

    Hi Nik,

    Well apparently dynamic record class is class used to get the umbraco form data ..

    What is the correct way to get values from umbraco form?

    Thanks

    Nav

  • Naveed Ali 161 posts 426 karma points
    May 20, 2016 @ 09:52
    Naveed Ali
    0

    Hi ,

    Does anyone know how to get value of custom form fields.

    I have managed to successfully get the created and state data which is from umbraco but cannot access the values of any of the fields I have added

    Here is my code:

    [HttpGet] public HttpResponseMessage CreateDocument() {

            var formGuid = "2d9043ea-4e35-4b88-bc94-0fada0d9a859";
            var records = Library.GetRecordsFromForm(formGuid);
    
            //var members = Services.MemberService.GetMembersByMemberType("Member");           
            var sw = new StringWriter();
            string columnnames = "State,Created,Hiddenfield,Email,Firstname,Lastname,Phonenumber,Mobilenumber,Street,City,County,Postcode,Country,Businesstype,Dietaryrequirments,Vouchercode,Company,Will you be attending the course,Jobtitle";
            sw.WriteLine(columnnames);
    
            var id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
            var date = Convert.ToDateTime(HttpContext.Current.Request.QueryString["date"]);
            var helper = new UmbracoHelper(UmbracoContext.Current);
            //var training = helper.TypedContent(id);
            var training = (TrainingDetail)helper.TypedContent(id);
            var attendeesJson = string.Empty;
            var bookerAttending = string.Empty;
            var voucher = string.Empty;
            var firstName = string.Empty;
            var lastName = string.Empty;
            var email = string.Empty;
            var phoneNumber = string.Empty;
            var jobTitle = string.Empty;
            var dietaryRequirements = string.Empty;
    
            foreach (DynamicRecord record in records)
            {
                //firstName = record.RecordFields.;
    
                foreach (var rf in record.RecordFields.Values)
                {
                    //sw.WriteLine(rf.ValuesAsString());
                    var str =
                     string.Format(
                       ,
                         "{0}, {1}",
    
                        record.State.ToString(),
                        record.Created.ToString("dd MMMM yyy"),                                                                      //record.RecordFields.Values.Equals("hiddenfieldField"),                                   
    
                          );
                    sw.WriteLine(str);
    
                }
            }         
            var res = Request.CreateResponse(HttpStatusCode.OK);
            res.Content = new StringContent(sw.ToString(), Encoding.UTF8, "text/csv");
            res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "export.csv"
            };
            return res;        
        }
    }
    

    Thanks

    Nav

  • Naveed Ali 161 posts 426 karma points
    May 20, 2016 @ 12:29
    Naveed Ali
    0

    anyone else trying to do this I managed to get the data through by doing the below.

    Only issue I now I have is converting the json data to plain string and to get full telephone number showing it is cutting out the first "0" from the number at the moment

    {
    
    public class GenerateDocumentController : UmbracoApiController
    {
        [HttpGet]
        public HttpResponseMessage CreateDocument()
        {
    
            var formGuid = "2d9043ea-4e35-4b88-bc94-0fada0d9a859";
            var records = Library.GetRecordsFromForm(formGuid);
    
            //var members = Services.MemberService.GetMembersByMemberType("Member");           
            var sw = new StringWriter();
            string columnnames = "State,Created,Hiddenfield,Email,Firstname,Lastname,Phonenumber,Mobilenumber,Street,City,County,Postcode,Country,Businesstype,Dietaryrequirments,Vouchercode,Company,Will you be attending the course,Jobtitle";
            //"State,Created,Hiddenfield,Email,Firstname,Lastname,Phonenumber";
    
            sw.WriteLine(columnnames);
    
            var id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
            var date = Convert.ToDateTime(HttpContext.Current.Request.QueryString["date"]);
            var helper = new UmbracoHelper(UmbracoContext.Current);
            //var training = helper.TypedContent(id);
            var training = (TrainingDetail)helper.TypedContent(id);
            var attendeesJson = string.Empty;
            var bookerAttending = string.Empty;
            var voucher = string.Empty;
            var firstName = string.Empty;
            var lastName = string.Empty;
            var email = string.Empty;
            var phoneNumber = string.Empty;
            var jobTitle = string.Empty;
            var dietaryRequirements = string.Empty;
            var mobileNumber = string.Empty;
            var street = string.Empty;
            var city = string.Empty;
            var county = string.Empty;
            var postCode = string.Empty;
            var country = string.Empty;
            var businessType = string.Empty;
            var company = string.Empty;
    
    
            foreach (DynamicRecord record in records)
            {
                //firstName = record.RecordFields.;
    
                foreach (var rf in record.RecordFields.Values)
                {
                    if (rf.Alias.Equals("doYouHaveAVoucherCode")) voucher = rf.ValuesAsString();
                    if (rf.Alias.Equals("willYouBeAttendingTheCourse")) bookerAttending = rf.ValuesAsString();
                    //if (rf.Alias.Equals("hiddenfieldField")) attendeesJson= JsonConvert.SerializeObject(rf.ValuesAsString()); 
                    if (rf.Alias.Equals("firstName")) firstName = rf.ValuesAsString();
                    if (rf.Alias.Equals("lastName")) lastName = rf.ValuesAsString();
                    if (rf.Alias.Equals("email")) email = rf.ValuesAsString();
                    if (rf.Alias.Equals("phoneNumber")) phoneNumber = rf.ValuesAsString();
                    if (rf.Alias.Equals("jobTitle")) jobTitle = rf.ValuesAsString();
                    if (rf.Alias.Equals("dietaryRequirements")) dietaryRequirements =  rf.ValuesAsString();
                    if (rf.Alias.Equals("mobileNumber")) mobileNumber = rf.ValuesAsString();
                    if (rf.Alias.Equals("Street")) street = rf.ValuesAsString();
                    if (rf.Alias.Equals("City")) city = rf.ValuesAsString();
                    if (rf.Alias.Equals("County")) county = rf.ValuesAsString();
                    if (rf.Alias.Equals("postcode"))  postCode = rf.ValuesAsString();
                    if (rf.Alias.Equals("country")) country = rf.ValuesAsString();
                    if (rf.Alias.Equals("businessType")) businessType = rf.ValuesAsString();
                    if (rf.Alias.Equals("company")) company = rf.ValuesAsString();
                    //sw.WriteLine(rf.ValuesAsString());
                    var str =
                     string.Format(
                         "{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}",                        
                         //"{0}, {1}, {2}, {3}, {4}, {5}, {6}",
    
                        record.State.ToString(),
                        record.Created.ToString("dd MMMM yyy"),
                        attendeesJson,
                        email,
                        firstName,
                        lastName,
                        phoneNumber,                      
                        mobileNumber,
                        street,
                        city,
                        county,
                        postCode,
                        country,
                        businessType,
                        dietaryRequirements,
                        voucher,
                        company,
                        bookerAttending,
                        jobTitle      
    
                          );
                    sw.WriteLine(str);
                    //sw.WriteLine(rf.ValuesAsString());
                }
            }         
            var res = Request.CreateResponse(HttpStatusCode.OK);
            res.Content = new StringContent(sw.ToString(), Encoding.UTF8, "text/csv");
            res.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "export.csv"
            };
            return res;        
    
    
           }
        }
    }
    

    Thanks

    Nav

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies