DataTable table = new DataTable();
table.Columns.Add("Title", typeof(string));
table.Columns.Add("UserName", typeof(string));
table.Rows.Add( "Mohamedsithik","User1");
table.Rows.Add("Raja", "User2");
table.Rows.Add("Uthaya", "User3");
table.Rows.Add("Elamaran", "User4");
SPSite mySite = new SPSite(SPContext.Current.Site.ID);
SPWeb myWeb = mySite.OpenWeb();
SPList myList = myWeb.Lists["User"];
string MultiUser = string.Empty;
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
string Mem = Convert.ToString(row["UserName"]);
if (MultiUser != string.Empty)
{
MultiUser = MultiUser + "," + Mem;
}
else
{
MultiUser = Mem;
}
}
SPListItem oSPListItem = myList.Items.Add();
SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
string[] userarray = MultiUser.Split(',');
for (int i = 0; i < userarray.Length; i++)
{
SPFieldUserValue usertoadd = ConvertLoginName(userarray[i]);
usercollection.Add(usertoadd);
}
oSPListItem["Title"] = "Add MultiUser";
oSPListItem["UserName"] = usercollection;
oSPListItem.Update();
public SPFieldUserValue ConvertLoginName(string userid)
{
SPSite oSPsite = new SPSite(SPContext.Current.Site.ID);
SPWeb oSPWeb = oSPsite.OpenWeb();
SPUser requireduser = oSPWeb.EnsureUser(userid);
SPFieldUserValue uservalue = new SPFieldUserValue(oSPWeb, requireduser.ID, requireduser.LoginName);
return uservalue;
}
No comments:
Post a Comment