Recently my client asked me to come up with a solution to display groups related only to the current web site in the Quick Launch Bar. The first thing came in to my mind was SPWeb.Properties["vti_associategroups"]. So, I decided to write a 'Web' scoped feature to modify the list of group names that go in to Quick Launch Bar. When activated, the feature will display the group names belong to the web site and when deactivated group names will be reverted back to its original values.
First, I implemented FeatureActivated method as follows;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SetQLGroups(properties);
}
My strategy here is to loop through all the group names of the sub site that feature is being activated and if the group name contains the site name then it should go in to quick launch bar. Therefor I store the group ID in a List
private void SetQLGroups(SPFeatureReceiverProperties properties)
{
SPWeb spWeb = null;
SPSite spSite = null;
Object oParent = properties.Feature.Parent;
SPFeature spFeature = properties.Feature;
List<String> groupIDs = new List<string>();
.
.
.
if (spWeb != null)
{
groupIDs.Clear();
/* loop through the groups to find groups having web site name as a part of group's name*/
foreach (SPGroup group in spWeb.Groups)
{
if (group.Name.Contains(spWeb.Name))
{
groupIDs.Add(group.ID.ToString());
}
}
/* this will be used for feature deactivation. when the feature
* is deactivated we need to revert the group Ids back
*/
spWeb.Properties.Add("vti_associategroups_original",
spWeb.Properties["vti_associategroups"]);
spWeb.Properties["vti_associategroups"] = String.Join(";",
groupIDs.ToArray());
spWeb.Properties.Update();
spWeb.Update();
}
Now, lets look what happens when the Feature is Deactivated.
Here, I simply replace SPWeb.Properties["vti_associategroups"] with the value of SPWeb.Property["vti_associategroups_original"] that I saved before during the future activation.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
UnSetQLGroups(properties);
}
private void SetQLGroups(SPFeatureReceiverProperties properties)
{
SPWeb spWeb = null;
SPSite spSite = null;
Object oParent = properties.Feature.Parent;
SPFeature spFeature = properties.Feature;
List<String> groupIDs = new List<string>();
.
.
.
if (spWeb != null)
{
spWeb.Properties["vti_associategroups"] = spWeb.Properties["vti_associategroups_original"];
spWeb.Properties.Update();
spWeb.Update();
}
}
The next step is, you have to sign the code and get the version information including the public key token for future usage.
Now, the feature receiver code in place, the next step is to define feature.xml and element.xml.
I have created the feature.xml as follows;
xml version="1.0" encoding="utf-8"?>
<Feature Id="GUID"
Title="QuickLaunchGroupsFeature"
Description=""
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
ReceiverAssembly="QuickLaunchGroups, Version=1.0.0.0, Culture=neutral, PublicKeyToken=25be03d338bc65ac"
ReceiverClass="QuickLaunchGroups.QuickLaunchGroupsFeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="elements.xml"/>
ElementManifests>
Feature>
Next, define the default element.xml file as follows;
xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
Elements>
Now, you can just go ahead install the feature in the system and activate in the web site.Once you activate the feature on a site, quick launch bar will be populated with the group names belong to the site.
To revert the group names to the default, just de-activate the feature.
No comments:
Post a Comment