Sunday, 21 August 2011

Adding a value to a user field

Adding a value to a user field



This is the same, but in reverse. We use the same class (SPFieldUserValue ) and give it the values we want for the user.



using (SPSite site = new SPSite("http://portal"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Example For Users"];
                    SPListItem item = list.Items[0];

                    SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
                    item["User Name"] = userValue;
                    item.Update();
                }
            }


The image below shows the list after I updated the list item:

No comments:

Post a Comment