I want to get different Rows from an SPListItem. I’ll show you my problem with an example.
This code
Console.WriteLine(SPItemName["Created By"]);
or
Console.WriteLine(SPItemName["Created By"].ToString);
returns “8;UserName” (8 is the User ID).
If I look up the row in SharePoint Designer, i can choose even a format for this data field.
So i could get the html code of this field.
How to set the format (like html code or text) of a datafield in c#?
thanks
Use Either SPFieldLookupValue
If you need just the username, use
SPFieldLookupValueto seperate id from value:var userValue = new SPFieldLookupValue(SPItemName["Created By"] as string)Then you can:
userValue.LookupValueto return UserNameuserValue.LookupIdto return IdOr SPFieldUserValue
Or better yet, you can create SPFieldUserValue object to access any other user properties like email, login name, etc..
Afterwards you can use:
http://www.sharepointkings.com/2009/04/spfielduservalue-and.html
Note: to create SPFieldUserValue you must pass reference to web, that’s because SharePoint has to get additional user information from user information list to construct SPFieldUserValue object.