This is the follow up article on the SPPortalConnection tool that I recently introduced.
So taking a look at how I've broken the solution up, you'll see that firstly I've got the PortalConnection project which is a class library project, the second project 'SPPortalConnectionSolution' has all the bits to make a Sharepoint solution by running the CreateSolution.cmd batch file.
So starting with the PortalConnection project, the first file ManagePortalConnectionPage.cs is the code behind page for the central admin interface. The most interesting method in this page is the logic to apply the portal connection:
private void ProcessStaple(SPWebApplication webApp)
{
PortalConfig portalConfig = null;
Guid stapleGuid = new Guid(SPPortalConnectionFeatureReciever.STAPLE_GUID);
SPFeature portalConnectionStaple = webApp.Features[stapleGuid];
try
{
//set property
if (webApp.Properties.ContainsKey(SPPortalConnectionFeatureReciever.PROP_KEY))
{
portalConfig = (PortalConfig)webApp.Farm.GetObject(
new Guid(webApp.Properties[SPPortalConnectionFeatureReciever.PROP_KEY].ToString()));
}
else
{
Guid guid = Guid.NewGuid();
webApp.Properties.Add(SPPortalConnectionFeatureReciever.PROP_KEY, guid);
portalConfig = new PortalConfig(webApp.DisplayName, webApp.Farm, guid);
}
portalConfig.PortalName = txtPortalName.Text.Trim();
portalConfig.PortalUrl = txtPortalUrl.Text.Trim();
portalConfig.Update();
portalConfig.Provision();
//activate feature
if (portalConnectionStaple == null)
webApp.Features.Add(stapleGuid);
webApp.Update();
BindData(webApp);
}
catch (Exception ex)
{
throw ex;
}
}
The second file is the SPPortalConnectionFeatureReciever.cs, this class derives from SPFeature and is called each time a new site is created, this is the feature staple.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
using (SPWeb curWeb = (SPWeb)properties.Feature.Parent)
{
SPSite curSite = curWeb.Site;
if (curSite.WebApplication.Properties.ContainsKey(PROP_KEY))
{
ApplyPortalConnectionSettings(curWeb, curSite);
}
}
}
The Feature folder SPPortalConnectionStaple contains the feature and manifest definitions.
The Elements.xml file declares the feature staple, notice the use of Global for the TemplateName (not GLOBAL#1):
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation Id="7D174E8A-E68E-4a9b-9462-1A65301F7317" TemplateName="GLOBAL"/>
</Elements>
So feel free to download the source code and have a play.