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?
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:
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();
}
}
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?
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
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...
Or build up a List<string[]> object and serialize it using the JavaScriptSerializer methods?
Cheers, Lee.
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)
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
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! ;-)
Quick one... which version of uComponents are you using?
ok, thanks for looking into this. My version of Umbraco is 4.7.1
oops, posts have crossed, my version of uComponents is 3.0.1
Thanks!
OK, the broken images are a bug. I wrongly assumed the paths were relative... doh! ;-)
Still looking into the set the value.
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;
Just checking, are you wanting to overwrite the existing data/value, or append to it?
Hi Anthony,
If you want to append a value to the array, try using the serializer. Might seem a little overkill, but will work...
Cheers, Lee.
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:
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.
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
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?
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.
i am assuming i would wrap this section:
but not sure what my check would be - remember, i am a hack ;)
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?
is working on a reply...