Recent posts

Make a SharePoint Web Read Only from code

My current customer is going a very popular road in corporate SharePoint use: Project sites to collect internal business project data and maintain the current status of a project in a formal way. Part of making the use of project sites formal is the use of workflow to start a project, end it or cancel it. So far so good.

Starting and creating stuff is always fun, but how to end or cancel? What does it mean to close a Project web site? Will it be removed? It might contain useful information and even documents that can be of use for future projects. In analogy of the real world, my customer wanted a very common but down to earth solution. Make the site read only. And after some retention time archive it. Sounds easy? Only when you define “read only”. I am using this definition:

- Break permission inheritance for the web site

- Set permissions for existing users and groups that have access to the site to Readonly

- Add the System Admin as a user to the site and give Full Control as permission

- Restore inheritance for all lists and libraries in the website

If this is also your definition of a read only site, then you might find the following code useful. How you want to use it is up to you. For me the code had to become part of a workflow action, but you might put it under a button or a menu item. If you want to you can enhance the code by marking documents as a record or anything that may help eventually archiving the project site after a retention period. Also note that this code assumes you have exception handling around it.

   1: private void MakeWebReadonlyInternal(SPWeb web)
   2:  {
   3:      SPRoleDefinition fullControlPermissionRole = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
   4:      SPRoleDefinition readerPermissionRole = web.RoleDefinitions.GetByType(SPRoleType.Reader);
   5:      if (!web.HasUniqueRoleAssignments)
   6:      {
   7:          web.BreakRoleInheritance(true);
   8:      }
   9:      if (!CheckSytemAdminFullControl(web, fullControlPermissionRole))
  10:      {
  11:          AddSystemAdminFullControl(web, fullControlPermissionRole);
  12:      }
  13:      MakeOtherUsersReader(web, readerPermissionRole);
  14:      RestoreRoleInheritance(web);
  15:  }
  16:  
  17:  private void RestoreRoleInheritance(SPWeb web)
  18:  {
  19:      foreach (SPList list in web.Lists)
  20:      {
  21:          if (list.HasUniqueRoleAssignments)
  22:          {
  23:              list.ResetRoleInheritance();
  24:          }
  25:      }
  26:  }
  27:  
  28:  private void MakeOtherUsersReader(SPWeb web, SPRoleDefinition readerPermissionRole)
  29:  {
  30:      foreach (SPRoleAssignment roleAssignment in web.RoleAssignments)
  31:      {
  32:          if (roleAssignment.Member.ID != web.Site.SystemAccount.ID)
  33:          {
  34:              roleAssignment.RoleDefinitionBindings.RemoveAll();
  35:              roleAssignment.RoleDefinitionBindings.Add(readerPermissionRole);
  36:              roleAssignment.Update();
  37:          }
  38:      }
  39:  }
  40:  
  41:  private void AddSystemAdminFullControl(SPWeb web, SPRoleDefinition fullControlPermissionRole)
  42:  {
  43:      SPRoleAssignment systemAdminRoleAssignment = new SPRoleAssignment(web.Site.SystemAccount);
  44:  
  45:      systemAdminRoleAssignment.RoleDefinitionBindings.Add(fullControlPermissionRole);
  46:      web.RoleAssignments.Add(systemAdminRoleAssignment);
  47:      web.Update();
  48:  }
  49:  
  50:  private bool CheckSytemAdminFullControl(SPWeb web, SPRoleDefinition fullControlPermissionRole)
  51:  {
  52:      foreach (SPRoleAssignment roleAssignment in web.RoleAssignments)
  53:      {
  54:          if (roleAssignment.Member.ID == web.Site.SystemAccount.ID)
  55:          {
  56:              if (!roleAssignment.RoleDefinitionBindings.Contains(fullControlPermissionRole))
  57:              {
  58:                  roleAssignment.RoleDefinitionBindings.Add(fullControlPermissionRole);
  59:                  roleAssignment.Update();
  60:              }
  61:              return true;
  62:          }
  63:      }
  64:      return false;
  65:  }

Published: 13-12-2011 by Wim The | 0 Comments | 0 Links to this post