开发者

Get SPListItem in another format

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 SPFieldLookupValue to seperate id from value: var userValue = new SPFieldLookupValue(SPItemName["Created By"] as string)

Then you can:

  • userValue.LookupValue to return UserName
  • userValue.LookupId to return Id

Or SPFieldUserValue

Or better yet, you can create SPFieldUserValue object to access any other user properties like email, login name, etc..

SPFieldUserValue objUserFieldValue = new SPFieldUserValue(web, SPItemName["Created By"].ToString());

Afterwards you can use:

objUserFieldValue.User.LoginName;
objUserFieldValue.User.Name;
objUserFieldValue.User.ID;
objUserFieldValue.User.Groups;
objUserFieldValue.User.Roles;
objUserFieldValue.User.Email;
objUserFieldValue.User.Sid;
objUserFieldValue.User.UserToken;

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜