Come ottenere un semplice testo da un campo di testo multi linea nel modello dell'oggetto laterale client?

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/96895

Domanda

Sto cercando di leggere il testo normale da un multilinetfield in un elenco. Ecco come il mio codice appare finora:

//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"]);
}
.

Nell'ultima riga in cui cerco di leggere gli ingredienti, sto leggendo da un multilinetfield che è impostato su RichText. Dovrebbe anche rimanere in formato RichText, poiché i collegamenti ipertestuali dovrebbero essere aggiunti lì. Il problema è che l'output non solo contiene i div-tag ma anche alcuni strani punti interrogativi che non ho mai aggiunto al campo. Sto cercando di risolvere questo problema per un paio di giorni, ma sembra che ci siano solo due possibili soluzioni.

    .
  1. Imposta il campo da RichText a PlointiveXt, che non è un'opzione nel mio caso.
  2. Utilizzare Regex per rimuovere i div-tag. Si prega di non suggerire questa opzione. Principalmente perché non ritengo che questa sia una soluzione pulita per questo problema. Inoltre, ci ho provato e rimuovei i tag ma quegli strani punti interrogativi rimangono.

    Sembra che ci sia una terza opzione che ho trovato qui . È anche menzionato in questo domanda e sembra lavorare. Ma non posso farlo funzionare per me. Ecco come appare:

    string myString = item.FieldMultiLineText[Field_Name]
    
    .

    Quindi mi chiedo cosa si suppone che il item sia qui? Suppongo che non sia un ListItem perché nel mio caso non offre una proprietà FieldMultiLineText.

    Un breve snippet di codice informativo sarebbe fantastico se decidi di aiutarmi con questo problema.

    Grazie in anticipo.

È stato utile?

Soluzione

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));

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top