in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ton Stegeman [MVP] weblog

I have moved my blog to http://www.tonstegeman.com/blog. If you have a blogpost at sharepointblogs that does not display all the content on the right hand side, please go to the new blog. It has all the posts that are on this blog as well. I you have any feedback, please send me a message through the contact form.

SharePoint 2007 SiteGroups - part 2 - Creating SiteGroups in code

This is the second article in a series (of 3) about SharePoint sitegroups. In part 1 I discussed how you can manage these site groups by using the MOSS interface. I also introduced the scenario that we will build on in this item. After creating a special teamsite for each of our customers, we configured the site groups for this customer site. This was quite a few mouse clicks. Takes a lot of time to configure this manually. Also we want to standardize the naming of these groups across all sites that have the same site definition. That is hard to do manually. And we prefer to give our account managers a self-service system to create and maintain their customer sites. Explaining them how to configure sitegroups and permission levels is not going to work.

We created a workflow that does all this work for us. This workflow is triggered by editting or creating an item in a list with customer data. This workflow is part of what we call our “SiteBuilder”. In this post I will show how you can create the same sitegroups and configure the QuickLaunch in the site to show them.

First we’ll get a reference to the site and we will try to find the account manager:

            string customerName = "Contoso";
            string accountManager = @"office2007\jsmith";
 
            SPSite site = new SPSite(string.Format("http://office2007:90/customers/{0}", customerName));
            SPWeb web = site.OpenWeb();
            SPUser owner = web.AllUsers[accountManager];

Then we generate the name for the Customer Team group. In this case this will be “Contoso Customer Team”. We will check if the group already exists. If it doesn’t, we’ll add it. The Account Manager will be the owner of the group.

Update 21-01-2007: Please read this item if you want to set a hyperlink in the group description, just like SharePoint does out of the box.

            string groupName = string.Empty;
            SPGroup group = null;
            string groupAssociations = string.Empty;
 
            groupName = string.Format("{0} Customer Team", customerName);
            group = GetSiteGroup(web, groupName);
            if (group == null)
            {
                web.SiteGroups.Add(
                    groupName, owner, null,
                    string.Format("{0} customer team", customerName));
                group = GetSiteGroup(web, groupName);
            }

The owner is the second parameter of the Add. This is a SPMember object, so it can be both a user and a site group. Being the owner of the groups, the Account Manager can manage the membership of these group. He can grant people access to his site, by adding them to the groups, depending on their role. They therefore do not have the “Manage Permissions” permission on their sites, but they can manage the membership themselves. And we keep the nice clean, clear, standardized security setup for all our customer sites.

The function GetSiteGroup that you saw above is just a helper function to test if a group exists.

        private SPGroup GetSiteGroup(SPWeb web, string name)
        {
            foreach (SPGroup group in web.SiteGroups)
                if (group.Name.ToLower() == name.ToLower())
                    return group;
            return null;
        }

If you get it straight from the collection, you get an error when it doesn’t exists.

The next step is to setup the SiteGroup we just created as the “Visitors” sitegroup for our site. By doing this you will see this group appearing as “Visitors to this Site” when you go to “Set Up Groups”. It took somedigging around before I found where this is stored. It turned out to be the property bag of the SPWeb. If you are interested in the property bag, please check this weblog by Serge van den Oever. The property bag has 3 properties for these groups:

  • vti_associatevisitorgroup
  • vti_associatemembergroup
  • vti_associateownergroup

To set one of the values, use this line of code:

        web.Properties["vti_associatevisitorgroup"] = group.ID.ToString();

For the other 2 groups the process is exactly the same. For the Account Managers group we will make 1 difference. We will already add our account manager to the group, which gives him/her direct access to the site after it is created. This can be done when creating the group by using the defaultUser parameter (an SPUser object):

        web.SiteGroups.Add(
              groupName, owner, owner,
              string.Format("e-office account managers team for {0}", customerName));

The last step is to configure the site to show our 3 custom groups in the quick launch when users navigate to “People and Groups”. Once again this is stored in the propertybag. This has a property called “vti_associategroups”. This contains a string value with the ID’s of the groups that you want to show, separate by “;”.

        web.Properties["vti_associategroups"] = "23;24;25";
        web.Properties.Update();

Please don’t forget to call the Update of the property bag after you updated any of these properties, otherwise they will not be persisted.

If all is well you end up with 3 new sitegroups, that are attached to the site, accessible from the People and Groups menu and can be managed by the account manager:

Sitegroups8

I have only shown the most important pieces of the code here. This should be enough to get you started. Next time I will show how to set the permissions for these groups.

 

Comments

No Comments

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts