SPSite oSPsite = new SPSite(SPContext.Current.Site.ID);
SPWeb oSPWeb = oSPsite.OpenWeb();
SPList list = oSPWeb.Lists["user"];
SPListItem oSPListItem = list.Items.Add();
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
userCollection = UserValidation(oSPWeb, "DomainName/UserName");
oSPListItem["Title"] = "Mohamed sithik";
oSPListItem["UserName"] = userCollection;
oSPListItem.Update();
private SPFieldUserValueCollection UserValidation(SPWeb web, string Users)
{
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
string FormatedUsers = string.Empty;
string[] UserArray = Users.Split(';');
foreach (string sUser in UserArray)
{
SPUser user = null;
try
{
user = web.AllUsers[sUser];
}
catch { }
if (user == null)
{
try
{
web.AllUsers.Add(sUser, "", sUser, "");
web.Update();
user = web.AllUsers[sUser];
}
catch { }
}
if (user != null)
{
userCollection.Add(new SPFieldUserValue(web, user.ID, user.LoginName));
}
}
return userCollection;
}
SPWeb oSPWeb = oSPsite.OpenWeb();
SPList list = oSPWeb.Lists["user"];
SPListItem oSPListItem = list.Items.Add();
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
userCollection = UserValidation(oSPWeb, "DomainName/UserName");
oSPListItem["Title"] = "Mohamed sithik";
oSPListItem["UserName"] = userCollection;
oSPListItem.Update();
private SPFieldUserValueCollection UserValidation(SPWeb web, string Users)
{
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
string FormatedUsers = string.Empty;
string[] UserArray = Users.Split(';');
foreach (string sUser in UserArray)
{
SPUser user = null;
try
{
user = web.AllUsers[sUser];
}
catch { }
if (user == null)
{
try
{
web.AllUsers.Add(sUser, "", sUser, "");
web.Update();
user = web.AllUsers[sUser];
}
catch { }
}
if (user != null)
{
userCollection.Add(new SPFieldUserValue(web, user.ID, user.LoginName));
}
}
return userCollection;
}
No comments:
Post a Comment