Monday, 8 August 2011

Deploying Site Templates in the Solution Gallery

Case
I downloaded a couple of site templates from the Solution Gallary of my SharePoint Portal.

I want to put these site templates in a Solution Package so I can easily deploy them in my development enviroments.

I just want to deploy the solution, go to my site collection and greate the subpages I need.

Problem
After putting the file in the correct location (_catalogs/solutions) in my site collection, I noticed I couldn't activate the templates. Activating the templates is necessary to be able to select the template while creating a new site.

Solution
The following solution turned out to work for me:

- Create a new Module in VS2010.
- Add the site templates to your Module.


[Module Name="Templates" List="121" Url="_catalogs/solutions"]
[File Path="Templates\Template_Name.wsp" Url="Template_Name.wsp" Type="GhostableInLibrary"]
[Property Name="ContentType" Value="Solution Gallery" /]
[Property Name="SolutionTitle" Value="Template Name" /]
[Property Name="SolutionDescription" Value="Standard Template" /]
[/File]
[/Module]


* Replace [ and ] signs with less than (<) and greater than (>) signs.


- Add an eventreceiver to the feature and uncomment 'FeatureActivated'


SPSite currentSite = (SPSite)properties.Feature.Parent;

// Get all names of added solutions
List solutionName = new List();
SPUserSolutionCollection solutionsCollection = currentSite.Solutions;

foreach (SPUserSolution solution in currentSite.Solutions)
{
string name = solution.Name;
if (!solutionName.Contains(name))
{
solutionName.Add(name);
}
}

// Add new solutions to the collection of the gallery
SPDocumentLibrary solutionsGallery = (SPDocumentLibrary)currentSite.GetCatalog(SPListTemplateType.SolutionCatalog);
foreach (SPListItem item in solutionsGallery.Items)
{
if (!solutionName.Contains(item.Name))
{
solutionsCollection.Add(item.ID);
}
}

// Properly activate solutions
SPFeatureCollection featureCollection = currentSite.Features;
foreach (SPUserSolution solution in solutionsCollection)
{
Guid solutionId = solution.SolutionId;

SPFeatureDefinitionCollection siteFeatures = currentSite.FeatureDefinitions;

var features = from SPFeatureDefinition f in siteFeatures
where f.SolutionId.Equals(solutionId) && f.Scope == SPFeatureScope.Site
select f;

foreach (SPFeatureDefinition featureDefinition in features)
{
SPFeature foundFeature = featureCollection[featureDefinition.Id];

if (foundFeature == null)
currentSite.Features.Add(featureDefinition.Id, false, SPFeatureDefinitionScope.Site);
}
}

2 comments:

Anonymous said...

Is it possible to activate the solution which is uploaded in the solution gallery without manually activating it? I have to automate the process of uploading the sandbox solution and activate in the required site collections. Uploading solution I can do it with client object model but I have no ideas how to activate it..? Can you give me some light on it?

Jasper Beerens said...

You'll probably need to activate the solution like af feature. These guys can probably help you out: http://www.learningsharepoint.com/2010/07/09/activate-feature-with-client-object-model-sharepoint-2010/

Post a Comment