0

我已经激活了一个包含网站模板的 WSP 文件。这可行,我可以在解决方案库中看到解决方案。当我尝试基于该模板创建网站时,它没有显示出来。但它显示“状态:已激活”。

然后我尝试停用它并再次手动激活它。突然间,出现了一个新模板,该模板的名称为我的模板名称后加了一个“2”。

那么这里到底发生了什么?激活我的解决方案的代码是:

System.IO.MemoryStream input = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(rootDirectory + "\\Templates\\Project.wsp"), true);

SPDocumentLibrary solutionGallery = (SPDocumentLibrary)web.Site.GetCatalog(SPListTemplateType.SolutionCatalog);

try
{
 SPFile solutionFile = solutionGallery.RootFolder.Files.Add("Project.wsp", input);
 SPUserSolution newUserSolution = web.Site.Solutions.Add(solutionFile.Item.ID);
}
catch { ... }
4

1 回答 1

0

好的,我自己找到了答案,并想在这里分享。解决方法是,不提供目录中的解决方案,还要启用功能!它是这样工作的:

System.IO.MemoryStream input = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(rootDirectory + "\\Templates\\Project.wsp"), true);

SPDocumentLibrary solutionGallery = (SPDocumentLibrary)web.Site.GetCatalog(SPListTemplateType.SolutionCatalog);

try
{
 SPFile solutionFile = solutionGallery.RootFolder.Files.Add("Project.wsp", input);
 SPUserSolution newUserSolution = web.Site.Solutions.Add(solutionFile.Item.ID);

 Guid solutionId = newUserSolution.SolutionId;

 SPFeatureDefinitionCollection siteFeatures = web.Site.FeatureDefinitions;
 var features = from SPFeatureDefinition f
             in siteFeatures
             where f.SolutionId.Equals(solutionId) && f.Scope == SPFeatureScope.Site
             select f;
 foreach (SPFeatureDefinition feature in features)
 {
  try
  {
   web.Site.Features.Add(feature.Id, false, SPFeatureDefinitionScope.Site);
  }
  catch { }
 }

 SPWebTemplateCollection webTemplates = web.Site.RootWeb.GetAvailableWebTemplates(1033);
 SPWebTemplate webTemplate = (from SPWebTemplate t
                           in webTemplates
                           where t.Title == "Projekt"
                           select t).FirstOrDefault();
 if (webTemplate != null)
 {
  try
  {
   web.Site.RootWeb.ApplyWebTemplate(webTemplate.Name);
  }
  catch { }
 }
}
catch { ... }
于 2012-12-18T15:09:49.990 回答