SharePoint 读取 UserMulti 类型的字段

话不多说了,写程序的最喜欢代码,代码如下:

using (SPSite site = new SPSite(""))
            {
                using (SPWeb web = site.RootWeb)
                {
                    _list = web.Lists["List"];
                    _query=new SPQuery();
                    foreach (SPListItem item in _list.GetItems(_query))
                    {
                        //SPUser user = item["UserTest2"] as SPUser;  //这一种写法是不正确的 Debug时User为Null
                        //SPFieldUserValueCollection user = item["UserTest2"] as SPFieldUserValueCollection;//这一种写法是不正确的 Debug时User为Null
                        SPFieldUser fieldUser = item.Fields["UserTest"] as SPFieldUser;
                        if (fieldUser.AllowMultipleValues)  //判断是否允许选择多个用户
                        {
                            SPFieldUserValueCollection userValues = item["UserTest"] as SPFieldUserValueCollection;
                            foreach (SPFieldUserValue userValue in userValues)
                            {
                                if (userValue != null)
                                {
                                    Console.WriteLine("User LookupId:" + userValue.LookupId);
                                    Console.WriteLine("User LookupValue:" + userValue.LookupValue);
                                    Console.WriteLine("User Email:" + userValue.User.Email);
                                    Console.WriteLine("User LoginName:" + userValue.User.LoginName);
                                    Console.WriteLine("User Name:" + userValue.User.Name);
                                }
                                Console.WriteLine();
                            }
                        }
                    }
                }
            }

posted @ 2012-06-01 13:18  @@@一统@@@  阅读(384)  评论(0编辑  收藏  举报