This tip demonstrates-
How to Populate SharePoint PeopleEditor Control .
How to get selected user from SharePoint PeopleEditor Control .
In .aspx file
<SharePoint:PeopleEditor ID="user" runat="server" SelectionSet="User" AllowEmpty="false" Enabled="false" MultiSelect="false" ValidatorEnabled="false" />
*Here MultiSelect property is set to false which allow only single user selection.
In .cs file
How to Populate SharePoint PeopleEditor Control .
How to get selected user from SharePoint PeopleEditor Control .
In .aspx file
<SharePoint:PeopleEditor ID="user" runat="server" SelectionSet="User" AllowEmpty="false" Enabled="false" MultiSelect="false" ValidatorEnabled="false" />
*Here MultiSelect property is set to false which allow only single user selection.
In .cs file
Method for populating SharePoint:PeopleEditor control with logged in User Name.
private void PopulatePeopleEditor()
{
PickerEntity entity;
SPSite site;
SPWeb web;
try
{
using (site = new SPSite(SPContext.Current.Web.Url))
{
using (web = site.OpenWeb())
{
entity = new PickerEntity();
entity.Key = web.CurrentUser.ID;
//Setting users display name as display text.
entity.DisplayText = web.CurrentUser.Name;
entity.IsResolved = true;
user.Entities.Add(entity);
}
}
}
catch { }
}
Method for getting the selected user from the SharePoint:PeopleEditor control .
private void GetSelectedUser()
{
string userId = string.Empty;
PickerEntity selectedEntity;
SPSitesite;
SPWeb web;
SPUser selectedUser;
try
{
using (site = new SPSite(SPContext.Current.Web.Url))
{
using (web = site.OpenWeb())
{
if (user.ResolvedEntities.Count > 0)
{
selectedEntity = (PickerEntity)user.ResolvedEntities[0];
userId = selectedEntity.Key;
}
if(!string.IsNullOrEmpty(userId))
selectedUser = web.SiteUsers.GetByID(Convert.ToInt32(userId ));
}
}
}
catch { }
}
In this case the SPWeb is coming from the workflowProperties, but it just needs the one that the SPList lives in.
private void PopulatePeopleEditor()
{
PickerEntity entity;
SPSite site;
SPWeb web;
try
{
using (site = new SPSite(SPContext.Current.Web.Url))
{
using (web = site.OpenWeb())
{
entity = new PickerEntity();
entity.Key = web.CurrentUser.ID;
//Setting users display name as display text.
entity.DisplayText = web.CurrentUser.Name;
entity.IsResolved = true;
user.Entities.Add(entity);
}
}
}
catch { }
}
Method for getting the selected user from the SharePoint:PeopleEditor control .
private void GetSelectedUser()
{
string userId = string.Empty;
PickerEntity selectedEntity;
SPSitesite;
SPWeb web;
SPUser selectedUser;
try
{
using (site = new SPSite(SPContext.Current.Web.Url))
{
using (web = site.OpenWeb())
{
if (user.ResolvedEntities.Count > 0)
{
selectedEntity = (PickerEntity)user.ResolvedEntities[0];
userId = selectedEntity.Key;
}
if(!string.IsNullOrEmpty(userId))
selectedUser = web.SiteUsers.GetByID(Convert.ToInt32(userId ));
}
}
}
catch { }
}
Getting AccountName from a "Person or Group" field
This is one I ran into a while back and blogged for reference:
Let's say you have a "Person or Group" field on a SharePoint list... How do you get the username of that user in code? Finally found the answer to this buried in MSDN.
Here is the code snippet to convert "Person or Group" type field in a SharePoint list(which is stored like: "12345#;Some Name") into the correct account name in the form of “<Domain>\<Username>”.
Let's say you have a "Person or Group" field on a SharePoint list... How do you get the username of that user in code? Finally found the answer to this buried in MSDN.
Here is the code snippet to convert "Person or Group" type field in a SharePoint list(which is stored like: "12345#;Some Name") into the correct account name in the form of “<Domain>\<Username>”.
SPFieldUserValue userValue = new SPFieldUserValue(workflowProperties.Web,retrievedFieldValue);
string userName = userValue.User.LoginName;
string userName = userValue.User.LoginName;
No comments:
Post a Comment