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
 

Silverlight Cookbook: Looking for a great UI design

Why? Because even though think I have a reasonable idea of when a user interface is consistent and user friendly, I suck at the raw design skills. Just check out the current ‘design’ if you don’t believe me.

Read more…


Published: 04-11-2011 by Dennis Doomen | 0 Comments | 0 Links to this post
 

In Retrospect: About Bugs

This is the third of several posts in which I’d like to share some of the things we learned throughout more than 14 sprints of Agile development using Scrum. Some of them might appear as open doors, but I wish I knew or thought about those before I started that project. Just by looking back at the mistakes a team of 9 developers and one tester made in a period of 12 months, they apparently aren’t that obvious. So after having discussed the way we handle the sprint planning meeting elaborately, let’s briefly talk about bugs.

Read more…


Published: 03-11-2011 by Dennis Doomen | 0 Comments | 0 Links to this post
 

In Retrospect: About Requirements Management

This is the first of several posts in which I’d like to share some of the things we decided throughout 14 sprint retrospective. Some of them might appear as open doors, but I wish I knew or thought about those before I started that project. Just by looking back at the mistakes a team of 9 developers and one tester in a period of 12 months made, they apparently aren’t that obvious.

Read more


Published: 31-10-2011 by Dennis Doomen | 0 Comments | 0 Links to this post
 

Fluent Assertions is finally gaining some momentum

Indeed it is, in particular within the part of the .NET community that believes test-first development is non-negotiable. We receive more and more suggestions, contributions and questions, and we’ve started to notice some blog posts here and there.

It’s not that it is being downloaded thousands of times per month, but since its first release in February 2010 it has been downloaded 1738 times through CodePlex. The biggest increase was caused by NuGet though. Since we’ve uploaded our first NuGet package in January this year, it counted 2863 downloads. That’s more than enough to make us happy.

Read more


Published: 30-10-2011 by Dennis Doomen | 0 Comments | 0 Links to this post
 

Working with DateTime.Now in Windows Azure apps

For most of us displaying the current date and time is summed up in the following statement: DateTime.Now;

No big deal, but what happens if the time that is displayed is of by a hour? Well most of us would check to see what the machines timezone is and adjust the timezone of the machine to the correct timezone.

Read more…


Published: 26-10-2011 by Hans ter Wal | 0 Comments | 0 Links to this post
 

Sending email from a windows azure application using an Office 365 mailbox

For one of my projects at work I’m trying to figure out what the best way of sending email from a Windows Azure application is.

Read more…


Published: 25-10-2011 by Hans ter Wal | 0 Comments | 0 Links to this post
 

Windows Azure Tools: Unable to connect to dfService.

Installed the azure 1.5 SDK yesterday. When I tried to start my simple web role on my development fabric it was taking forever and would eventually end with the message: Windows Azure Tools: Unable to connect to dfService.

Read more…


Published: 05-10-2011 by Hans ter Wal | 0 Comments | 0 Links to this post
 

BizTalk Server 2010 unleashed book

Back in the days when I was working with BizTalk 2004 I always had the book
‘Microsoft BizTalk Server 2004 Unleashed‘ close by on my desk.
Later on, when BizTalk 2006 was released the same was the case for the book ‘Professional BizTalk Server 2006‘.
 

Published: 22-09-2011 by Randal van Splunteren | 0 Comments | 0 Links to this post
 

Why I created Fluent Assertions in the first place

A few weeks ago I read The value of open-source is the vision not the source code and that made me think about my own reasons for starting Fluent Assertions, now more than a year ago. In the light of that article, lets briefly go over my own goals for Fluent Assertions.

Read more…


Published: 29-07-2011 by Dennis Doomen | 0 Comments | 0 Links to this post
 
 Next >>