a developer’s blog
Random header image... Refresh for more!

Working with Users and Groups in SharePoint 2007

As I am about to show you, one of the easier things to do in SharePoint (programmatically) is that add a user to a group and add a user or group to a site.

I’ll be using the SharePoint web service to do this, so first we need to setup the web reference.  Add a web reference to your project using the URL: http://[sitename]/_vti_bin/usergroup.asmx (of course you’ll substitute your site name).

Then we need to setup the web service object (I named my web reference AdminService):

   1: static AdminService.UserGroup userService = new AdminService.UserGroup();
   2: userService.Credentials = System.Net.CredentialCache.DefaultCredentials;

NextI have an enum, class and method to make it easy to remember and work with the different permission types.

   1: /// <summary>
   2: /// Enumeration representing the various permission levels in SharePoint
   3: /// </summary>
   4: public enum SharePointPermissionType
   5: {
   6:     /// <summary>
   7:     /// Full Control - Has full control.
   8:     /// </summary>
   9:     [Description("Full Control")]
  10:     FullControl, 
  11:     /// <summary>
  12:     /// Design - Can view, add, update, delete, approve, and customize.
  13:     /// </summary>
  14:     Design,
  15:     /// <summary>
  16:     /// Manage Hierarchy - Can create sites and edit pages, list items, and 
  17:     /// documents.
  18:     /// </summary>
  19:     [Description("Manage Hierarchy")]
  20:     ManageHierarchy,
  21:     /// <summary>
  22:     ///  Approve - Can edit and approve pages, list items, and documents. 
  23:     /// </summary>
  24:     Approve, 
  25:     /// <summary>
  26:     /// Contribute - Can view, add, update, and delete.
  27:     /// </summary>
  28:     Contribute, 
  29:     /// <summary>
  30:     /// Read - Can view only.
  31:     /// </summary>
  32:     Read,
  33:     /// <summary>
  34:     /// Restricted Read - Can view pages and documents, but cannot view historical 
  35:     /// versions or review user rights information.
  36:     /// </summary>
  37:     [Description("Restricted Read")]
  38:     RestrictedRead,
  39:     /// <summary>
  40:     /// View Only - Members of this group can view pages, list items, and documents. 
  41:     /// If the document has a server-side file handler available, they can only view 
  42:     /// the document using the server-side file handler.
  43:     /// </summary>
  44:     [Description("View Only")]
  45:     ViewOnly
  46: }

   1: /// <summary>
   2: /// This internal class is used in conjunction with the SharePointPermissionType enum.  
   3: /// It allows for the definition of values as strings.
   4: /// </summary>
   5: class Description : Attribute 
   6: { 
   7:     public string Text; 
   8:     public Description(string text) 
   9:     { 
  10:         Text = text; 
  11:     } 
  12: }

Place this within the class where you’re using the enum:

   1: /// <summary>
   2: /// Used in conjunction with the SharePointPermissionType enum. It returns the value 
   3: /// as a string.
   4: /// </summary>
   5: /// <param name="en">SharePointPermissionType</param>
   6: /// <returns>String representation of a SharePointPermissionType</returns>
   7: private string GetDescription(Enum en)
   8: {
   9:     Type type = en.GetType();
  10:     MemberInfo[] memInfo = type.GetMember(en.ToString());
  11:     if (memInfo != null && memInfo.Length > 0)
  12:     {
  13:         object[] attrs = memInfo[0].GetCustomAttributes(typeof(Description), false);
  14:         if (attrs != null && attrs.Length > 0)
  15:             return ((Description)attrs[0]).Text;
  16:     }
  17:     return en.ToString();
  18: }

 

Finally the meat & potatoes…

Add a user to a SharePoint Group (SPGroup):

   1: /// <summary>
   2: /// Adds a user to a group
   3: /// </summary>
   4: /// <param name="userName">username of user to add</param>
   5: /// <param name="groupName">group name</param>
   6: /// <param name="userEmail">user’s email address</param>
   7: public void AddUserToGroup(string userName, string groupName, string userEmail)
   8: {
   9:     try { userService.AddUserToGroup(groupName, userName.Replace("CSUpmProvider:", ""), userName, userEmail, ""); }
  10:     catch { }
  11: }

 

Add a user to a SharePoint Site (SPSite):

   1: /// <summary>
   2: /// Adds a user to a site
   3: /// </summary>
   4: /// <param name="userName">username of the user to add</param>
   5: /// <param name="siteUrl">url of the site</param>
   6: /// <param name="permission">permission level to grant the group on the site.</param>
   7: public void AddUserToSite(string userName, string siteUrl, SharePointPermissionType permission)
   8: {
   9:     OpenSite();
  10:     SPWeb web = _site.OpenWeb(siteUrl);
  11:     SPMember member = web.ParentWeb.ParentWeb.AllUsers[userName];
  12:     if (member != null)
  13:     {
  14:         SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)member);
  15:         SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
  16:         roleDefBindings.Add(web.RoleDefinitions[GetDescription(permission)]);
  17:         web.RoleAssignments.Add(roleAssignment);
  18:     }
  19:     web.Close();
  20:     web.Dispose();
  21:     CloseSite();
  22: }

 

Add a Group to a Site:

   1: /// <summary>
   2: /// Adds a group to a site
   3: /// </summary>
   4: /// <param name="group">SharePoint group</param>
   5: /// <param name="siteUrl">url of the site.</param>
   6: /// <param name="permission">permission level to grant the group on the site.</param>
   7: public void AddGroupToSite(SPGroup group, string siteUrl, SharePointPermissionType permission)
   8: {
   9:     OpenSite();
  10:     SPWeb web = _site.OpenWeb(siteUrl);
  11:     SPMember member = web.SiteGroups[group.Name];
  12:  
  13:     if (member != null)
  14:     {
  15:         SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)member);
  16:         SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
  17:         roleDefBindings.Add(web.RoleDefinitions[GetDescription(permission)]);
  18:         web.RoleAssignments.Add(roleAssignment);
  19:         web.Update();
  20:     }
  21:     web.Close();
  22:     web.Dispose();
  23:     CloseSite();
  24: }

 

Finally you’ll notice I keep using the methods OpenSite() and CloseSite().  These simply open and close the SPSite _site object:

   1: /// <summary>
   2: /// Opens a SharePointSite
   3: /// </summary>
   4: /// <returns>SPSite object representing the base site collection</returns>
   5: private SPSite OpenSite()
   6: {
   7:     CloseSite();
   8:     _absoluteUrl = "http://myspsite.com";
   9:     _site = new SPSite(_absoluteUrl);
  10:     return _site;
  11: }
  12:  
  13: /// <summary>
  14: /// Close opened site
  15: /// </summary>
  16: private void CloseSite()
  17: {
  18:     if (_site != null)
  19:     {
  20:         _site.Close();
  21:         _site.Dispose();
  22:         _site = null;
  23:     }
  24: }

Tags: , ,

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment