1

I have a list with a couple of multivalued lookupcolumns. When I try to do something with these fields the count is 0 even though I assigned it a couple of values.

Below is a snippet of my code: (I was not allowed to do SingleOrDefault so please just focus on the Lookup part of my problem :) )

Selvbetjening.SelvbetjeningDataContext context = Init();
VirksomheterItem virksomhet = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
     var virksomheter = context.Virksomheter.Where(i => i.Organisasjonsnummer.Equals(orgnummer)).ToList();

    if (virksomheter.Count == 0 || virksomheter.Count > 1)
        virksomhet = null;
    else
        virksomhet = virksomheter[0];

    virksomhet.Adminbrukere; // this is the multivalued column that has the count of 0 even though ive assigned it a couple of values
3
  • 1
    what exacly does your code do? Your naming is not understandable to me ;)
    – Gwny
    May 26, 2014 at 13:54
  • ah! yes sorry about that. There is some norwegian there. My code gets an organization based on an id and then it lists all the users with admin-, read- and write-rights within that organization. (Obmitted for brevity) So the property Adminbrukere is the property that should contain all the lookups to usernames with admin-rights. But currently it returns count == 0;
    – Pantani
    May 26, 2014 at 14:07
  • But the part where you assign the values to the "Adminbrukere" is not visible. Did you check in Debug-Mode if it is > 0 after assigning?
    – Gwny
    May 26, 2014 at 14:15

2 Answers 2

1

REST will (by default) only supply you with a link-tag to the "Adminbrukere"-list. You will have to expand the link of the lookup content with the following in order to be able to access the nested data.

AddQueryOption("$expand", "Adminbrukere")"

The resulting query will be something like the following:

IEnumerable<BrukereItem> result = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    virksomhet = context.Virksomheter.AddQueryOption("$expand", "Adminbrukere").Where(v => v.Organisasjonsnummer.Equals(virksomhet.Organisasjonsnummer)).First();
    result = virksomhet.Adminbrukere;
});
return result;
1

I see what you are trying to do but there is an easier way than using Linq:

if(item["Adminbrukere"] != null)
{
  string [] choices = item["Adminbrukere"].ToString().Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);
  for (int i= 0; i < choices.Count(); i++)
  {
    string choice = choices[i].ToString();
  }
}
1
  • Indexing is not applicable in this case, and i do want to use the listdatas.svc
    – Pantani
    May 27, 2014 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.