以任何方式获得具有给定站点URL的sharepoint组

时间:2010-05-18 10:48:24

标签: sharepoint

我陷入了困境。假设我创建了5个站点。对于每一个,我创建了一些sharepoint组。因此,我创建了一个下拉列表控件来绑定5个站点,当我点击那里的任何站点时,我将在其上创建共享点组。但我总是看到用于绑定这些组的下拉列表仍然永远不会改变。我的意思是它每次只在sahrepoint中绑定一些默认组。新创建的组不是。

我有这样的困惑

web.AssociatedGroups web.Groups web.SiteGroups

我们将使用哪一个案例?请指导我

这是我的代码段

private void BindSPGroupsToDropDownList(DropDownList ddl,string siteUrl)         {

        ddl.Items.Clear();
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SPSite site = new SPSite(siteUrl))
               {
                   using (SPWeb web = site.OpenWeb())
                   {
                       //web.SiteGroups
                       foreach (SPGroup spGroup in web.Groups)
                       {


                           ddl.Items.Add(new ListItem(spGroup.Name.Trim(), spGroup.ID.ToString()));



                       }

                   }
               }
           });
        }

}

提前致谢

1 个答案:

答案 0 :(得分:1)

您没有显示如何添加群组,因此很难理解为何无法列出群组。

一些注意事项:

  1. 组的范围是网站集级别,即网络(SPWeb)级别没有组的概念;仅限于SPSite级别
  2. 几年前我编写了用于管理组的代码,所以我不记得不同属性之间的区别。查看我的代码,我使用SPWeb.SiteGroups来测试组是否存在以及添加新组。
  3. 这是我的 - 稍微匿名化 - 代码:

    public static void CreateSiteGroup(SPSite site, string strGroupName, string strGroupDesc, string strGroupOwner) {
        if (site == null) {
            string message = GetMessagePrefix() + " Site is null";
            XxxLog.Error(XxLogEventId.Common, message, XxxLogCategory.CommonBusiness);
         } else if (String.IsNullOrEmpty(strGroupName)) {
            string message = GetMessagePrefix() + " The group name is empty";
            XxxLog.Error(XxxLogEventId.Common, message, XxxLogCategory.CommonBusiness);
         } else {
            try {
                using (SPWeb rootWeb = site.RootWeb) {
                    SPMember owner;
                    if (String.IsNullOrEmpty(strGroupOwner)) {
                        owner = rootWeb.CurrentUser;
                    } else {
                        if (!ContainsGroup(site, strGroupOwner)) {
                            string message = GetMessagePrefix() + " Can not find owner group name: " + strGroupOwner;
                            XxxLog.Error(XxxLogEventId.Common, message, XxxLogCategory.CommonBusiness);
                            return;
                        } else {
                            owner = rootWeb.SiteGroups[strGroupOwner];
                        }
                    }
    
                    if (!ContainsGroup(site, strGroupName)) {
                        rootWeb.SiteGroups.Add(strGroupName,
                                               owner,
                                               null, // no default user
                                               strGroupDesc);
                    } else {
                        string message = GetMessagePrefix() + " The group " + strGroupName + " was already present";
                        XxxLog.Info(message, XxxLogCategory.CommonBusiness);
                    }
                }
            } catch (Exception e) {
               string message = GetMessagePrefix() + " Cannot create " + strGroupName + " group";
               XxxLog.Error(XxxLogEventId.Common, message,e, XxxLogCategory.CommonBusiness);
            }
         }
      }
    
      public static Boolean ContainsGroup(SPSite site, string name) {
         SPGroup group = null;
         using (SPWeb rootWeb = site.RootWeb) {
            foreach (SPGroup g in rootWeb.SiteGroups) {
               if (g.Name.ToUpper().Equals(name.ToUpper())) {
                  group = g;
                  break;
               }
            }
         }
    
         return (group != null);
      }