1

I created a custom list which contains different field values e.g.

  • Text
  • Choice
  • Note
  • Currency
  • Peoplepicker

Using the following code I print the field values to console:

List mealList = spLists.GetByTitle("Meals");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXML = "<View/>";
ListItemCollection listItems = mealList.GetItems(camlQuery);
clientcontext.Load(mealList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();

foreach(ListItem item in listItems)
{
Console.WriteLine("Meal: {0}",item.FieldValues["Meal"]); //Textfield
Console.WriteLine("Country: {0}",item.FieldValues["Country"]); //Textfield
Console.WriteLine("Ingredients: {0}",item.FieldValues["Ingredients"]); //Notefield
Console.WriteLine("Price: {0}",item.FieldValues["Price"]); //Currency
Console.WriteLine("User: {0}",item.FieldValues["User"]); //Peoplepicker
}

The output looks fine except for two of the fields.

The Ingredients output somehow seems to print html-tags along with the normal output. So it looks like:

Ingredients: <div class="ExternalClass9B27C4....."><p>water, salt, potatos...</p></div>

Also at some point there are question marks where I never put them in.

The second troublemaker field is the "User". I expected it to print the user's name as a string. instead it writes:

Microsoft.Sharepoint.Client.FieldUserValue

How could i solve this issue?

2 Answers 2

2

About the first one: since it is a note field, I suppose that that is the actual content of the field. SharePoint automagically formatted your text when you inserted it into the list.

About the second one. Again, expected. The user will be returned as an instance of the FieldUserValue class. Cast it to read the actual info. For example:

//Note: will give the display name for the user. To get the ID, use .LookupId  instead
Console.WriteLine("User: {0}",
    ((FieldUserValue)item.FieldValues["User"]).LookupValue ); 

If you need more info you can get them from the user list in the site. This link goes in detail - I am reporting here the main code snippet in case the link goes down:

List userList = ctx.Web.SiteUserInfoList;
ctx.Load(userList);

ListItemCollection users = userList.GetItems(CamlQuery.CreateAllItemsQuery());

ctx.Load(users, items => items.Include(
    item => item.Id, item => item["Name"]));

ctx.ExecuteQueryAsync((ss, eee) =>
{
    ListItem principal = users.GetById(YOUR_USER_ITEM.LookupId);

Also, as a reference. Using the server object model you will get the user from the SPFieldUserValue class.

 SPFieldUser field = item.Fields["User"] as SPFieldUser;
 SPFieldUserValue fieldValue = 
      field.GetFieldValue(item["User"].ToString()) as SPFieldUserValue; 

You shouldn't need to do that, though.

8
  • Is there an easy way of unformatting the note field, without messing around with the string as such? Commented Apr 17, 2014 at 9:01
  • Btw, your solution gets me following exception: Unable to cast object of type 'Microsoft.Sharepoint.Client.FieldUserValue' to type 'Microsoft.Sharepoint.SPFieldUserValue'. Commented Apr 17, 2014 at 9:07
  • @bodycountPP - sorry my bad, that was because I used the server side object model classes. Will edit. Again, sorry for the typo. Commented Apr 17, 2014 at 9:41
  • 1
    @bodycountPP - and done. Give me some time and I will try to see if anything comes to mind about cleaning up the note field. One idea could be disabling rich text support for the field. Commented Apr 17, 2014 at 9:52
  • Thank you this solved the "User" issue. If you could help me with the first problem that would be great. Disabling rich text sound like that would limit possibilities though. ListItem has a 'FieldValuesAsText'-property but I guess thatstill would return html along with the text. Commented Apr 17, 2014 at 10:02
1

For the user you need to get the LookupValue property and then use toString().

objFieldUserValue.LookupValue.ToString();

Microsoft.SharePoint.Client.FieldUserValue Title

For the NoteField try to get the property Text.
NoteField properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.