Update "Request Access Email" of all the sites in a Farm in a single shot ! (SharePoint server 2003)

Recently I got a customer request and their requirement was to update the “Request Access email” of all the sites in a SharePoint V2 farm in a single shot using SharePoint object model. Below image is a screen shot of the Manage Request Access setting page.

clip_image002

And here I am not going to changing the whole email address; I am only updating the Domain name of the Email. Once this code executes, it will create a text file (bin/debug) which will give you the report of all the sites which were get updated.

 

code for .NET console based application. 

    1: using System;
    2: using System.IO;
    3: using System.Collections.Generic;
    4: using System.Text;
    5: using Microsoft.SharePoint;
    6: using Microsoft.SharePoint.Administration;
    7: using Microsoft.SharePoint.Utilities; 
    8:  
    9: namespace UpdateAllEmails
   10: {
   11:     class Program
   12:     {
   13:         static void Main(string[] args)
   14:         {
   15:             SPGlobalAdmin spGlobalAdmin = new SPGlobalAdmin();           
   16:  
   17:             TextWriter reportfile = new StreamWriter("EmailUpdateReport.txt");
   18:  
   19:             foreach (SPVirtualServer oWebApp in spGlobalAdmin.VirtualServers)
   20:             {
   21:                 if (oWebApp.State == SPVirtualServerState.Ready)
   22:                 {
   23:                     SPSiteCollection oSites = oWebApp.Sites; // Retrieving all sites for a particular web application 
   24:  
   25:                     if (oSites != null && oSites.Count > 0)
   26:                     {
   27:                         foreach (SPSite oSite in oSites)
   28:                         {
   29:                            SPWebCollection oWebCollection = oSite.AllWebs; // Retrieving all websites for a particular site collection
   30:  
   31:                             foreach (SPWeb oWeb in oWebCollection)
   32:                             {                                
   33:                                 SPPermissionCollection oPermission = oWeb.Permissions;
   34:                                 // sites will be getting the RequestAccessEmail only if it has created with unique permission, so here I am checking whether this site has unique permission or not.
   35:  
   36:                                 if (oWeb.HasUniquePerm)
   37:                                 {
   38:                                     string strOldEmail = oPermission.RequestAccessEmail;
   39:                                     string strNewEmail = string.Empty;
   40:                                     if (strOldEmail != string.Empty)
   41:                                     {
   42:                                         string[] strSplittedEmail = strOldEmail.Split('@');
   43:                                         strNewEmail = strSplittedEmail[0].ToString() + "@test.com";
   44:                                         oPermission.RequestAccessEmail = strNewEmail;
   45:                                     }
   46:  
   47:                                     reportfile.WriteLine("WebAppliction : " + oWebApp.Url + " >> Site Collection :" + oSite.Url + " >> Web Site :" + oWeb.Url + " >> Old Request access email :" + strOldEmail + " >> new email ID is :" + strNewEmail);
   48:                                     oWeb.Update();
   49:                                 }                               
   50:  
   51:                             }
   52:  
   53:                         }
   54:  
   55:                     }
   56:  
   57:                 }
   58:  
   59:             }
   60:  
   61:             reportfile.Close();
   62:  
   63:         }
   64:  
   65:     }
   66:  
   67: }