none
add people or group field of sharepoint list C# code RRS feed

  • Question

  • Hi,

    I an working on adding custom value to SharePoint list, so far I am able to add custom values to SharePoint list. but when it comes to people and groups I am not sure how it works can I add it to the list. other once are pretty straight forward.

    item["Full_x0020_Name"] = empname.Replace('.', ' ');
                            item["Emp_x0020_ID"] = summarydr.EMPLOYEEID.ToString();
                            item["Location"] = summarydr.Location.ToString();
                            item["Division"] = strDivision;
                            item["Department"] = summarydr.PROFESSION;
                            item["Professional_x0020_Title"] = summarydr.POSITION;
                            item["employment_x0020_status"] = true;
                            item["Revision"] = DateTime.Now.Year.ToString();
                            item["Years_x0020_of_x0020_Experience"] = summarydr.TOTALYRSEXP;
                            //item["Experience_x0020_Type"] = str.TrimEnd(',');
                            item["Experience_x0020_Type"] = strExptype;
                            item["CVBuilder"] = "Yes";
                            item["CV_x0020_Coordinator_x0020_Email"] = "Myname@host.com";
    
    
                            //this is the people or group column
                            //item["CV_x0020_Coordinator"] = "Userid";
                            item.Update();
                            uploadFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
                            Context.ExecuteQuery();


    • Edited by roma_victa Tuesday, September 17, 2019 9:19 AM
    Tuesday, September 17, 2019 9:18 AM

All replies

  •  static SPWeb _web;
                static SPSite _site;
                static SPList myList;
                static void Main(string[] args)
                {
                    string usercontrolvalue = "test";
                    _site = new SPSite("URL");
                    _web = _site.OpenWeb();
                    _web.AllowUnsafeUpdates = true;
                    myList = _web.Lists["MYList"];
                    SPListItem item = myList.Items.Add();
                    SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
                    for (int i = 0; i < userarray.Length; i++)
                    {
                        SPUser usr = web.EnsureUser(userarray[i]);
                        SPFieldUserValue usertoadd = new SPFieldUserValue(_web, usr.ID, usr.Name);
                        if (usertoadd.User == null) // value is a SharePoint group if User is null
                        {
                            SPGroup group = web.Groups[usertoadd.LookupValue];
                            SPFieldUserValue groupValue = new SPFieldUserValue(_web, group.ID, group.Name);
                            usercollection.Add(groupValue);
                        }
                        else
                        {
                            usercollection.Add(usertoadd);
                        }
                    }
                    item["Approver"] = usercollection;
                    item.Update();
                }



    Please remember to click Mark as Answer on the answer if it helps you

    Tuesday, September 24, 2019 11:35 PM