One thing I’ve discovered about SharePoint data is that it often has information appended to the beginning of it. If the field shows someone’s name, such as “Mark Smith”, the data as retrieved from SharePoint actually reads “1;#Mark Smith”.
To get rid of this, I use a simple C# function that returns only the name in this case:
String GetStringFromListString(object sItem)
{
string sItemString= sItem.ToString();
return sItemString.Substring(sItemString.LastIndexOf(“#”) + 1);
}
The function is called in the usual fashion for such methods, and that is to use the function name followed by the parameter:
string sAuthor = GetStringFromListString(oTask["Author"].ToString());
This code assumes you already know how to use the SPList and SPListItem objects to find the SharePoint list you wish to manipulate. If not, I have other posts here in the SharePoint section of the blog.
Even worse, when there are multiple items in the list, it can look like this: “1;#Mark Smith;#2;#John Doe”. I haven’t needed to do more than grab the final name so far, in which case my function works, but if needing to grab each of the entries, a new function will be needed. I’ll post it if and when I ever need it myself.
Happy coding!