码迷,mamicode.com
首页 > 其他好文 > 详细

SharePoint 判断用户是否在字段"人员和组"里面

时间:2014-09-15 10:08:38      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   使用   ar   strong   for   

  两个自己平时写的方法,记录下来,方便以后查找使用;

  1、判断用户是否在字段人员和组里面;

    public static bool IsUserInFiled(int UserID, string ListName, int ItemID, string FieldInterName)
        {
            bool value = false;
            try
            {
                using (SPSite site = new SPSite(SITERUL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists.TryGetList(ListName);
                        SPListItem item = list.GetItemById(ItemID);
                        string dispName = list.Fields.GetFieldByInternalName(FieldInterName).Title;
                        SPFieldUserValueCollection uu = new SPFieldUserValueCollection(web, item[dispName].ToString());
                        foreach (SPFieldUserValue u in uu)
                        {
                            if (u.User != null)
                            {
                                if (UserID == u.User.ID)
                                    value = true;
                            }
                            else
                            {
                                string userStr = u.ToString().Split(;)[0];
                                int groupID = Convert.ToInt32(userStr);
                                SPGroup group = web.SiteGroups.GetByID(groupID);
                                foreach (SPUser user in group.Users)
                                {
                                    if (UserID == user.ID)
                                        value = true;
                                }
                            }
                        }
                    }
                }
            }
            catch { }

            return value;
        }

  2、获取Menu菜单,有点弱爆的赶脚;

    public static string GetMenu(string ListName)
        {
            string menuHtml = string.Empty;
            using (SPSite site = new SPSite(SITEURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList(ListName);
                    SPQuery query = new SPQuery();
                    query.Query = "";
                    SPListItemCollection itemcoll = list.GetItems(query);
                    foreach (SPListItem item in itemcoll)
                    {
                        string Title = list.Fields.GetFieldByInternalName("Title").Title;
                        string Link = list.Fields.GetFieldByInternalName("Link").Title;
                        string IsShow = list.Fields.GetFieldByInternalName("IsShow").Title;
                        string IsOpenNewPage = list.Fields.GetFieldByInternalName("IsShow").Title;

                        menuHtml += item[Title].ToString();
                    }
                }
            }
            return menuHtml;
        }

备 注

  仅作个人记录,如有需要,记得先调试哦;

SharePoint 判断用户是否在字段"人员和组"里面

标签:style   blog   color   io   os   使用   ar   strong   for   

原文地址:http://blog.csdn.net/linyustar/article/details/39288397

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!