开发者

any way to get sharepoint groups with a given site url

开发者 https://www.devze.com 2022-12-30 11:48 出处:网络
I\'m getting a stuck. Suppose that I create 5 sites. For each one, I create a few sharepoint groups. So, I create a dropdownlist control to bind 5 sites and when I click on any site there, I will get

I'm getting a stuck. Suppose that I create 5 sites. For each one, I create a few sharepoint groups. So, I create a dropdownlist control to bind 5 sites and when I click on any site there, I will get sharepoint groups created on it. But I always see the dropdownlist used to bind these groups still never change. I mean it only binds a fe开发者_StackOverflow中文版w default groups in sahrepoint everytime. The new created groups is not.

And I have confusion like this

web.AssociatedGroups web.Groups web.SiteGroups

which one we will use this case ? Please guide me

Here my snippet code

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()));



                       }

                   }
               }
           });
        }

}

thanks in advance


You don't show how you add groups, so it is hard to see why you can't list them.

Some considerations:

  1. groups are scoped at the site collection level, i.e. there is no notion of groups at the web (SPWeb) level; only at the SPSite level
  2. I wrote code a couple of years ago for managing groups, so I don't remember the difference between the different properties. Looking at my code, I used SPWeb.SiteGroups to test if a group exists and to add a new group.

Here is my - slightly anonymized - code:

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);
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号