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 List Field Types. Show all posts
Showing posts with label Sharepoint List 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
Tuesday, June 9, 2009
One of more field types are not installed properly in CAML Query
Hello Friends,
I got error "One of more field types are not installed properly" .
What does this mean?Let me first explain basic concept which is related to this error and you will find solution.
Concept:
Whenever creating any column for list, SharePoint creates three names for it.
For e.g. trying to create field “Emp Name” gives internal name as “Emp_x0020_Name”
You can think like why this description is needed!!!But for the error like "One of more field types are not installed properly" in their applications.“You must understand this concept vey well. You must have clear picture of that.
So now for that error there is no problem with creating list and their fields.
The problem is simple:
CAML Query always uses Internal Name.
CAML Query uses Internal name not display name.If you have a field called "Program ID" (for example), in CAML you should refer to it as “Program _x0020_ID", because the internal name is encoded to avoid special characters.
Also, the internal name may be totally different from the display name - this will happen if you created a field and then renamed it.
Let’s Say we create a custom List name as “Employee” . Now Title field is already available.
Now I want to rename “Title” with “EmployeeID” But when I rename it ,it only and only changes display name but its internal name does not change. And Internal name is always created when we create field first time. We can not change internalname afterwards. So the internal name remains the same as the old name, but the display name changed. You can change the display name as many times as you want, but can never change the internal name once column is created.
So, how to know about the internal name for field?
Let me clear entire picture by elaborate an example. Every List has Field name as “Created By” field which is having internal name “Author” and Display Name as “Created By”.
If you want to know internal name of particular field how you can indentify .Go to ListàList Settingsà Click On Column (Created By) à so you have Edit Column Page. On that page you can see Column Name Textbox has its display name as “Created By”. Now Look at Addressbar It looks like this.
http://SharepointURL/_layouts/FldEditEx.aspx?List=%7BE6C3FC5E%2D04E6%2D4393%2DB637%2D031CE08C620B%7D&Field=Author
Internal Name of any field is identified by Field=InernalNanme. So Internal Name is for “Created By” is “Author”.
So Now If you wish some easier way to write CAML and do not want to find internal name by checking “Field” from address bar use Some CAML Query Builder.
I provide One table so you can convert Hex code to Character like Program _x0020_ID as so we have x0020=”” See last Row of this table “Program ID”
Disha Shah
I got error "One of more field types are not installed properly" .
What does this mean?Let me first explain basic concept which is related to this error and you will find solution.
Concept:
Whenever creating any column for list, SharePoint creates three names for it.
- External name /Display Name to display in views, forms
- Internal name we use in CAMP Query, Assign Values
- Static Name : Name which is used by field type.
For e.g. trying to create field “Emp Name” gives internal name as “Emp_x0020_Name”
You can think like why this description is needed!!!But for the error like "One of more field types are not installed properly" in their applications.“You must understand this concept vey well. You must have clear picture of that.
So now for that error there is no problem with creating list and their fields.
The problem is simple:
CAML Query always uses Internal Name.
CAML Query uses Internal name not display name.If you have a field called "Program ID" (for example), in CAML you should refer to it as “Program _x0020_ID", because the internal name is encoded to avoid special characters.
Also, the internal name may be totally different from the display name - this will happen if you created a field and then renamed it.
Let’s Say we create a custom List name as “Employee” . Now Title field is already available.
Now I want to rename “Title” with “EmployeeID” But when I rename it ,it only and only changes display name but its internal name does not change. And Internal name is always created when we create field first time. We can not change internalname afterwards. So the internal name remains the same as the old name, but the display name changed. You can change the display name as many times as you want, but can never change the internal name once column is created.
So, how to know about the internal name for field?
Let me clear entire picture by elaborate an example. Every List has Field name as “Created By” field which is having internal name “Author” and Display Name as “Created By”.
If you want to know internal name of particular field how you can indentify .Go to ListàList Settingsà Click On Column (Created By) à so you have Edit Column Page. On that page you can see Column Name Textbox has its display name as “Created By”. Now Look at Addressbar It looks like this.
http://SharepointURL/_layouts/FldEditEx.aspx?List=%7BE6C3FC5E%2D04E6%2D4393%2DB637%2D031CE08C620B%7D&Field=Author
Internal Name of any field is identified by Field=InernalNanme. So Internal Name is for “Created By” is “Author”.
So Now If you wish some easier way to write CAML and do not want to find internal name by checking “Field” from address bar use Some CAML Query Builder.
I provide One table so you can convert Hex code to Character like Program _x0020_ID as so we have x0020=”” See last Row of this table “Program ID”
Character | Internal Hex Code |
~ | _x007e_ |
! | _x0021_ |
@ | _x0040_ |
# | _x0023_ |
$ | _x0024_ |
% | _x0025_ |
^ | _x005e_ |
& | _x0026_ |
* | _x002a_ |
( | _x0028_ |
) | _x0029_ |
_ | _ |
+ | _x002b_ |
- | _x002d_ |
= | _x003d_ |
{ | _x007b_ |
} | _x007d_ |
: | _x003a_ |
“ | _x0022_ |
| | _x007c_ |
; | _x003b_ |
‘ | _x0027_ |
\ | _x005c_ |
< | _x003c_ |
> | _x003e_ |
? | _x003f_ |
, | _x002c_ |
. | _x002e_ |
/ | _x002f_ |
` | _x0060_ |
_x0020_ |
Disha Shah
Subscribe to:
Posts (Atom)