Hello Friends
I found one error when I was trying to assign value to List Column which has field type as “Person/Group”. I have one list name as Project and in that list I have column name as “ProjectManager” which has type “Person or group” when I am trying to assign values by programmatically Like below line. listItem["projectmanager"] = "Domain Name\\User Name";
It gives error: Invalid data has been used to update the list item. The field you are trying to update may be read only.
How can we edit or assign value to “Person or group”. I searched and researched and I found solution how can we assign or edit the item which has “Person or group” field type. For that we have to keep in mind some of matters. We can do that thing by:
Spweb Web = Spcontext.Current.Web;
……………………………………………………….
………………………………………………..
Contains code for list declaration and item newly created declaration.
listItem["projectmanager"] = Web.AllUsers["DomainName\\UserName"].ID;
The thing we have to keep in mind is we have to specify user name not only name of user but with Domain Name.We have to give that person's ID to person/Group Field Type
Disha Shah
Showing posts with label Sharepoint Field Types. Show all posts
Showing posts with label Sharepoint Field Types. Show all posts
Wednesday, September 15, 2010
Error when trying to update person or group type field type insharepoint List
Wednesday, June 17, 2009
How to add/update SharePoint person/group field from Object model
While adding/updating the person/group field, we need to take care, because it’s required to be in specific format like value must be ID;#Name when assigning value.
Example code:
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
string loginName = "domainname\\username";
SPUser theUser = web.SiteUsers[loginName];
SPList theList = (SPList)web.Lists["firstcustomlist"];
SPListItem theItem = theList.GetItemById(1);
theItem["usercol"] = theUser.ID.ToString() + ";#" + theUser.Name;
theItem.Update();
theList.Update();
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
Example code:
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
string loginName = "domainname\\username";
SPUser theUser = web.SiteUsers[loginName];
SPList theList = (SPList)web.Lists["firstcustomlist"];
SPListItem theItem = theList.GetItemById(1);
theItem["usercol"] = theUser.ID.ToString() + ";#" + theUser.Name;
theItem.Update();
theList.Update();
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
Labels:
Add or update Sharepoint person/group field,
How to add/update Sharepoint person/group field,
How to add/update SharePoint person/group field from Object model,
List Field Types,
Person/Group Field Type,
Sharepoint,
Sharepoint Field Types,
Sharepoint List Field Types,
Sharepoint person/group field
How to display users to SharePoint People Picker control and get data from SharePoint People Picker control
In below example, I’ll demonstrate how to display users to people picker web control and get data from the same control. I have field called “Staff Assigned” and its data type is People/Group.
Below code will tell us how we can take data from that people/Group field and display to people picker SharePoint web control and also get data from people picker and give back to people/Group field from object model programmatically.
//Display users from people/Group field to People Picker Control
if (itmAudit["Staff Assigned"] != null)
{
char[] to_splitter = { ';' };
string to_list = itmAudit["Staff Assigned"].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
PickerEntity entity = new PickerEntity();
entity.Key = user;
entity = userPicker.ValidateEntity(entity);
entityArrayList.Add(entity);
}
}
userPicker.UpdateEntities(entityArrayList);
//Save users to people/Group field from People Picker Control
SPFieldUserValueCollection values = new SPFieldUserValueCollection();
foreach (PickerEntity entity in userPicker.ResolvedEntities)
{
SPFieldUserValue fuv = new SPFieldUserValue(SPContext.Current.Web, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);
values.Add(fuv);
}
itmAudit["Staff Assigned"] = values;
Below code will tell us how we can take data from that people/Group field and display to people picker SharePoint web control and also get data from people picker and give back to people/Group field from object model programmatically.
//Display users from people/Group field to People Picker Control
if (itmAudit["Staff Assigned"] != null)
{
char[] to_splitter = { ';' };
string to_list = itmAudit["Staff Assigned"].ToString(); // Reads value stored in SPList. (i.e., "Domain\User1; Domain\User2")
string[] arr = to_list.Split(to_splitter);
string user = string.Empty;
System.Collections.ArrayList entityArrayList = new System.Collections.ArrayList();
for (int i = 1; i < arr.Length; i++)
{
if ((i % 2) != 0)
{
user = arr[i].Substring(arr[i].IndexOf("#") + 1);
PickerEntity entity = new PickerEntity();
entity.Key = user;
entity = userPicker.ValidateEntity(entity);
entityArrayList.Add(entity);
}
}
userPicker.UpdateEntities(entityArrayList);
//Save users to people/Group field from People Picker Control
SPFieldUserValueCollection values = new SPFieldUserValueCollection();
foreach (PickerEntity entity in userPicker.ResolvedEntities)
{
SPFieldUserValue fuv = new SPFieldUserValue(SPContext.Current.Web, Convert.ToInt16(entity.EntityData[PeopleEditorEntityDataKeys.UserId]), entity.Description);
values.Add(fuv);
}
itmAudit["Staff Assigned"] = values;
Labels:
Display data to Sharepoint People Picker,
Display users from people/Group field to People Picker Control,
display users to Sharepoint People Picker,
get data from Sharepoint PeoplePicker,
get data from Sharepoint PeplePicker,
How to display users to People Picker and to get data from Peple Picker,
how to display users to people picker web control,
How to display users to SharePoint People Picker control and get data from SharePoint People Picker control,
How to use sharepoint PeoplePicker?,
People/Group Sharepoint Field,
Person/Group Field Type,
PickerEntity,
Sharepoint,
Sharepoint Field Types,
Sharepoint List Field Types,
SharePoint web control
Subscribe to:
Posts (Atom)