Sharepoint中得到spuser的方法

本文介绍了一种从SharePoint列表项中获取用户详细信息的方法,通过一个实用的代码片段,能够有效地解决仅能获取用户名的基本问题。此外,还展示了如何更新SharePoint中的用户信息。
 

1

Get SPUser object from SPList item

If you are doing a fair amount of programmatic access to SharePoint lists, you will quickly find there are a number of limitations with the data that is returned in each SPList item object in a SPListItemCollection (what is returned from a SPQuery object). One common problem which I have come across is getting more useful information from a user field (rather than their basic name).

I recently came across a post on David San Filippo's Blog which addresses this issue with a simple lookup method that he has written. I have included this below as it is a fantastic little piece of code, which gets you some great information about a user, when doing a query (rather than having to loop over the SiteCollection Users or something similar).

public static SPUser GetSPUser(SPListItem item, string key)
{
      SPFieldUser field = item.Fields[key] as SPFieldUser; 

       if (field != null)
       {
            SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
            if (fieldValue != null)
            {
                return fieldValue.User;
            }
       }
      return null;
 }

2.

SPSite siteCollection = SPControl.GetContextSite(Context);
SPWeb site = siteCollection.AllWebs["Site_Name"];
SPUser user = site.AllUsers["User_Name"];

user.Email = "E-mail_Address";
user.Name = "Display_Name";
user.Notes = "User_Notes";

user.Update();


http://hi.baidu.com/soungcha/blog/item/3fc311d18cb5ee3c9b5027a0.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值