SharePoint : Update Access Request Settings email address using SSOM

Introduction

In SharePoint whenever user tries to access a site which he is not authorized to then he gets redirected to Access Denied page. In SharePoint 2010 they have introduced a feature where we can request an access for Site and that access request will get sent to an email address configured at Access Requests and Invitation page.

Solution

Find below code snippet which you can use to update email address at Access Requests and Invitation page.

The below method uses three parameter as shown:

  1. Email address to update : String of email address where you wish to send access request.
  2. web object : SPWeb Object
  3. IsTopSuite : Boolean value to identify if the site is top site. True if site is Top level Site collection else false.
  1. /// <summary>   
  2. /// method to update Access request settings   
  3. /// </summary>   
  4. /// <param name="emailAddress">Core Site Directory object</param>   
  5. /// <param name="rootWeb">root web object</param>   
  6. /// <param name="IsTopSite">boolean field</param>   
  7. private void UpdateAccessRequestSettings(string emailAddress, SPWeb rootWeb, bool IsTopSite)   
  8. {   
  9.    rootWeb.AllowUnsafeUpdates = true;   
  10.    try   
  11.    {   
  12.       if (IsTopSite == true)   
  13.       {   
  14.          if (rootWeb.RequestAccessEnabled == true)   
  15.          {   
  16.             rootWeb.RequestAccessEmail = emailAddress;   
  17.             rootWeb.Update();   
  18.          }   
  19.       }   
  20.       else   
  21.       {   
  22.          if (!siteDirList.InheritSitePermissions)   
  23.          {   
  24.             if (rootWeb.RequestAccessEnabled == true)   
  25.             {   
  26.                rootWeb.RequestAccessEmail = emailAddress;   
  27.                rootWeb.Update();   
  28.             }   
  29.          }   
  30.       }   
  31.    }   
  32.    catch (Exception ex)   
  33.    {   
  34.       //catch exception   
  35.    }   
  36.    finally   
  37.    {   
  38.       rootWeb.AllowUnsafeUpdates = false;   
  39.    }   
  40. }   
This is how you need to call a method :
  1. // For top level site collection  
  2. UpdateAccessRequestSettings ("[email protected]",spweb,True)   
  3.   
  4. // For subsite not inheriting permission from parent site  
  5. UpdateAccessRequestSettings ("[email protected]",spweb,false)  
We can do the same thing with PowerShell as well. To do that please find my another blog at http://www.c-sharpcorner.com/code/226/powershell-script-to-update-access-request-settings-email-a.aspx

Happy SharePointing !!