Search This Blog

Monday, February 8, 2016

How to check the user has permission for current site programmatically in sharepoint 2013?

//Get the Current user using SPContext 
  SPUser user = SPContext.Current.Web.CurrentUser;

  bool isval = false;

  using (SPSite oSite = new SPSite("http://Servername/test"))
  {
    using (SPWeb oWeb = tempsite.OpenWeb())
    {
      isval = IsUserHasAccess(oWeb, user.LoginName);
       if (isval)
       {
         //The Current user has Permission for this web
       }
       else
       {
         //The Current user Doesn't has Permission for this web
       }
    }
  }

  //Method to check the current user has permission for the particular web

  private bool IsUserHasAccess(SPWeb web, string userLoginName)
  {
   bool hasAccess = false;
   try
   {
     web.Site.CatchAccessDeniedException = false;
     hasAccess = web.DoesUserHavePermissions(userLoginName, SPBasePermissions.Open);
   }
   catch
   {
     hasAccess = false;
   }
     return hasAccess;
  }