1

I have a web part that submits data to a list and it uses a PeopleEditor control for selecting users.

Once user is selected, when I submit the form it gives following error

Invalid data has been used to update the list item. The field you are trying to update may be read only.

I have included tag prefix as

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Control is inserted as

<SharePoint:PeopleEditor ID="a" runat="server"  PrincipalSource="All" SharePointGroup="Everyone" MultiSelect="false" />

And on click of a submit button-

SPWeb web = SPContext.Current.Web;
SPListCollection vlist = web.Lists;

SPList list = web.Lists[new Guid("{xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}")];
SPListItem listitem = list.Items.Add();
listitem["FormCompletedBy"] = a.ResolvedEntities;
listitem.Update();

Where

FormCompletedBy

is a field in SharePoint List where the data is to be submitted.

1 Answer 1

1

You can save User data as below

string[] userarray = a.CommaSeparatedAccounts.ToString().Split(',');;
SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();

for (int i = 0; i < userarray.Length; i++)
{
     SPFieldUserValue usertoadd = ConvertLoginAccount(userarray[i]);
     usercollection.Add(usertoadd);
}
listItem["FormCompletedBy"] = usercollection;
listItem.Update();

public SPFieldUserValue ConvertLoginAccount(string userid)
{
    SPFieldUserValue uservalue;
    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
    {
        using (SPWeb web = site.OpenWeb())
       {
          SPUser requireduser = web.EnsureUser(userid);
          uservalue = new SPFieldUserValue(web, requireduser.ID, requireduser.LoginName);
       }
    }
    return uservalue;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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