Question

I have a list item with one field Classes which is a multiple choice lookup. I want to implement an ASPX page which contains a ListBox which is filled with values from the Classes field. The following method does that:

private void GetSelectedClasses()
{
    SPFieldLookupValueCollection currentSelection;

    using (SPWeb web = this.Web)
    {
        ClassesListBox.Items.Clear();

        currentSelection = item["Classes"] as SPFieldLookupValueCollection;
        foreach (SPFieldLookupValue value in currentSelection)
        {
            ClassesListBox.Items.Add(value.LookupValue);
        }
    }
}

This method works fine if I run/debug/deploy it with my account which is a SharePoint admin. However, when I run it under User1 account, which has contribute permissions on the site and the list which contains the list item, I get the strange error:

System.ArgumentException: Value does not fall within the expected range.

The error is happening in the line
currentSelection = item["Classes"] as SPFieldLookupValueCollection;
When I promote the User1 to Full Control permissions user or even site collection administrator, I am still getting the error. The ArgumentException appears to be firing because the field named Classes doesn't exist, allegedly. This is of course not true, because checking the item fields does list it and it all works well with my default account.

Could anyone help me out? I feel quite desperate. Thanks!

Was it helpful?

Solution

Ok, I've solved it. I am not sure why, but when I use User1 account, I need to explicitly specify the fields I want to get when I am retrieving SPItem object. So, previously I have had instantiated the item object with the following line:

this.item = this.list.Items.GetItemById(Convert.ToInt32(itemId, CultureInfo.InvariantCulture));

But, in order to have my Classes field included in the query, I need to use:

this.item = this.list.GetItemByIdSelectedFields(Convert.ToInt32(itemId, CultureInfo.InvariantCulture), "Classes");

My GetSelectedClasses() method mentioned in the question body is actually correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top