1

I am trying to remove a user from SPFieldUserValueCollection field and I keep getting an error

Object reference not set to an instance”

Here is my code

string userDeleted1 = SPContext.Current.Web.CurrentUser.ToString();
SPUser userDeleted2 = sPWeb.SiteUsers[userDeleted1];

SPFieldUserValueCollection Users = (SPFieldUserValueCollection)item[fieldName];
SPFieldUserValue DeletedUserValue = new SPFieldUserValue(sPWeb, userDeleted.ID, userDeleted.Name);
if (DeletedUserValue != null)
{
    Users.Remove(DeletedUserValue);
    item[fieldName] = Users;
    item.Update();
}
2

Looks like you changed from userDeleted2 to userDeleted in your code. Try this instead:

string userDeleted1 = SPContext.Current.Web.CurrentUser.ToString();
SPUser userDeleted2 = sPWeb.SiteUsers[userDeleted1];

SPFieldUserValueCollection Users = (SPFieldUserValueCollection)item[fieldName];
SPFieldUserValue DeletedUserValue = new SPFieldUserValue(sPWeb, userDeleted2.ID, userDeleted2.Name);
if (DeletedUserValue != null)
{
    Users.Remove(DeletedUserValue);
    item[fieldName] = Users;
    item.Update();
}

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.