|
In one of the comments on this blogpost on SharePoint sitegroups, I was asked if it is possible to set a hyperlink in the group description, just like SharePoint does out of the box. If you create a new site called “Team”, that site has a sitegroup called “Team Members”. The desciption for that site is: “Use this group to give people contribute permissions to the SharePoint site: Team”. The last part is a link to the site, as you can see in the screenshot below.

The question how to programmatically set this link in the description. The problem with this is that the Description property is a normal string property, that does not have the HTML value that is displayed above. The trick here is not to use the SPGroup object, but set the property directly on the listitem in a list called “User Information List. This is the list you are looking at when you are viewing People and Groups. Just read the comment in the screenshot above: “There are no items to show in this view of the "User Information List" list.”.
All site groups (and users) are stored in this list, that is available in the rootweb of your site collection. I have extended the code of the previous example, so that it now also sets the description of the SPGroup to have a nice description with a hyperlink to the site it is used for.
// Initialize string customerName = "Contoso"; string accountManager = @"office2007\jsmith"; // Get the site and the account manager for the new customer site SPSite site = new SPSite(string.Format("http://office2007:13145/customers/{0}", customerName)); SPWeb web = site.OpenWeb(); SPUser owner = web.AllUsers[accountManager]; // Get the site group string groupName = string.Empty; SPGroup group = null; string groupAssociations = string.Empty; groupName = string.Format("{0} Customer Team", customerName); group = GetSiteGroup(web, groupName); // Add the group if it does not yet exist if (group == null) { web.SiteGroups.Add( groupName, owner, null, groupName); group = GetSiteGroup(web, groupName); } // If the group was created, set the Visitor reference in the site property bag // and set the description for the site group. if (group != null) { web.Properties["vti_associatevisitorgroup"] = group.ID.ToString(); groupAssociations = string.Format("{0};", group.ID.ToString()); // Get the userlist from the rootweb SPWeb root = site.RootWeb; SPList userInformation = root.Lists["User Information List"]; // Construct and submit the query to find the qroup SPQuery query = new SPQuery(); query.Query = string.Format("<Where><Eq><FieldRef Name='ID'/> <Value Type='Text'>{0}</Value></Eq></Where>", group.ID.ToString()); SPListItemCollection items = userInformation.GetItems(query); // Update the item if it was found. if ((items != null) && (items.Count == 1)) { SPListItem item = items[0]; item["About me"] = string.Format("Use this group to add people to the customer support team for site: <a href=\"{0}\">{1}</a>", web.ServerRelativeUrl, customerName); item.Update(); } }
In the last bit of this code example, a reference to the RootWeb of the site collection is set. This SPWeb has a SPList object called “User Information List”. We can find the correct item in this list using the ID of the SPGroup object we just created. We create a SPQuery object and run that against the list. This returns 1 SPListItem object. The description for the site group is stored in a field called “About me” (The internal name of this field is “Notes”). After updating the item and running the code, we now have set the description as we wanted:

|