Question

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
Was it helpful?

Solution

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;

OTHER TIPS

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();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top