1

I am pretty new in**.NET** and moreover in SharePoint and I have the following problem working on a SharePoint 2013 project.

Into my code I do:

for (int i = 0; i < eitchettaCorrenteList.Items.Count; i++)
{
    string valoreColonnaIniziativaInternalName = eitchettaCorrenteList.Fields[nomeColonna].InternalName;

    string valore = eitchettaCorrenteList.Items[i][valoreColonnaIniziativaInternalName].ToString();
}

I am iterating on a SharePoint list named etichettaCorrentList and, for each row, retrieving the value of a specific field.

It works bu the problem is that this line:

string valore = eitchettaCorrenteList.Items[i][valoreColonnaIniziativaInternalName].ToString();

returns a strange value that contains the expected value (the one contained in my list) preceded by a code that is not part of the value of the selected field, something like: string;#S01-INIZIATIVA EUROPA 1 TEST

What is this string;? Why am I retrieving it? How can I correctly remove or not obtain this string; before the desired information? What is wrong? What am I missing?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • which data type is eitchettaCorrenteList.Items[i][valoreColonnaIniziativaInternalName]? Maybe ToString function return metadata info too – Joe Taras Dec 12 '18 at 14:40
  • 1
    Don't use `ToString()`. There are *many* different field types, including lookups (which store both ID and text), pictures, multi-value text fields and lookupts etc. The way you wrote the code you are retrieving the internal data. – Panagiotis Kanavos Dec 12 '18 at 14:42
  • Can you say what type of field with that value ? You can see it in SharePoint list in list settings. Each field type can be cast to specific for SharePoint .NET classes. This classes has own methods to get value correctly. And may be if you will use this correct class to get value then your issue will be resolved. –  Dec 12 '18 at 14:46
  • It is a calculated value that simply concatenate 2 text fields. What method have I to use? – AndreaNobili Dec 12 '18 at 14:49
  • `String strPrice = priceField.GetFieldValueAsText(item["Retail_x0020_Price"])` Source: https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ff521580(v%3Doffice.14) – Cleptus Dec 12 '18 at 14:49
  • Why are you using such code? You won't find this in any tutorial. For example, instead of a `for` loop you'll find `foreach (SPListItem it in items)`. Instead of retrieving all fields (an expensive operation) samples show a call to `var items=list.GetItems("Title", "ISBN", "Retail_x0020_Price");` to get only the necessary fields. Since you are using the server-side API, check [How to: Read the Value of a Field in a List Item](https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ff521580(v%3Doffice.14)). – Panagiotis Kanavos Dec 12 '18 at 14:50
  • @AndreaNobili there are no calculated values in SharePoint. Lists aren't tables and fields aren't columns. You can use `GetFieldValueAsText()` to retrieve the value rendered as text or `GetFieldValueAsHtml(Object)` if you want it formatted for display as HTML. BTW if you used the *client* side API retrieving data would be a lot faster. You could use a LINQ query to get what you want – Panagiotis Kanavos Dec 12 '18 at 14:53

1 Answers1

0

Use this link.

Here exists answer to your question. Your field type is SPFieldCalculated. Use such code:

var item = eitchettaCorrenteList.Items[i];
var field = eitchettaCorrenteList.Fields[nomeColonna];
string valoreColonnaIniziativaInternalName = field.InternalName;
if (field.Type.Equals(SPFieldType.Calculated)) {
}
    SPFieldCalculated cf = (SPFieldCalculated)field;
    string valore = cf.GetFieldValueForEdit(item[valoreColonnaIniziativaInternalName ]);
    // or 
    // string valore = cf.GetFieldValueAsText(item[valoreColonnaIniziativaInternalName ]);
}
else {
    string valore = item[valoreColonnaIniziativaInternalName].ToString();
}

Or may be this will work correctly with all cases:

var item = eitchettaCorrenteList.Items[i];
var field = eitchettaCorrenteList.Fields[nomeColonna];
var value = field.GetFieldValueAsText(item[field.InternalName]);