add

Wednesday, April 15, 2009

How to set People Picker value in Sharepoint

We all are aware of peoplepicker control.
Today we will discuss how to set its value from code and how to retrieve it.

Suppose we have a peoplepicker control named: pplowner

Setting Value to PeoplePicker from code:

pplOwner.CommaSeparatedAccounts = "EUNET\USERNAME";

If you wish to put the current logged in user name in peoplepicker, you can use:

pplOwner.CommaSeparatedAccounts =SPContext.Current.Web.CurrentUser.LoginName;



Fetching Value of PeoplePicker from code:

if (pplOwner.Accounts.Count > 0)
string userid= pplOwner.Accounts[0].ToString();

You can also iterate the usernames in peoplepicker as;

for (int i=0; i<pplowner.Accounts.Count;i++)
Console.Writeline( pplOwner.Accounts[i].ToString);

2 comments:

Manab Ranjan Basu said...

Good Artical,Very Useful.
To Know more about the .NET Visit at
http://portal-management.blogspot.com

Anonymous said...

you can use this code for valid user.

string Owner = NullConvertor.NullToString(item["Owner"]);
if (!string.IsNullOrEmpty(Owner))
{
int OwnerId = NullConvertor.NullToInteger(Owner.Substring(0, Owner.IndexOf(";#")));
SPUser uOwner = web.AllUsers.GetByID(OwnerId);
if (uOwner != null)
{

System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
PickerEntity entity = new PickerEntity();
entity.Key = uOwner.LoginName;
entity.EntityData["SPUserID"] = uOwner.ID;
entity.EntityData["AccountName"] = uOwner.LoginName;
entity.EntityData["Email"] = uOwner.Email;
entity.EntityData["PrincipalType"] = "User";
entity.EntityData["IXPeople"] = true;
entity.DisplayText = NullConvertor.NullToString(uOwner.Name);
entity.Description = uOwner.LoginName;


entity = OwnerPicker.ValidateEntity(entity);
entityArrayList.Add(entity);

OwnerPicker.UpdateEntities(entityArrayList);
bool Tier = OwnerPicker.Is string Owner = NullConvertor.NullToString(item["Owner"]);
if (!string.IsNullOrEmpty(Owner))
{
int OwnerId = NullConvertor.NullToInteger(Owner.Substring(0, Owner.IndexOf(";#")));
SPUser uOwner = web.AllUsers.GetByID(OwnerId);
if (uOwner != null)
{

System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
PickerEntity entity = new PickerEntity();
entity.Key = uOwner.LoginName;
entity.EntityData["SPUserID"] = uOwner.ID;
entity.EntityData["AccountName"] = uOwner.LoginName;
entity.EntityData["Email"] = uOwner.Email;
entity.EntityData["PrincipalType"] = "User";
entity.EntityData["IXPeople"] = true;
entity.DisplayText = NullConvertor.NullToString(uOwner.Name);
entity.Description = uOwner.LoginName;


entity = OwnerPicker.ValidateEntity(entity);
entityArrayList.Add(entity);

OwnerPicker.UpdateEntities(entityArrayList);
bool Tier = OwnerPicker.IsValid;


}
}
;


}
}


Thanks
Navaratan