views:

390

answers:

2

In SharePoint many fields id-value pairs that are formatting like the following id;#value. This is further complicated with fields like multi-lookup where when extracting the value of that field can yield results like id_1;#value_1;#id_2;#value_2;#id_3;#value_3

I am wondering if there is any known built in function that will simplify this process and at the very least remove the IDs from the value.

+2  A: 

Field value objects are stored as strings in the Sharepoint database. For simple values (e.g. "Hello world") this is simple enough. But for complex field values - such as an ID/value pair, how to store the entire value as a single string is obviously more complex as well. Each field value class in Sharepoint is responsible for its own storage implementation. ToString() is responsible for writing a string representation of the value; while the field value's constructor takes a string and is responsible for parsing that and setting all the properties on itself appropriately.

For example, the SPFieldUrlValue (which represents an <a href="url">description</a>) has Url and Description properties. Creating a new SPFieldUrlValue(string fieldValue) object will parse the value and set the properties accordingly.

In order to get a true/correct (and often strongly-typed!) representation of the field value, you must know what type the field is, and what that field's value class is.

Rex M
+2  A: 

The SPField class has many derived classed

For example assuming a Lookup field type (that uses the ID;#value) you can check SPField.Type == SPFieldType.Lookup and then cast SPField to SPFieldLookup and use its overriden methods to get the records value.

See Custom Field Value Classes for more details

Also - If I remember correctly (I can't check this right now so DYOR) you can call .ValueAsText and .ValueAsHtml on the base SPField object and it will remove ID;# from the values.

Ryan