Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 12:55
    Anthony Candaele
    0

    setting values for uComponents TextString Array

    Hi Guys,

    For a membertype property I am using the uComponents TextString Array data type. This property holds jobs selected by a member like this:

    jobid   job title

    Now I need to set this programmaticaly from my selectjob.ascx user control.

    I tried to do this like this:

    string selectedjobs = jobId.ToString() + "," + jobFunctionTitle;
    jobseeker.selectedjobs = selectedjobs;

    However, when I test this, I get an YSOD in the member for that member:

    the error is: System.ArgumentException: Invalid JSON primitive: test job

    Does anyone know how to set a membertype property of datatype uComponents TextString Array programmaticaly?

    Your help is much appreciated,

    Anthony

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 13:11
    Lee Kelleher
    2

    Hi Anthony,

    The underlying format for the data (as in stored in Umbraco's database) is as aJavaScript serialized List<string[]>, (or a multi-dimensional array of strings).

    The format you'd need to take is...

    "[ ['id1', 'title1'], ['id2', 'title2'] ... ]";

    Or build up a List<string[]> object and serialize it using the JavaScriptSerializer methods?

    Cheers, Lee.

     

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 13:38
    Anthony Candaele
    0

    Perfect:

    Thanks a lot Lee, you saved my (Satur)day. Now I can enjoy the beautifull weather and have a beer :)

    greetings,

    Anthony

    Ghent (Belgium)

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 14:15
    Anthony Candaele
    0

    Hi Lee,

    The beer will have to wait a little longer. When I test this and selected a second job, the first selected job in the uComponent TextString Array data type gets overwritten:

    I configured the data type like this:

    I there something wrong with this configuration?

    greetings,

    Anthony

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 14:22
    Lee Kelleher
    1

    Hi Anthony,

    Concerned to why the images next to the textboxes aren't displaying?

    I'll give it a quick test here... see if I run into the same problem.

    Cheers, Lee.

    PS. I'm sure the beer will taste so much better once this is working! ;-)

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 14:27
    Lee Kelleher
    0

    Quick one... which version of uComponents are you using?

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 14:27
    Anthony Candaele
    0

    ok, thanks for looking into this. My version of Umbraco is 4.7.1

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 14:29
    Anthony Candaele
    0

    oops, posts have crossed, my version of uComponents is 3.0.1

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 14:34
    Lee Kelleher
    0

    Thanks!

    OK, the broken images are a bug.  I wrongly assumed the paths were relative... doh! ;-)

    Still looking into the set the value.

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 14:43
    Anthony Candaele
    0

    Ok, my code looks like this:

    //Add selected job to member_selected_job property of jobseeker membertype

                string selectedjobs = "[ ['" + jobId.ToString() +"', '" + jobFunctionTitle + "']]";

                jobseeker = (MemberProfile)HttpContext.Current.Profile;

                jobseeker.SelectedJobs = selectedjobs;

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 14:48
    Lee Kelleher
    0

    Just checking, are you wanting to overwrite the existing data/value, or append to it?

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 14:55
    Lee Kelleher
    0

    Hi Anthony,

    If you want to append a value to the array, try using the serializer.  Might seem a little overkill, but will work...

    var member = new umbraco.cms.businesslogic.member.Member(1172);
    if (member != null)
    {
        var property = member.getProperty("test");
        if (property != null && property.Value != null)
        {
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            var value = property.Value.ToString();
            var array = serializer.Deserialize<List<string[]>>(value);
    
            // add the items to the array
            array.Add(new string[] { jobId.ToString(), jobFunctionTitle });
    
            // write back to the property
            property.Value = serializer.Serialize(array);
    
            member.Save();  
        }
    }

    Cheers, Lee.

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 16:05
    Anthony Candaele
    1

    Hi Lee,

    Thanks for the code. I hade to tweek it a little because I couldn't acces the Id property of the member:

    //Add selected job to member_selected_job property of jobseeker membertype

                jobseeker = (MemberProfile)HttpContext.Current.Profile;

                var member = Member.GetCurrentMember();

                if (member != null)

                {

                    var property = member.getProperty("member_selected_jobs");

                    if (property != null && property.Value != null)

                    {

                        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

                        var value = property.Value.ToString();

                        var array = serializer.Deserialize<List<string[]>>(value);

     

                        // add the items to the array

                        array.Add(new string[] { jobId.ToString(), jobFunctionTitle });

     

                        // write back to the property

                        jobseeker.SelectedJobs = serializer.Serialize(array);

     

                       

                    }

                }

    But now it works fine:

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 24, 2012 @ 16:09
    Lee Kelleher
    1

    Cool! Now enjoy that beer!

    Also I've fixed the broken images - but that wont be out until next bug-fix release (v3.0.4)  (unless you want to compile the library yourself?)

    Cheers, Lee.

  • Anthony Candaele 1197 posts 2049 karma points
    Mar 24, 2012 @ 16:22
    Anthony Candaele
    0

    Hi Lee,

    I'm glad I could help - a little bit - at  locating this broken images bug. I think I just wait 'till version v3.0.4 of uComponents.

    thanks a lot for your help and have a nice weekend,

    Anthony

     

  • bob baty-barr 1180 posts 1294 karma points MVP
    Sep 19, 2012 @ 23:26
    bob baty-barr
    0

    This looks great, i am using it to store page favorites for members, but for some reason i have a blank one first, then it fills in?

    pretty much using the exact code above...

    what am i doing wrong?

  • bob baty-barr 1180 posts 1294 karma points MVP
    Sep 20, 2012 @ 15:42
    bob baty-barr
    0

    hey, i think i got this working fairly well... not sure about the blank one still... need more testing...

     

    however, how would i check for a duplicate and not add if it already exists - for example i am adding page ids and titles.

    string myValue = currentNode.Id.ToString();

    i am assuming i would wrap this section:

    // add the items to the array
    array.Add(new string[] { myValue, nodename });
     // write back to the property
    property.Value = serializer.Serialize(array);

    but not sure what my check would be - remember, i am a hack ;)

  • bob baty-barr 1180 posts 1294 karma points MVP
    Sep 21, 2012 @ 15:34
    bob baty-barr
    0

    okay, so i have tried to change this process to just saving the page id into a multiple textstring datatype... however, i am running into an issue of how to append that list? using the same methodology gives me int to generic list error. any thoughts?

Please Sign in or register to post replies

Write your reply to:

Draft