Sunday, February 24, 2013

Adding Active directory group in SharePoint group

This code will help you to add the Active Directory Group in the Custom SharePoint Group. Consider the situation you need to give the access permission to multiple users on the basis of department wise or any category based which is already created in AD. Now add this group in SharePoint group to simplify the process. This will add the new sharepoint group programmatically and the AD group in to it.

           SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            web.SiteGroups.Add("Champion10", web.CurrentUser, web.CurrentUser, string.Empty);
         
            SPGroup group= web.SiteGroups["Champion10"];
            SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
            SPRoleDefinition roleDefinition = web.RoleDefinitions["Full Control"];
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            web.RoleAssignments.Add(roleAssignment);


            SPUser AdGroup = web.EnsureUser("MurugesanAD");
           //This line will do the trick.You can also ensure by using the  bool isAD=AdGroup.IsDomainGroup

            group.AddUser(g);

       
            web.Update();
            web.AllowUnsafeUpdates = false;
         
            Literal1.Text = "Done";