October 2008 - Posts

SPWeb.Groups and SPWeb.SiteGroups

WSS 3.0 deprecates the site group concept used in WSS 2.0.  As a result manipulating groups and security objects through the object model can be less than obvious.

For Example, a developer may try to add a group to a sub site (SPWeb) using this syntax SPWeb.Groups.Add(Group Name). However, in WSS 3.0 and MOSS 2007 this code will result in the following error:

“You cannot add a group directly to the Groups collection. You can add a group to the SiteGroups collection.”

To resolve this error and create a group use the following object model syntax:  SPWeb.SiteGroups.Add().  This code will create a new group at the site collection level. Groups created at the site collection level are basically the same as “Cross Site Groups” from WSS 2.0. 

Note: All sub sites that are inheriting permissions from a parent site will see the groups added using SPWeb.SiteGroups.Add() when iterating through the SPWeb.Groups collection.

Ref: http://msdn.microsoft.com/en-us/library/ms469194.aspx

Associating a Group with a Site

It is important to realize that groups are never really “Added” to sites (SPWebs) but are instead associated. For example, if you have created a sub site that does not inherit permissions from its parent and you would like to “Add” a project manager group to that site you would use the following code.

   1: // Get a reference a group by name.
   2: SPGroup oGroup = webSite.SiteGroups[groupName];
   3:  
   4: // Get the role definition to assign ex: Full Control
   5: SPRoleDefinition oRole = webSite.RoleDefinitions[roleDefinition];
   6:  
   7: // Create the role assignment object
   8: SPRoleAssignment oRoleAssignment = new SPRoleAssignment(oGroup);
   9:  
  10: // Add the role definition to the role assignemnt. 
  11:  
  12: // Assign the specific permission to the security principal for this role assignemnt.
  13: oRoleAssignment.RoleDefinitionBindings.Add(oRole);
  14:  
  15: // Add the role assignment to the web
  16: webSite.RoleAssignments.Add(oRoleAssignment);
  17:  
  18: webSite.Update();

This code associates a site collection (Cross Site) Group with a SPWeb and assigns a role definition such as “Full Control.” With the code discussed in this post we could create a group, then give that group full control to a sub site.

How do I create a custom list?

   1: Guid listId = webSite.Lists.Add("Sample", "Sample List", SPListTemplateType.GenericList);
   2:               
How to determine if a list already exists within an SPWeb?
   1: public static bool DoesListExist(SPWeb web, String listName)
   2:        {
   3:            foreach (SPList list in web.Lists)
   4:            {
   5:                if (true == list.Title.Equals(listName, StringComparison.OrdinalIgnoreCase))
   6:                    return true;
   7:            }
   8:            return false;
   9:        }

How to determine if a column (field) already exists?

   1: if (!list.Fields.ContainsField("Description"))
   2:                     list.Fields.Add("Description", SPFieldType.Note, false);

How to add a lookup column to a list?

   1: Guid listGuid = webSite.Lists["lookupList"].ID;
   2:                 Guid listId = webSite.Lists.Add("Sample", "Sample List", SPListTemplateType.GenericList);
   3:                 SPList list = webSite.Lists[listId] as SPList;
   4:                 list.Fields.AddLookup("Sample Lookup", listGuid, true);

Shane Carter has founded the Bartlesville SharePoint User Group which is a sister group to the newly founded Bartlesville .NET User Group.  The group's web site is http://sharepoint.bdnug.com/.  I will present tomorrow at the SharePoint group's lunch time meeting.  Details below:

 

Date: October 3rd at 11:30 AM.
We will be meeting at the Information Center (IC) building on 511 S. Keeler in room 625.

Speaker:

Dennis J. Bottjer will be presenting "Tips for Improving SharePoint Performance".
Agenda:
11:30-11:45 Grab your lunch provided by Astra Solutions.
11:45-12:30 Technical Presentation.
12:30-12:45 Door prizes and wrap up.