I'm using Microsoft's Deploying Branding Solutions for SharePoint 2010 Sites using Sandboxed Solutions sample solution to start. I just want to:
- Create a subsite
- Apply custom master page only to the new subsite on FeatureActivated
I'm only able to apply branding to both the root site and the subsite or not at all.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
namespace Branding101.Features.Main {
[Guid("990ff443-6288-4f6e-b22d-298c0881317d")]
public class MainEventReceiver : SPFeatureReceiver {
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
// Creating the subsite below
SPSite mySite = null;
try
{
using (mySite = new SPSite("http://sp2010dev:26681/sites/UniqueSC"))
{
using (SPWeb web = mySite.AllWebs.Add("UniqueSubsite", "Reporting Portal", "Reporting Portal", 1033, "STS#0", true, false))
{
}
}
}
catch (Exception ex)
{
throw new YourCustomException("Error Creating Subsite", e);
}
finally
{
mySite.Dispose();
}
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null) {
SPWeb topLevelSite = siteCollection.RootWeb;
// calculate relative path of site from Web Application root
string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!WebAppRelativePath.EndsWith(@"/")) {
WebAppRelativePath += @"/";
}
foreach (SPWeb site in siteCollection.AllWebs) {
String siteURL = site.Url.ToString();
Int32 iLocation = siteURL.IndexOf("UniqueSubsite");
if (iLocation >= 0)
{
site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/Branding101.master";
//site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/Branding101.master";
site.AlternateCssUrl = WebAppRelativePath + "Style%20Library/Branding101/Styles.css";
site.SiteLogoUrl = WebAppRelativePath + "Style%20Library/Branding101/Images/Logo.gif";
site.UIVersion = 4;
site.UIVersionConfigurationEnabled = false;
site.Update();
}
else
{
//do nothing
}
}
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
SPSite siteCollection = properties.Feature.Parent as SPSite;
if (siteCollection != null) {
SPWeb topLevelSite = siteCollection.RootWeb;
// calculate relative path of site from Web Application root
string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
if (!WebAppRelativePath.EndsWith(@"/")) {
WebAppRelativePath += @"/";
}
foreach (SPWeb site in siteCollection.AllWebs) {
site.MasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
site.CustomMasterUrl = WebAppRelativePath + "_catalogs/masterpage/v4.master";
site.AlternateCssUrl = "";
site.SiteLogoUrl = "";
site.UIVersion = 4;
site.Update();
}
}
}
}
}