2

我有一個自定義項目模板,我將其添加到Sharepoint項目中。我需要確保我的模塊僅與我的功能相關聯,即使項目已包含其他功能。在IWizard中設置項目子屬性?

通過修改RunStarted方法中的replacementsDictionary,替換.feature文件中的projectItemReference元素中的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> 

通過在IWizard.RunStarted方法修改所述replacementsDictionary更換$SampleModuleID$$SampleFeatureID$是微不足道的。但我怎樣才能修改生成的.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> 

在這種情況下,我需要爲.feature.spdata項目設置FeatureIdSharePointProjectItemId性能。如果我什麼也沒做,Visual Studio會自動生成這些GUID,但它們不會匹配我在我的replacementsDictionary中的內容。而這反過來又導致我的.feature文件中的參考文件被破壞,我的模塊可能與錯誤的功能相關聯。

如何設置這些自定義屬性,使它們持續到當用戶將其保存在.csproj文件? IWizard.ProjectItemFinishedGenerating似乎是正確的方法來實現,我可以檢查ProjectItem參數弄清楚的時候已經產生了.spdata.feature文件,但我應該做的有設置FeatureIdSharePointProjectItemId屬性?

+0

項目項 - >屬性 - >按名稱查找 - >設定值(我幹了TreatWarningAsErrors屬性。) – alxx

回答

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

因爲我可以通過編程修復引用,我可以留在.feature文件GUID舊模塊並通過修改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); 
} 
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> 

重要! replacementDictionary,因爲IDictionaryl區分大小寫!

+0

只能修改.csproj的,如果你還創建包括自定義項目模板。 csproj文件。我的示例中的模板是可以添加到任何SharePoint項目的項目模板,因此我無法使用replacementsDictionary來修改.csproj文件。 – Hirvox

相關問題