Cory Peters

Adventures with SharePoint

Jul

21

Properly Populating and Retrieving SharePoint Field Data

By Cory

SharePoint uses a lot of field types that have different underlying schemas, delimiters and formats. I see a lot of people reverse engineer the field information and “hack” the data into the list using a string such as “1;#Title” for a lookup field. Well this isn’t exactly best practice so I’ve put together a reference table below to assist in using the correct data types for populating or retrieving information from a SharePoint list.

Lookup Field

Field Class: SPFieldLookup
Field Value Class: SPFieldLookupValue

Populating Information:

item["FieldName"] = new SPFieldLookupValue("Title"); // SharePoint will do the lookup as long as the LookupValue's are unique
item.Update();

or

item["FieldName"] = new SPFieldLookupValue(1, "Title");
item.Update();

Retrieving Information:

SPFieldLookupValue itemValue = item["FieldName"] as SPFieldLookupValue;
int id = itemValue.LookupId;
string value = itemValue.LookupValue;

Multiple Lookup Field

Field Class: SPFieldLookup
Field Value Class: SPFieldLookupValueCollection

Populating Information:

SPFieldLookupValueCollection itemValues = SPFieldLookupValueCollection();
itemValues.Add(new SPFieldLookupValue(1, "Title"));
item["FieldName"] = itemValues;
item.Update();

Retrieving Information:

SPFieldLookupValueCollection itemValues = item["FieldName"] as SPFieldLookupValueCollection;
foreach (SPFieldLookupValue itemValue in itemValues)
{
int id = itemValue.LookupId;
string value = itemValue.LookupValue;
}

User Field

Field Class: SPFieldUser
Field Value Class: SPFieldUserValue

Populating Information:

web.EnsureUser(@"domain\username");
SPUser user = web.AllUsers[@"domain\username"];
item["FieldName"] = user;
item.Update();

Retrieving Information:

string currentValue = item["FieldName"].ToString();
SPFieldUser userField = list.Fields.GetFieldByInternalName("FieldName");
SPFieldUserValue itemValue = (SPFieldUserValue)userField.GetFieldValue(currentValue);
SPUser user = itemValue.User;

URL Field

Field Class: SPFieldUrl
Field Value Class: SPFieldUrlValue

Populating Information:

SPFieldUrlValue urlValue = new SPFieldUrlValue();
urlValue.Url = "http://www.google.com";
urlValue.Description = "Google";
item["FieldName"] = urlValue;
item.Update();

Retrieving Information:

SPFieldUrlValue urlValue = new SPFieldUrlValue(item["FieldName"].ToString());
string url = urlValue.Url;
string description = urlValue.Description;

Multiple Choice Field

Field Class: SPFieldMultiChoice
Field Value Class: SPFieldMultiChoiceValue

Populating Information:

SPFieldMultiChoiceValue itemValue = new SPFieldMultiChoiceValue();
itemValue.Add("Choice 1");
itemValue.Add("Choice 2");
itemValue.Add("Choice 3");
item["FieldName"] = itemValue;
item.Update();

Retrieving Information:

SPFieldMultiChoiceValue itemValue = new SPFieldMultiChoiceValue(item["FieldName"].ToString());
for (int i = 0; i < itemValue.Count; i++)
{
string choice = itemValue[i];
}

14 Responses so far

Hi this post is great!
However, is it possible to post soemthing on SPFieldUserMulti.

I wish to extract all the users from a multiselect Person field and give them permissions in an event handler.

Any ideas??Thank you

Hi, thanks for the heads up on this matter.

Following you method I’m getting this error “Value does not fall within the expected range” when I make this assignment:

listItem["ItemName"] = User;
//user is an SPUser obj

I filled the User variable with the steps that you provide and debugging it on VS2005 I can see that the value in it is correct. Something like {SPVM\bob}

Any help available?

Thanks,
Roger

SPFieldMultiChoiceValue does not implement an iterator so you can not use a foreach loop to retrieve individual values.

Thanks for the comments guys.

Rogerio, usually you will see that error when your field name you’re looking for doesn’t exist in the collection. In your case “ItemName” might not be a valid column on your list.

Dimitrie, you’re absolutely right and I’ve updated the code to reflect this issue.

I create the class Lookup to validate the fields of Lookup and insert them in SharePoint.
When I create a document, and call this class gives me the following error to save the fields Lookup …
ERROR:
Value does not fall within the expected range

Class Lookup:

public class WPLookUP : Indice
{
DropDownList dropDownList;
SPFieldLookup lookup;

public WPLookUP(SPFieldLookup l, string defaultValue)
: base(l)
{

lookup = l;
dropDownList = new DropDownList();
bool exist = false;
SPList list;
defaultValue = “Escolha uma opção”;
dropDownList.Items.Add(new ListItem(string.Empty, string.Empty));

using (SPSite site = new SPSite(WebSettings.SiteUrl + WebSettings.DCSite))
{
using (SPWeb web = site.OpenWeb())
{
list = web.Lists.GetList(new Guid(lookup.LookupList), false);

SPListItemCollection items = list.Items;

foreach (SPListItem spitem in items)
{

ListItem item = new ListItem();

string value = (string)spitem[lookup.Title];

SPFieldLookupValue lookupValue = (SPFieldLookupValue)lookup.GetFieldValue(value);-> Error

item.Text = lookupValue.LookupValue;

item.Value = lookupValue.LookupValue;

item.Attributes.Add(“LookupId”, lookupValue.LookupId.ToString());

if (defaultValue != null)
{
if (defaultValue.Equals(spitem[lookup.LookupField].ToString()))
{
item.Selected = true;
exist = true;
}
}
dropDownList.Items.Add(item);

}
}
}

}
Help me please…

Hi, I hope you can help. I need to populate a SPUserField control that is not part of a list, it will be displayeed on a custom web part. How can I tell the SPUserFIeld which user I want it to display?

thanks!

The Value is not within expected range error is because the method expects an ID if you know the ID then you can fill it in yourself like Blogposter did. But if your ideas are created dynamically or you have hundreds of items in your lookup field then that could be a problem.

Check out the link below for a helper class to retrieve a an ID from a specific lookup list item. My lookup column is working just great now thanks to corey and the guy below. You gotta love the sharepoint community.

http://blogs.sharepointguys.com/matt/sharepoint-2007-development/splookupfield-helper-function/

sorry its cory, my bad

ideas = ID’s

!@#%^&*

Thanks, Worked Perfectly.
I’m not sure why it doesn’t implement IEnumerable

Thank you for the most needed information. I am trying to do some SPQuery and I think my CAML might be crashing due to some of the lookup fields coming back with raw formatting? Can your technique be used for that?

Can this technique be used when running field compares in SPquery? Like in a contains or eq?

I am thinking CAML might not match the values due to the SP formatting junk?

” + “srch1, srch2″ + “

Thanks so much for summarizing the classes for the raw data types and the great examples!!! I had been searching for how to handle these.

Leave a comment