Responsive Ads Here

Friday, October 17, 2014

RequestAccessEnabled programatically using the SharePoint Object Model

If you don't have an access an particular List or any other object SharePoint redirects to the page, where we can request to access for that particular object. An email will sent to the administrator.

Generally the primary site collection administrator mail will be set to handle this access requests. we can change this email address in site settings.

The navigation is site actions --> site settings --> advanced permissions --> settings --> access.

But the problem is when we are creating site collection dynamically the email field will not been set. so we need to set an email either from UI or using object model.

In UI when we are trying to set  an email we need to check the check the check box enable access requests. Then only email text box will be enabled.

When we are doing the same using object model web.RequestAccessEnabled property it has only get property.so we can't set the value as checked for this property.

I goggled for how to set the email address using object model finally find out the tricky way top set the email. This is simply solved by using the some what similar property called RequestAccessEmail. So, if you do web.RequestAccessEmail="sampleEmail"; you are automatically enabling the request access for this site.

Go through the below links for clear understanding.
http://zimmergren.net/technical/tip-requestaccessenabled-programatically-using-the-sharepoint-object-model
http://sharepointrenancr.blogspot.in/2014/02/spwebrequestaccessenable-tip.html 

Code Snippet:--

/// 
/// sets site owner email id as access request email for the site
/// 
/// SPSite object for Sharepoint Site
private void EnableAccessRequests(SPSite site)
{
  try
  {
     using (SPWeb web = site.OpenWeb())
     {
          web.AllowUnsafeUpdates = true;
          web.RequestAccessEmail = site.Owner.Email;
          web.Update();                  
          web.AllowUnsafeUpdates = false;
      }
   }
   catch (Exception ex)
   {
       LogManager.LogError(ex, TraceLogEventId.EventInFeatureActivation, false);
   }

}

No comments:

Post a Comment