2

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

通过修改方法中的projectItemReference.feature 文件中的元素替换 ID很简单。replacementsDictionaryRunStarted

例如,我有以下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$修改方法是微不足道replacementsDictionary的。IWizard.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>

在这种情况下,我需要为and项设置FeatureIdandSharePointProjectItemId属性。如果我什么都不做,Visual Studio 将自动生成这些 GUID,但它们与我的. 这反过来会导致我的文件中的引用损坏,并且我的模块可能与错误的功能相关联。.feature.spdatareplacementsDictionary.feature

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

4

2 回答 2

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();

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

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);
}
于 2013-01-18T06:46:52.980 回答
0

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

如果您在 replacementDictionary 中有要替换的 Guid,只需检查 .vstemplate 以确保文件替换为真:

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

并正确编辑 .csproj,替换 Guid:

<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>

重要的!替换字典,因为 IDictionaryl 区分大小写!!

于 2013-01-14T22:23:52.913 回答