zoukankan      html  css  js  c++  java
  • 【MOSS】SPUser的操作

    处理Sharepoint中的SPListItem时,有时需要获得这个对象的"Created By"(创建者),并且希望是SPUser类型,而不是string,这样可以进而得到该用户的权限、ID等等。
    "Person or Group"(用户或组)对应的类型是SPFieldUser,怎么把SPFieldUser转成SPUser呢?
    网上找了些资料,发现要获得SPUser需要借助SPFieldUserValue,借鉴了一些代码,自己试了试,写了一个通用的方法:
             SPUser GetSPUserFromSPListItemByFieldName(SPListItem spItem, string fieldName)
            {
                string userName = spItem[fieldName].ToString();
                SPFieldUser _user = (SPFieldUser)spItem.Fields[fieldName];
                SPFieldUserValue userValue = (SPFieldUserValue)_user.GetFieldValue(userName);
                return userValue.User;
            }
    //SharePoint中的"用户或用户组"栏,当选择了"允许多重选择"后,用对象模型SPListItemCollection["栏名"]获得到的是SPFieldUserValueCollection的对象:
    SPFieldUserValueCollection users = SPListItem["栏名"] as SPFieldUserValueCollection;
    如果将SPListItemCollection["栏名"]输入字符串的话,是"用户ID;#用户Name",
     一 为栏赋值有这几种方式,
    SPListItem["栏名"] = SPUser实例或者SPGroup实例,但是不能赋SPUserCollection;
    SPListItem["栏名"] = "用户ID;#用户Name;#用户ID;#用户Name......";
    SPListItem["栏名"] = SPFieldUserValueCollection实例;(当栏设置允许多重选择为否时,赋的值都是集合中的第一个)
    SPListItem["栏名"] = SPFieldUserValue实例;
     二 获取栏的值
    当栏设置为"允许多重选择"为否时,不管用SPFieldUserValueCollection userValues=item["栏名"] as SPFieldUserValueCollection;还是SPFieldUserValue userValue = item["栏名"] as SPFieldUserValue;得到的值都是Null;
    如果想判断是否选择了"允许多重选择",可以先查看SPFieldUser字段的AllowMutipleValues属性.不过真麻烦.
    实际上在该字段在"允许多重选择"为否时,字段类型是string,为是时,字段类型是SPFieldUserValueCollection.
              protected void btnOK_Click(object sender, EventArgs e)
            {
                ArrayList list = PeopleEditor1.ResolvedEntities;
                string msgTo = "";
                //获取id和显示名称
                foreach (Microsoft.SharePoint.WebControls.PickerEntity p in list)
                {
                    string userId = p.EntityData["SPUserID"].ToString();
                    msgTo += userId + "#";
                }
                //获取帐号
                ArrayList selectedAccoutList = PeopleEditor1.Accounts;
                string selectedAccouts2 = PeopleEditor1.CommaSeparatedAccounts;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    if (msgTo != "")
                    {
                        for (int i = 0; msgTo.Length > 1; i++)
                        {
                            string lassmsgto = string.Empty;
                            lassmsgto = msgTo.Substring(msgTo.IndexOf("#") + 1);
                            msgTo = msgTo.Substring(0, msgTo.IndexOf("#"));
                            SPSite site = new SPSite(SPContext.Current.Site.Url);
                            SPWeb web = site.RootWeb;
                            .AllowUnsafeUpdates = true;
                            web.AllowUnsafeUpdates = true;
                            SPListItem item = web.Lists["Messageboard"].Items.Add();
                            item["ToNameID"] = msgTo;
                            item["Content"] = this.txtContent.Text.Trim();
                            item["Title"] = this.txtTitle.Text.Trim();
                            SPFieldUserValue userValue = new SPFieldUserValue(web,msgTo);
                            item["ToName"] = userValue;
                            item.Update();
                            web.AllowUnsafeUpdates = false;
                            site.AllowUnsafeUpdates = false;
                            msgTo = lassmsgto;
                        }
                    }
                });
                GridView1Show();
            }

    我的签名:想要得到,必先做到!
  • 相关阅读:
    geoserver发布地图服务WMTS
    geoserver发布地图服务WMS
    geoserver安装部署步骤
    arcgis api 3.x for js 入门开发系列十四最近设施点路径分析(附源码下载)
    arcgis api 3.x for js 入门开发系列十三地图最短路径分析(附源码下载)
    cesium 之自定义气泡窗口 infoWindow 后续优化篇(附源码下载)
    arcgis api 3.x for js 入门开发系列十二地图打印GP服务(附源码下载)
    arcgis api 3.x for js 入门开发系列十一地图统计图(附源码下载)
    arcgis api 3.x for js 入门开发系列十叠加 SHP 图层(附源码下载)
    arcgis api 3.x for js入门开发系列九热力图效果(附源码下载)
  • 原文地址:https://www.cnblogs.com/BruceGoGo/p/3710354.html
Copyright © 2011-2022 走看看