Copied to clipboard

Flag this post as spam?

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


  • Craig Bentley 21 posts 41 karma points
    Aug 18, 2013 @ 21:00
    Craig Bentley
    0

    Saving checkboxlist values to custom datatype in umbraco

    Does anyone know if it is possible to save checkboxlist values to a matching data type field in the umbraco member profile?

    I have a contour form which users to complete to store info in some customer member fields I have created. One of the fields is checkboxlist with multiple choices. I have used codefirst custom workflow to create this list and save the values chosen by the user, back into their member profile to an exact copy of the field.

    The form creates an entry fine but values that were chosen on the checkboxlist do not update on their profile. Has anyone achieved this? The values are listed as a comma separated list so I wonder if I need to convert the selections to their matching id numbers first?

    If that is the case, does anyone know how I can do this please?

    thanks.

  • Comment author was deleted

    Aug 20, 2013 @ 11:16

    Hey Craig,

    Yeah it should be possible you'll just need to see how the checkbox list saves it's values and then replicate this

  • Craig Bentley 21 posts 41 karma points
    Aug 26, 2013 @ 18:36
    Craig Bentley
    0

    Thanks for responding Tim.

    If I change the member field type to a text field and tick a few checkboxes on the contour form then the member field is populated with "test1,test2,test3" etc..

    All text values with comma delimeters. So I am thinking that this text value from the checkboxlist field is submitted as a text string but the member field type I created (of type checkboxlist originally) is actually expecting the values as integers (to match the id number of each checkbox entry) and so does not save the value from the contour form into the member field?

    What do you think? If that was the case, how could I get the checkboxlist in contour to save the actual ids instead of the text to the backend?

    I am loading my prevalue list from the same datatype as Im trying to save to. It is a custom datatype called "Organisation Type" with a datatype of Nvarchar.

    cheers

    Craig.

  • Craig Bentley 21 posts 41 karma points
    Aug 26, 2013 @ 20:03
    Craig Bentley
    0

    Hi Tim,

    Further to my last post. I have started again from scratch using your example code and saving the result to a simple textstring field in the backend to see what the result is. This is the entry that appears in the field after I submit the form "System.Collections.Generic.List`1[System.String]"

    I have no idea why this value is appearing in the field instead of a comma separated list of values as I expected? Does this mean the form is not submitting its values correctly?

    I am totally stumped on this so any help is greatly appreciated.

    cheers

    Craig.

  • Comment author was deleted

    Aug 27, 2013 @ 11:01

    Hmmm that's strange, can you share the code for your code first form?

  • Craig Bentley 21 posts 41 karma points
    Aug 27, 2013 @ 13:53
    Craig Bentley
    0

    using System;

    using System.Collections.Generic;

    using System.Web;

    using Umbraco.Forms.CodeFirst;

    using umbraco.cms.businesslogic.member;

    using umbraco.BusinessLogic;

    using Umbraco.Forms.Core.Providers.FieldTypes;

     

    namespace Contour.Organisation

    {

        

        public enum FormPages

        {

            Registration

        }

     

        public enum FormFieldsets

        {

            Details

        }

     

        [Form("Member/Organisation", ShowValidationSummary = true, MessageOnSubmit="You are now registered!")]

        public class Registration: FormBase

        {

            public const string MemberTypeAlias = "Organisation";

            public const string MemberGroupName = "Organisations";

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Mandatory = true)]

            public string Title { get; set; }

     

            [Field(FormPages.Registration,FormFieldsets.Details,

                Mandatory= true)]

            public string Name { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Mandatory = true)]

            public string Organisation { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Caption="Job title",

                Mandatory = true)]

            public string JobTitle { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Mandatory = true,

                Regex = @"(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})")]

            public string Email { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Mandatory = true)]

            public string Telephone { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.CheckBoxList),

                Caption = "Organisation Type",

                Mandatory = true,

                PrevalueSource = "Org Type")]

            public List<string> OrganisationType { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Caption = "Other Organisation Type",

                Mandatory = false)]

            public string OrganisationTypeOther { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.CheckBoxList),

                Caption = "Who benefits from your organisation's work?",

                Mandatory = true,

                PrevalueSource = "Benefits")]

            public List<string> Benefits { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.CheckBoxList),

                Caption = "What kind of activities does your organisation offer?",

                Mandatory = true,

                PrevalueSource = "Activities")]

            public List<string> Activities { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.DropDownList),

                Caption = "What area do you cover?",

                Mandatory = true,

                PrevalueSource = "Area")]

            public string Area { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Caption = "Other Area",

                Mandatory = false)]

            public string AreaOther { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.Textarea),

                Caption = "Describe your organisation's purpose:",

                Mandatory = true)]

            public string Purpose { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Caption = "Charity Number",

                Mandatory = false)]

            public string CharityNumber { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Caption = "Company Number",

                Mandatory = false)]

            public string CompanyNumber { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.DropDownList),

                Caption = "How did you hear about us?",

                Mandatory = true,

                PrevalueSource = "Heard")]

            public string HeardAboutUs { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Caption = "Other Method",

                Mandatory = false)]

            public string HeardAboutUsOther { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.DropDownList),

                Caption = "Can we share you details and opportunites with Volunteering Liverpool Website?",

                Mandatory = true,

                PrevalueSource = "Yes/No")]

            public string ShareDetails { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details,

                Mandatory = false,

                Type = typeof(FileUpload))]

            public string Avatar { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details, 

                Type = typeof(Password),

                Mandatory = true)]

            public string Password { get; set; }

     

            [Field(FormPages.Registration, FormFieldsets.Details, 

                Type = typeof(Password),

                Mandatory = true)]

            public string RepeatPassword { get; set; }

     

            public override IEnumerable<Exception> Validate()

            {

                var e = new List<Exception>();

                //checks if email isn't in use

                if(Member.GetMemberFromLoginName(Email) != null)

                    e.Add(new Exception("Email already in use"));

                //makes sure the passwords are identical

                if (Password != RepeatPassword)

                    e.Add(new Exception("Passwords must match"));

     

                return e;

            }

            public override void Submit()

            {

                //get a membertype by its alias

                var mt = MemberType.GetByAlias(MemberTypeAlias); //needs to be an existing membertype

                //get the user(0)

                var user = new User(0);

                //create a new member with Member.MakeNew

                var member = Member.MakeNew(Name, mt, user);

                //assign email, password and loginname

                member.Email = Email;

                member.Password = Password;

                member.LoginName = Email;

     

                //assign custom properties

                member.getProperty("benefits").Value = Benefits;

                member.getProperty("activities").Value = Activities;

                member.getProperty("orgType").Value = OrganisationType;

     

     

                //asign custom properties

                if(!string.IsNullOrEmpty(Avatar))

                    member.getProperty("avatar").Value = Avatar;

                if (!string.IsNullOrEmpty(Title))

                    member.getProperty("title").Value = Title;

                if (!string.IsNullOrEmpty(Organisation))

                    member.getProperty("organisation").Value = Organisation;

                if (!string.IsNullOrEmpty(JobTitle))

                    member.getProperty("jobTitle").Value = JobTitle;

                if (!string.IsNullOrEmpty(Telephone))

                    member.getProperty("telephone").Value = Telephone;

                if (!string.IsNullOrEmpty(AreaOther))

                    member.getProperty("areaOther").Value = AreaOther;

                if (!string.IsNullOrEmpty(OrganisationTypeOther))

                    member.getProperty("orgTypeOther").Value = OrganisationTypeOther;

                if (!string.IsNullOrEmpty(CharityNumber))

                    member.getProperty("charityNumber").Value = CharityNumber;

                if (!string.IsNullOrEmpty(CompanyNumber))

                    member.getProperty("companyNumber").Value = CompanyNumber;

                if (!string.IsNullOrEmpty(HeardAboutUsOther))

                    member.getProperty("heardAboutUsOther").Value = HeardAboutUsOther;

                if (!string.IsNullOrEmpty(HeardAboutUs))

                    member.getProperty("heardAboutUs").Value = HeardAboutUs;

                if (!string.IsNullOrEmpty(Purpose))

                    member.getProperty("purpose").Value = Purpose;

                if (!string.IsNullOrEmpty(Area))

                    member.getProperty("area").Value = Area;

                if (!string.IsNullOrEmpty(ShareDetails))

                    member.getProperty("shareDetails").Value = ShareDetails;

     

                //asssign a group, get the group by name, and assign its Id

                var group = MemberGroup.GetByName(MemberGroupName); //needs to be an existing MemberGroup

                member.AddGroup(group.Id);

                //generate the member xml with .XmlGenerate

                member.XmlGenerate(new System.Xml.XmlDocument());

                //add the member to the website cache to log the member in

                Member.AddMemberToCache(member);

                

            }

        }

    }

  • Comment author was deleted

    Aug 27, 2013 @ 15:47

    Ah yes you'll need to update

    member.getProperty("orgType").Value = OrganisationType;

    to member.getProperty("orgType").Value = string.Join(",", OrganisationType.ToArray());

  • Craig Bentley 21 posts 41 karma points
    Aug 27, 2013 @ 15:56
    Craig Bentley
    0

    Brilliant thanks Tim. I'll give it a go shortly and update post.

    Craig.

  • Comment author was deleted

    Aug 27, 2013 @ 16:02

    Yeah since umbraco won't handle the conversion of the List

  • Craig Bentley 21 posts 41 karma points
    Aug 27, 2013 @ 19:36
    Craig Bentley
    0

    That worked fine Tim.........finally some success for me, thank you!

    I can submit a checkboxlist field and store the value in a textstring field to prove my values are transferring.

    What would you suggest I do next to sort the checkboxlist to checkboxlist problem. I have changed the member field Organsisation Type back to a custom checkboxlist and the prevalues on the contour form use that same field to get a list of options for selection.

    So my code to collect the field value is:

            [Field(FormPages.Registration, FormFieldsets.Details,

                Type = typeof(Umbraco.Forms.Core.Providers.FieldTypes.CheckBoxList),

                Caption = "Organisation Type",

                Mandatory = true,

                PrevalueSource = "Org Type")]

            public List<string> OrganisationType { get; set; }

     

    and the code to pass it to the checkboxlist field in umbraco called orgType is:

    member.getProperty("orgType").Value = OrganisationType;

     

    Does that look correct?

     

  • Comment author was deleted

    Aug 28, 2013 @ 09:40

    Nope it wont match you'll still need to process the OrganisationType before setting the value

    Here is a little snippet that might help

    http://blog.dampee.be/post/2012/04/15/Get-a-prevalues-dropdownlist-in-Razor-(umbraco)-for-a-datatype.aspx

  • Comment author was deleted

    Aug 28, 2013 @ 09:41

    So add this method

    public static Dictionary

    while (preValueIterator.MoveNext()) { 
      retVal.Add(Convert.ToInt32(preValueIterator.Current.GetAttribute("id", "")), preValueIterator.Current.Value); 
    } 
    return retVal; 
    

    }

    Then you'll need the datatype id of the checkboxlist

    GetPrevalues(id) and then simply use that return value to fetch the id based on the value (and store those id's instead of the values)

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 11:04
    Craig Bentley
    0

    So from a logical point of view I feed the List string value from the contour form into the preValueIterator which takes each entry that was ticked and looks up its actual id in the datatype for the checkboxlist?

    Then I store those id numbers again using the GetProperty method for the member profile?

    Is that correct? Looking at the code and the snippet you listed it seems that in my case I would replace dataTypeId (in the new method) with organisationType and then use something like member.getProperty("orgType").Value = retVal; to store the list of id's in the member field.

    Hopefully Ive not misunderstood but please let me know if Im not getting it!

  • Comment author was deleted

    Aug 28, 2013 @ 11:12

    I'll double check in a min, but the checkboxlist datatype stored a comma separated list of id's so that's what you need to feed into the member property in order to have the correct ones selected

  • Comment author was deleted

    Aug 28, 2013 @ 11:15

    Hmm it doesn't seem to save the id's so just feed it in a comma seperated list of values and that should work

  • Comment author was deleted

    Aug 28, 2013 @ 11:17

    So member.getProperty("orgType").Value = string.Join(",", OrganisationType.ToArray()); should work

    How does your datatype look that you use for the orgType property, mind sharing a screenshot of that one?

  • Comment author was deleted

    Aug 28, 2013 @ 11:18

    Since you'll need to populate that with the same values .... but share a screenshot of that one and I'll give you some tips, think we are almost there ;)

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 11:30
    Craig Bentley
    0

    Here you go Tim, screenshots attached.

    I use this datatype as the source for the prevalue in contour as well as on the member profile page to store what the user submits on the form.

     

  • Comment author was deleted

    Aug 28, 2013 @ 11:31

    Ok and member.getProperty("orgType").Value = string.Join(",", OrganisationType.ToArray()); doesn't work?

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 11:35
    Craig Bentley
    0

    No. It doesnt throw up any errors when I submit the form but looking at the member profile, the organisationType field has empty tickboxes throughout as if no value was passed to it.

    The entry inside contour shows a comma separated list of values that match my datatype and I even tested submitting the value to a temporary textstring field and the value saved fine again as a comma separated list so I know Contour can save the entry to the backend.................just not to the orgType field??

  • Comment author was deleted

    Aug 28, 2013 @ 11:36

    Ok will give it a try and get back to you in the afternoon, must be something simple missing

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 11:37
    Craig Bentley
    0

    Thanks Tim, appreciate your time.

  • Comment author was deleted

    Aug 28, 2013 @ 13:01

    Ah it does store the keys so we'll need to go from value to key's, I'll post a snippet in a min

  • Comment author was deleted

    Aug 28, 2013 @ 13:14

    Ok got it working :)

    Add this method

           public static Dictionary<int, object> GetPrevalues(int dataTypeId)
           {
               XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(dataTypeId);
               preValueRootElementIterator.MoveNext(); //move to first 
               XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("preValue", "");
               var retVal = new Dictionary<int, object>();
    
               while (preValueIterator.MoveNext())
               {
                   retVal.Add(Convert.ToInt32(preValueIterator.Current.GetAttribute("id", "")), preValueIterator.Current.Value);
               }
               return retVal;
           } 
    

    then you can do

               var vals = GetPrevalues(1049);
               var ids = new List<int>();
               foreach (var org in OrganisationType)
               {
                   if (vals.ContainsValue(org))
                       ids.Add(vals.Single(x => x.Value.ToString() == org).Key);
               }
    
               member.getProperty("test").Value = string.Join(",", ids.ToArray());
    

    Just make sure to pass in the correct datatype id :) (you can see that when you hover the datatype tree)

  • Comment author was deleted

    Aug 28, 2013 @ 13:16

    So problem was that the checkboxlist datatype will store a comma seperated list of id's (keys of the prevalues) and we need to get the id based on the value :)

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 13:27
    Craig Bentley
    0

    Ah ok, Im trying it now.

    When I add the code, visual studio isnt happy with my use of public static Dictionary. The word Dictionary is underlined with error "expected class, delegate, enum, interface or struct". Am I missing a reference somewhere?

  • Comment author was deleted

    Aug 28, 2013 @ 13:32

    Probably adding it to the wrong place, try adding it under the submit method

  • Comment author was deleted

    Aug 28, 2013 @ 13:33

    So

    public override void Submit()

    {

    }

    it goes HERE

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 13:47
    Craig Bentley
    0

    Ok nearly there :-)

    Getting an error on XPathNodeIterator now "type or namespace could not be found" and also on my use of Single "'System.Collections.Generic.Dictionary<int,object>' does not contain a definition for 'Single' and no extension method 'Single' accepting a first argument of type"

    Probably still putting things in the wrong place sorry.

     

  • Comment author was deleted

    Aug 28, 2013 @ 13:51

    Add using System.Linq; using System.Xml.XPath; at the top of your code file

  • Craig Bentley 21 posts 41 karma points
    Aug 28, 2013 @ 13:58
    Craig Bentley
    0

    It works! Success!

    Thank you so much for helping me sort this out Tim. I can't thank you enough. :-)

  • Comment author was deleted

    Aug 28, 2013 @ 13:58

    AH finally, great it's working now and glad I could help out :)

Please Sign in or register to post replies

Write your reply to:

Draft