Tuesday, December 16

Custom User Group in SharePoint

Steps:

1) Go to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

2) Create a new folder named ‘CustomGroup

3) Copy Feature.xml from any other folder in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES

4) Write following code in Feature.xml file
style="font-weight:bold;">
Title="CustomGroup"
Description="This Feature Creates a Custom Group"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Site"
DefaultResourceFile="core"
ReceiverAssembly="CustomGroupFeature, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=859e49442b7d12e1"
ReceiverClass="CustomGroupFeature.FeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/">


Notice the ResourceAssembly and ReceiverClass attributes. These serve as references to the assembly that will handle the FeatureActivated, FeatureDeactivating, FeatureInstalled and FeatureUninstalling events.

5) create the SPFeatureReceiver class referenced above

a) Open Visual Studio
b) New project  Class library
c) Name it as CustomGroupFeature
d) Add reference Microsoft.SharePoint
e) Copy the code given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace CustomGroupFeature
{
class FeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
//get a reference to the site collection where the feature is activated
using (SPSite site = properties.Feature.Parent as SPSite)
{
//get a reference to the root site / web of the site collection
using (SPWeb web = site.RootWeb)
{
//get a reference to a Site Owner
//(there should always be at least 1; position 0 of the
//Administrators UserCollection)
//we'll make this user the owner of the custom group
SPMember siteOwner = web.SiteAdministrators[0];

//prepare the group name
string grpName = "Custom Group";

//check if group exists
bool grpExists = false;

//-----------------------------------------------------
//THIS CODE SHOULD BE MOVED TO ITS OWN HELPER FUNCTION
//OR UTILITIES CLASS. IN FACT MOST OF THIS CODE NEEDS
//TO BE REFACTORED
foreach (SPGroup group in web.SiteGroups)
if (group.Name.ToLower() == grpName.ToLower())
grpExists = true;
//------------------------------------------------------

//add the custom group to the site
//if it doesnt allready exist
if (!grpExists)
{
web.SiteGroups.Add(grpName, siteOwner, null,
"Custom Group that I created because I can!");
web.Update();

//get a reference to the group we just created
SPGroup customGroup = web.SiteGroups[grpName];

//add the group to the quicklaunch
web.Properties["vti_associategroups"] =
web.Properties["vti_associategroups"] + ";"
+ customGroup.ID.ToString();
web.Properties.Update();
}
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
//throw new NotImplementedException();
}

//.....FeatureDeactivating, FeatureInstalled, and FeatureUninstalling methods
}
}

f) Create strong name
g) Build it

6) Add the dll to Assembly

7) Get the publickeytocken

8) Update in Feature.xml

9) Open command prompt

10) Go to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN

11) Install the feature using:

stsadm.exe –o installfeature –filename CustomGroup \feature.xml –force

12) Open SharePoint site

13) Go to Site settingsSite Collection features

14) Now CustomUserGroup is available.

15) Activate this feature.

16) Now if the SharePoint site is opened, then CustomUsergroup is available.

Reference:

No comments: