.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Programmatically Create SharePoint Group Using Feature

Posted By:Ravishankar       Posted Date: May 26, 2011    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

This article will demonstrate how to create custom group in sharepoint2010 on feature activation.
 

Hi All,

Through this article I am just going to demonstrate how to create a feature for creating group.

Below are the steps:

Step 1. Create a new sharepoint solution using VS2010.Set you feature scope as "Web".

step 2. Right Click on feature,Add FeatureEventreciever.cs.

Step 3: Add the below code in FeatureEventreciever.cs class

For Creating group:

/// 
/// Create group 
/// 
public static void CreateSubSiteGroup(SPWeb web, string groupName, string PermissionLevel,string groupDescription)
{
    SPUserCollection users = web.AllUsers;
    SPUser owner = web.SiteAdministrators[0];
    SPMember member = web.SiteAdministrators[0];
    SPGroupCollection groups = web.SiteGroups;
    groups.Add(groupName, member, owner, groupDescription);
    SPGroup newSPGroup = groups[groupName];
    SPRoleDefinition role = web.RoleDefinitions[PermissionLevel];
    SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup);
    roleAssignment.RoleDefinitionBindings.Add(role);
    web.RoleAssignments.Add(roleAssignment);
    web.Update();

}

For Deleting Group

/// 
/// Delete group for subsite
/// 
public static void DeleteSubSiteGroup(SPWeb web, string groupName)
{
    SPGroupCollection groups = web.SiteGroups;
    groups.Remove(groupName);
    web.Update();

}

Step 4: Add the below code

string groupName="Custom Group";

/// 
/// Method to Attach Event receiver on feature activation.
/// 
/// SPFeatureReceiverProperties Properties
/// 
public static void FeatureActivated(SPFeatureReceiverProperties properties)
{
              string groupDescription="My Custom Group";
   try
    {
        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
             CreateSubSiteGroup(web, groupName, premissionLevel,groupDescription);
                           }
      
        
    }
    catch (Exception ex)
    {
       ex.Message;
    }
}

/// 
/// Method to Remove Event receiver on feature deactivation.
/// 
/// SPFeatureReceiverProperties Properties
public static void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
       try
    {
                        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
           
                DeleteSubSiteGroup(web, groupName);
                           }
    }
    catch (Exception ex)
    {
        ex.Message;
    }
}

Step 5: That's it.Deploy this in your server.

Now its ready. Please let me know if you have any doubt or need clarification.


 Subscribe to Articles

     

Further Readings:

Responses
Author: Prem Shrestha         Company URL: http://www.dotnetspark.com
Posted Date: May 16, 2013

premissionLevel doesn't exist.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend