Hi All,
In one of my requirments , I was supposed to develop a custom visual webpart. The webpart was having people editor controls in it.
So this Post is all about People Editor Control.
To have this control on your *.acsx page(user control page), you have to first Register Assembly for it.
Code:
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
For Adding People Editor Control to your Page :
<SharePoint:PeopleEditor AllowEmpty="true" ID="pplprimarycontact"
runat="server" MultiSelect="false" SelectionSet="User" />
or
<SharePoint:PeopleEditor ID="spAttendies" runat="server" MultiSelect="true"
SelectionSet="User" />
Here MultiSelect="true" will allow you to more than one users the the control.
SharePoint Column should be of Person or Group type which hold the value for the input given by the user.
Now some code listing:
1. Getting SPSite and SPWeb Objects
SPSite osite = SPContext.Current.Site;
SPWeb oweb = SPContext.Current.Web;
using (SPSite site = new SPSite(osite.ID))
{
using (SPWeb thisWeb = site.OpenWeb(oweb.ID))
{
//getting items values stored in item id.
SPList lststats = thisWeb.Lists["your list name here"];
// -----------------------------------------------------
// SPListItem edititemid = lststats.AddItem();
or(as per requirment)
//SPListItem edititemid = lststats.Items.GetItemById(itemid);
// .......
// .......
// ... // Rest of the Source code will come here.. which iam providing.
}
1. Converting the user id into SPFieldUserValue (Important)
This Method i will be using in all other Listings. It simply converts a user id into SPFieldUserValue object.
2. For adding single user to sharepoint
list
3. For adding many users to sharepoint list
string[] userarray = spAttendies.CommaSeparatedAccounts.ToString().Split(',');
SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
for (int i = 0; i < userarray.Length; i++)
{
SPFieldUserValue usertoadd = ConvertLoginAccount(userarray[i], osite, oweb);
usercollection.Add(usertoadd);
}
item["Attendees"] = usercollection;
If you are facing any difficulty in using above please reply.
In one of my requirments , I was supposed to develop a custom visual webpart. The webpart was having people editor controls in it.
So this Post is all about People Editor Control.
To have this control on your *.acsx page(user control page), you have to first Register Assembly for it.
Code:
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
For Adding People Editor Control to your Page :
<SharePoint:PeopleEditor AllowEmpty="true" ID="pplprimarycontact"
runat="server" MultiSelect="false" SelectionSet="User" />
or
<SharePoint:PeopleEditor ID="spAttendies" runat="server" MultiSelect="true"
SelectionSet="User" />
Here MultiSelect="true" will allow you to more than one users the the control.
SharePoint Column should be of Person or Group type which hold the value for the input given by the user.
Now some code listing:
1. Getting SPSite and SPWeb Objects
SPSite osite = SPContext.Current.Site;
SPWeb oweb = SPContext.Current.Web;
using (SPSite site = new SPSite(osite.ID))
{
using (SPWeb thisWeb = site.OpenWeb(oweb.ID))
{
//getting items values stored in item id.
SPList lststats = thisWeb.Lists["your list name here"];
// -----------------------------------------------------
// SPListItem edititemid = lststats.AddItem();
or(as per requirment)
//SPListItem edititemid = lststats.Items.GetItemById(itemid);
// .......
// .......
// ... // Rest of the Source code will come here.. which iam providing.
}
1. Converting the user id into SPFieldUserValue (Important)
This Method i will be using in all other Listings. It simply converts a user id into SPFieldUserValue object.
public SPFieldUserValue
ConvertLoginAccount(string userid, SPSite site, SPWeb
web)
{
{
SPFieldUserValue uservalue;
using (SPSite thissite = new SPSite(site.ID))
{
using (SPWeb thisweb = thissite.OpenWeb(web.ID))
{
SPUser requireduser = thisweb.EnsureUser(userid);
uservalue = new SPFieldUserValue(thisweb, requireduser.ID, requireduser.LoginName);
}
using (SPSite thissite = new SPSite(site.ID))
{
using (SPWeb thisweb = thissite.OpenWeb(web.ID))
{
SPUser requireduser = thisweb.EnsureUser(userid);
uservalue = new SPFieldUserValue(thisweb, requireduser.ID, requireduser.LoginName);
}
}
return uservalue;
return uservalue;
}
string srprimaryuser=string
.Empty;
if (pplprimarycontact.ResolvedEntities.Count > 0)
{
PickerEntity primarypickerEntity = (PickerEntity)pplprimarycontact.ResolvedEntities[0];
Hashtable primaryuser = primarypickerEntity.EntityData;
srprimaryuser = primaryuser["AccountName"].ToString();
item["Primary Contact Name"] = ConvertLoginAccount(srprimaryuser, site, thisWeb);
}
if (pplprimarycontact.ResolvedEntities.Count > 0)
{
PickerEntity primarypickerEntity = (PickerEntity)pplprimarycontact.ResolvedEntities[0];
Hashtable primaryuser = primarypickerEntity.EntityData;
srprimaryuser = primaryuser["AccountName"].ToString();
item["Primary Contact Name"] = ConvertLoginAccount(srprimaryuser, site, thisWeb);
}
3. For adding many users to sharepoint list
string[] userarray = spAttendies.CommaSeparatedAccounts.ToString().Split(',');
SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
for (int i = 0; i < userarray.Length; i++)
{
SPFieldUserValue usertoadd = ConvertLoginAccount(userarray[i], osite, oweb);
usercollection.Add(usertoadd);
}
item["Attendees"] = usercollection;
4. Fetching a single user and displaying in people editor control.
ArrayList lst_contactuser = new ArrayList();
string stpuser = Convert.ToString(edititem["Primary Contact Name"]);
int phashindex = stpuser.IndexOf("#");
stpuser = stpuser.Remove(0, phashindex + 1);
if (!stpuser.Equals(string.Empty))
{
SPFieldUserValue puser = ConvertLoginAccount(stpuser, site, thisWeb);
if (puser != null)
{
ArrayList lst_contactuser = new ArrayList();
string stpuser = Convert.ToString(edititem["Primary Contact Name"]);
int phashindex = stpuser.IndexOf("#");
stpuser = stpuser.Remove(0, phashindex + 1);
if (!stpuser.Equals(string.Empty))
{
SPFieldUserValue puser = ConvertLoginAccount(stpuser, site, thisWeb);
if (puser != null)
{
PickerEntity primarypicker = new
PickerEntity();
primarypicker.Key = puser.User.Name;
primarypicker.IsResolved = true;
lst_contactuser.Add(primarypicker);
pplprimarycontact.UpdateEntities(lst_contactuser);
lst_contactuser.Clear();
primarypicker.Key = puser.User.Name;
primarypicker.IsResolved = true;
lst_contactuser.Add(primarypicker);
pplprimarycontact.UpdateEntities(lst_contactuser);
lst_contactuser.Clear();
}
5. Fetching a Multiple users and displaying in
people editor control.
ArrayList _arrayList = new
ArrayList();
SPFieldUserValueCollection users = edititemid["Attendees"] as SPFieldUserValueCollection;
if (users != null)
{
foreach (SPFieldUserValue user in users)
{
PickerEntity _pickerEntity = new PickerEntity();
_pickerEntity.Key = user.User.LoginName;
_pickerEntity.IsResolved = true;
_arrayList.Add(_pickerEntity);
}
spAttendies.UpdateEntities(_arrayList);
}
SPFieldUserValueCollection users = edititemid["Attendees"] as SPFieldUserValueCollection;
if (users != null)
{
foreach (SPFieldUserValue user in users)
{
PickerEntity _pickerEntity = new PickerEntity();
_pickerEntity.Key = user.User.LoginName;
_pickerEntity.IsResolved = true;
_arrayList.Add(_pickerEntity);
}
spAttendies.UpdateEntities(_arrayList);
}
If you are facing any difficulty in using above please reply.
Hello Rohit,
ReplyDeleteNice article. I created Visual web part to send email notifications to AD users in Sharepoint 2010. I have People Editor in the webpart and need to send emails to all the AD Users entered in PE. How can I do that. Appreciated for your help.
Thanks
Satish