在IWizard中设置项目项子项?

时间:2012-10-31 07:58:55

标签: visual-studio-2010 visual-studio visual-studio-extensions

我有一个自定义项目模板,我将其添加到Sharepoint项目中。我需要确保我的模块仅与我的功能相关联,即使项目已包含其他功能。

通过修改projectItemReference方法中的replacementsDictionary,可以轻松替换.feature文件中RunStarted个元素中的ID。

例如,我有以下SampleModule_WebParts.feature文件:

<?xml version="1.0" encoding="utf-8"?>
<feature xmlns:dm0="http://schemas.microsoft.com/VisualStudio/2008/DslTools/Core" dslVersion="1.0.0.0" Id="$SampleFeatureID$" activateOnDefault="false" description="Sample Web Part" featureId="$SampleFeatureID$" scope="Site" solutionId="00000000-0000-0000-0000-000000000000" title="Contoso Intranet Sample Module Web Parts" version="" deploymentPath="$SharePoint.Project.FileNameWithoutExtension$_$SharePoint.Feature.FileNameWithoutExtension$" xmlns="http://schemas.microsoft.com/VisualStudio/2008/SharePointTools/FeatureModel">
  <projectItems>
    <projectItemReference itemId="$SampleModuleID$" />
  </projectItems>
</feature>

通过修改$SampleModuleID$方法中的$SampleFeatureID$来取代replacementsDictionaryIWizard.RunStarted是微不足道的。但是,如何修改生成的.csproj文件片段?

<None Include="Features\SampleModule_WebParts\SampleModule_WebParts.feature">
  <FeatureId>{78185D58-6398-4ED2-B0D0-3DC20946FF8F}</FeatureId>
</None>
<Compile Include="SPItems\SampleModule\SampleWebPart\SampleWebPart.cs" />
<Compile Include="SPItems\SampleModule\SampleWebPart\SampleWebPartUserControl.ascx.cs">
  <DependentUpon>SampleWebPartUserControl.ascx</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="SPItems\SampleModule\SampleWebPart\SampleWebPartUserControl.ascx.designer.cs">
  <DependentUpon>SampleWebPartUserControl.ascx.cs</DependentUpon>
</Compile>
<None Include="SPItems\SampleModule\SampleWebPart\SampleWebPart.webpart" />
<None Include="SPItems\SampleModule\SampleWebPart\SharePointProjectItem.spdata">
  <SharePointProjectItemId>{D982D304-E7FB-4E8C-899B-7D4096A55892}</SharePointProjectItemId>
</None>

在这种情况下,我需要为FeatureIdSharePointProjectItemId项设置.feature.spdata属性。如果我什么都不做,Visual Studio会自动生成这些GUID,但它们与我replacementsDictionary中的GUID不匹配。而这反过来导致我的.feature文件中的参考文件被破坏,我的模块可能会与错误的功能相关联。

如何设置这些自定义属性,以便在用户保存时将它们保存到.csproj文件中? IWizard.ProjectItemFinishedGenerating似乎是正确的实现方法,我可以检查ProjectItem参数以确定何时生成.spdata.feature文件,但 what < / em>我应该在那里设置FeatureIdSharePointProjectItemId属性吗?

2 个答案:

答案 0 :(得分:1)

解决方案是将Project引用转换为ISharePointProject并进行调用 ISharepointProject.Synchronize()。之后,我可以遍历SharePoint项目的对象模型。

var sp = project.DTE as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
var projectService = new ServiceProvider(sp).GetService(typeof(ISharePointProjectService)) as ISharePointProjectService;
var sharepointProject = projectService.Convert<Project, ISharePointProject>(project);
sharepointProject.Synchronize();

ProjectItemsFeatures属性返回的集合中找到我的模块和我的功能后,我可以简单地将模块与功能相关联:

sampleFeature.ProjectItems.Add(sampleModule);

因为我可以通过编程方式修复引用,所以我可以将旧模块GUID保留在.feature文件中,并通过修改sampleFeature.Model.ProjectItems集合来清除无效关联。

var invalidSampleModuleAssociation = (from projectItem in sampleFeature.Model.ProjectItems
                                      where projectItem.ItemId == originalSampleModuleID
                                      select projectItem).First();
sampleFeature.Model.ProjectItems.Remove(invalidSampleModuleAssociation);

最后,我需要从项目可能具有的任何其他功能中删除我的模块,因为Visual Studio会自动将新模块与具有适当范围的第一个功能相关联。

var unneededAssociations = (from otherFeature in sharepointProject.Features
                            where otherFeature.Name != sampleFeature.Name
                            from projectItem in otherFeature.ProjectItems
                            where projectItem.Name == sampleModule.Name
                            select new
                            {
                                Feature = otherFeature,
                                ModuleToRemove = projectItem
                            }).ToArray();
foreach (var moduleAssociation in unneededAssociations)
{
    moduleAssociation.Feature.ProjectItems.Remove(moduleAssociation.ModuleToRemove);
}

答案 1 :(得分:0)

AFAIK,您也可以在任何文件中进行替换,包括.csproj(文件名和内容)

如果您想在replacementDictionary中替换Guids,只需检查.vstemplate以确保文件替换为真:

<Project
    File="MyProject.proj"
    TargetFileName="$safeprojectname$.csproj"
    ReplaceParameters="true">
</Project>

正确编辑.csproj,替换Guids:

<None Include="Features\SampleModule_WebParts\SampleModule_WebParts.feature">
  <FeatureId>{$SampleFeatureID$}</FeatureId>
</None>
<Compile Include="SPItems\SampleModule\SampleWebPart\SampleWebPart.cs" />
<Compile Include="SPItems\SampleModule\SampleWebPart\SampleWebPartUserControl.ascx.cs">
  <DependentUpon>SampleWebPartUserControl.ascx</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="SPItems\SampleModule\SampleWebPart\SampleWebPartUserControl.ascx.designer.cs">
  <DependentUpon>SampleWebPartUserControl.ascx.cs</DependentUpon>
</Compile>
<None Include="SPItems\SampleModule\SampleWebPart\SampleWebPart.webpart" />
<None Include="SPItems\SampleModule\SampleWebPart\SharePointProjectItem.spdata">
  <SharePointProjectItemId>{$SampleModuleID$}</SharePointProjectItemId>
</None>

重要! replacementDictionary,因为IDictionaryl区分大小写!!