views:

60

answers:

4

Hi all,

I have list in Sharepoint with two new custom column "First line approvers" and "Second line approvers". Type of this columns is "Person or Group" and "Allow multiple selections" is set to "Yes".

How to get value of custom columns?

I try this method:

string Name = item["Name"] as string;
string ModifiedBy = item["Modified By"] as string;
string FirstLineApprovers = item["First line approvers"] as string;
string SecondLineApprovers = item["Second line approvers"] as string;

and for first two columns I obtain correct values:

"New Text Document.txt"
"1;#SHAREPOINT\\Administrator"

but for my new columns I obtain

null
null

but this columns have values!!!

"First line approvers"

SHAREPOINT\user1
SHAREPOINT\user2
SHAREPOINT\user3

and "Second line approvers"

SHAREPOINT\user4

What is wrong and how I can get values of this columns?

A: 

I would look at how you are accessing the columns. Since the returned value is null, it seems like it cannot reference the columns based on the names you provided. When you make a custom column you specify a name and a display name. Try changing "first line approvers" to whatever the name of the column is (assuming you are using the display name now)

Can you provide a bit more information about the columns? I assume the datatype is SPListItem but it would be helpful to know the attributes of the custom columns you added.

kniemczak
A: 

If your custom columns were created using the browser, try:

string FirstLineApprovers = item["First_x0020_line_x0020_approvers"] as string; 
string SecondLineApprovers = item["Second_x0020_line_x0020_approver"] as string; 
Rich Bennema
+2  A: 

I resolve my problem.

Correct is to cast to SPFieldUserValueCollection and not to string

SPFieldUserValueCollection FirstLineApprovers = (SPFieldUserValueCollection ) item["First line approvers"];

foreach (SPFieldUserValue userValue in FirstLineApprovers)
{
  //...
}
Dumitru
+1: This answer is correct. You see, the `as string` type casting fails because a `SPFieldUserValueCollection` cannot be cased to string. Because you used the defensive `as string` syntax, there was no exception; only `null`.
kbrimington
A: 

Get SharePoint Manager 2007 or 2010 (http://spm.codeplex.com) to check the InternalName property of your fields, as this is the property that item[] uses.

Be careful though - while you can use it to change properties, you shouldn't!

Mike