Thursday, August 16, 2012

Get value SPFieldUserValueCollection in Sharepoint

Get value from Sharepoint:PeopleEditor and save to list.


Linq expression
            var thisWeb = SPContext.Current.Web;
            var list = thisWeb.Lists["List name"];
            var listItems = list.Items;
            var item = listItems.Add();
            var usersstring = txbTravelers.Accounts;
            var usersfield = new SPFieldUserValueCollection();
            usersfield.AddRange(from object s in usersstring select thisWeb.SiteUsers[s.ToString()]       into user select new SPFieldUserValue(thisWeb, user.ID, user.LoginName));
            item[new Guid("fb51591c-e3d2-401f-9e33-f012c2ff88fa")] = usersfield;
            item.Update();


C# code

var usersstring = txbTravelers.Accounts;
 var usersfield = new SPFieldUserValueCollection();
 foreach (var s in usersstring)
 {
                var user = thisWeb.SiteUsers[s.ToString()];
                usersfield.Add(new SPFieldUserValue(thisWeb,user.ID,user.LoginName));
}

No comments:

Post a Comment