tags:

views:

66

answers:

1

Hi all.

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 few 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

+1  A: 

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);
  }
Timores
Hi Timores,Many thanks to you.Could you please to bonus for me an answer ?I wanted to use 1 dropdownlist to bind all SiteCollections in sharepointWhen I choose which one, then I can get its subsites and bind them to another dropdownlist. When i choose which one on this dropdownlist. I can get sharepoint groups and bind them to another dropdownlist again.That's my ideas.Hope you help meThanks :)
If you have a SPContext, you can use SPContext.Current.Site.WebApplication.Sites to get at this list of site collections. From the SPSite, there is the AllWebs property for all webs and SiteGroups of the RootWeb for the list of groups. Please remember that there is no notion of a group in a sub-site.
Timores
Hi Timores. Thanks again. I did. :)
Good. If the answer suits you, you should accept it, which closes the question.
Timores