0

I am trying to remove a user from the SPFieldUserValueCollection but I am getting a null reference from my ListItem Item, Please help.

SPUser userDeleted = SPContext.Current.Web.CurrentUser;

SPList List = spWeb.Lists["BGAG Learning Calendar"];

SPListItem ListItem = List.GetItemById(ItemID);

SPFieldUserValueCollection FieldUserValueCollection = (SPFieldUserValueCollection)ListItem["Course Attendies"];

SPFieldUserValueCollection fieldUserValues = new SPFieldUserValueCollection();

foreach (SPFieldUserValue fieldUserValue in FieldUserValueCollection)     
  {

    if (fieldUserValue.LookupId != userDeleted.ID && fieldUserValue.LookupValue != userDeleted.Name)     
    {

     fieldUserValues.Remove(new SPFieldUserValue(spWeb, fieldUserValue.LookupId, fieldUserValue.LookupValue));

    }

  }

 ListItem["Course Attendies"] = fieldUserValues;

 ListItem.Update();
1

Where do you get the null reference exception?

I had a null reference with a Field of type FieldChoice.

This helped me to convert an get values:

FieldChoice groupChoice = cc.CastTo<FieldChoice>(item["ColName"])

where CC is ClientContext object.

0

Ah no...

You have to use following:

cc.Load(ListItem);
cc.ExecuteQuery();

where cc is ClientContext .

Just after loading the item and executing from ClientContext, the item instance is available.

Be sure your list is loaded properly too.

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.