2

I am trying to read plain text from a MultiLineTextField in a List. This is how my code looks so far:

//Get connection
ClientContext context = new ClientContext("URL");
Web site = context.Web;
context.Load(site);
context.ExecuteQuery();

//Get list collection
ListCollection lists = context.Web.Lists;
context.Load(lists);
context.ExecuteQuery();

//Get specific list
List menu = lists.GetByTitle("menu");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXML = "<View/>";
ListItemCollection dishes = menu.GetItems(camlQuery);
context.Load(menu);
context.Load(dishes);
context.ExecuteQuery();

//Iterate through all dishes

foreach(ListItem dish in dishes)
{
Console.WriteLine("Dish: {0}:", dish.FieldValues["Name"]);
Console.WriteLine("Price: {0}:", dish.FieldValues["Price"]);
//Here I get my problem
Console.WriteLine("Ingredients: {0}:", dish.FieldValues["Ingredients"]);
}

In the last line where I try to read the ingredients, I am reading from a MultiLineTextField which is set to RichText. It is also supposed to stay in RichText format, as hyperlinks are supposed to be added there. The problem is that the output not only contains div-tags but also some weird question marks I never added to the field. I am trying to solve this issue for a couple of days now but it seems that there are only two possible solutions.

  1. Set the field from RichText to PlainText, which is not an option in my case.
  2. Use Regex to remove the div-tags. Please do not suggest this option. Mainly because I do not consider this to be a clean solution for this issue. Plus, I tried it and it removes the tags but those weird question marks stay.

There seems to be a third option I found here. It is also mentioned in this question and it seems to work. But I can not get it to work for me. This is how it looks:

string myString = item.FieldMultiLineText[Field_Name]

So I wonder what is item supposed to be here? I suppose it is not a ListItem because in my case it does not offer a FieldMultiLineText-property.

A short, informative code snippet would be great if you decide to help me with this issue.

Thanks in advance.

3
  • did you try this:dish.FieldValuesAsText("Ingredients");
    – Unnie
    Apr 22, 2014 at 15:49
  • This points in the right direction but FieldValueAsText can not be used as a method. But i guess you already know that ;) Apr 23, 2014 at 6:34
  • it was typo .. ignore that chk my answer post
    – Unnie
    Apr 23, 2014 at 6:40

2 Answers 2

3

There is a FieldMultiLineText class in CSOM which is equivalent to SPFieldMultiLineText class of Server object model.You can try the below code :

dish.FieldValuesAsText["Ingredients"];

Also you have to include the FieldValuesAsText property in the load statement like below:

context.Load(dishes,items=>items.Include(item=>item.FieldValuesAsText));
2
  • Your example did the job and seems to be a more native solution then all the Regex-proposals which are spread all over web. The question marks remain but maybe this has to do with something else. Also could you give a short explanation on what exactly happens in the "context.Load()" line? Apr 23, 2014 at 6:32
  • 1
    @bodycountPP FieldValuesAsText property will not be loaded by default, so you have to explicitly specify that you want that member of the ListItem object to be loaded from server to your client app.
    – Unnie
    Apr 23, 2014 at 6:39
0

I had the same issue and tried Unnie's solution above but that didn't work for me. What I found on another post is what worked for me and thought would share it here and may help someone.

The external developer had added a column to the list as 'Single line of text' which wasn't sufficient for the need. I changed the column type to 'Multiple lines of text' but forgot that by default the Specify the type of text to allow: property had 'Rich text (Bold, italics, text alignment, hyperlinks)' selected. This caused the system to add an unwanted element with a weird 'ExternalClass##########' that was killing the whole functionality.

Changing the radio button list value from Rich text to Plain Text made the difference and no changes to my code required.

Your Answer

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

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