Tuesday, May 4, 2010

Identify Restrict a Group User

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.Office.Server;

namespace RestrictGroup
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsUserAuthorized("Viewers"))
            {
                Response.Write("true");
            }
            else
            {
                Response.Write("False");
            }
        }
      


        public bool IsUserAuthorized(string groupName) //find user & check whether it belong to specific group
        {
           
            SPSite site = SPContext.Current.Site;

           
            using (SPWeb web = site.OpenWeb())
            {
              
                SPUser currentUser = web.CurrentUser;

             
                SPGroupCollection userGroups = currentUser.Groups;

               
                foreach (SPGroup group in userGroups)
                {
                                   
                    if (group.Name.Equals(groupName))  //Traverse all the group & check mentioned group
                        return true;
                }
            }
            return false;
        }
    }
}

Read more...

Blog Popularty Partners

  ©All Right Reserved.

Back to TOP