SharePoint 專案是一個或多個 SharePoint 專案項目的容器。 您可以自行建立專案項目類型再使其與專案範本產生關聯,藉此擴充 Visual Studio 2010 中的 SharePoint 專案系統。 在本逐步解說中,您將建立定義用於建立網站欄的專案項目類型,然後建立可用來建立包含網站欄專案項目之新專案的專案範本。
本逐步解說將示範下列工作:
建立為網站欄定義新的 SharePoint 專案項目類型的 Visual Studio 擴充功能。 此專案項目類型包含會出現 [屬性] 視窗中的簡單自訂屬性。
為專案項目建立 Visual Studio 專案範本。
建置 Visual Studio 擴充功能 (VSIX) 套件以部署專案範本和擴充功能組件。
偵錯和測試專案項目。
這是獨立的逐步解說。 完成本逐步解說之後,您可以將精靈加入至專案範本來增強專案項目。 如需詳細資訊,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)。
注意事項 |
|---|
您可以從下列位置取得本逐步解說所含之完成的專案、程式碼和其他檔案:https://go.microsoft.com/fwlink/?LinkId=191369 (英文)。 |
必要條件
開發電腦上需要下列元件才能完成此逐步解說:
支援的 Microsoft Windows、SharePoint 和 Visual Studio 版本。 如需詳細資訊,請參閱開發 SharePoint 方案的要求。
Visual Studio 2010 SDK。 這個逐步解說使用 SDK 中的 VSIX 專案範本建立 VSIX 套件,以部署專案項目。 如需詳細資訊,請參閱擴充 Visual Studio 中的 SharePoint 工具。
了解下列概念有助於完成此逐步解說 (但非必要):
SharePoint 中的網站欄。 如需詳細資訊,請參閱欄 (英文)。
Visual Studio 中的專案範本。 如需詳細資訊,請參閱 Visual Studio 範本簡介。
建立專案
若要完成這個逐步解說,您必須建立三個專案:
VSIX 專案。 這個專案會建立 VSIX 套件,以部署網站欄專案項目和專案範本。
專案範本專案。 這個專案會建立可用來建立包含網站欄專案項目之新 SharePoint 專案的專案範本。
類別庫專案。 這個實作 Visual Studio 擴充功能的專案會定義網站欄專案項目的行為。
從建立這些專案開始進行此逐步解說。
若要建立 VSIX 專案
啟動 Visual Studio。
在 [檔案] 功能表上,指向 [新增],然後按一下 [專案]。
在 [新增專案] 對話方塊頂端的下拉式方塊中,確定已選取 [.NET Framework 4]。
在 [新增專案] 對話方塊中,展開 [Visual C#] 或 [Visual Basic] 節點,然後按一下 [擴充性] 節點。
注意事項[擴充性] 節點只有在安裝 Visual Studio 2010 SDK 時才可使用。 如需詳細資訊,請參閱本主題稍早討論的<必要條件>一節。
按一下 [VSIX 專案] 範本。
在 [名稱] 方塊中,輸入 SiteColumnProjectItem。
按一下 [確定]。
Visual Studio 會將 SiteColumnProjectItem 專案加入至 [方案總管]。
若要建立專案範本專案
在 [方案總管] 中,以滑鼠右鍵按一下方案節點,指向 [加入],然後按一下 [新增專案]。
注意事項在 Visual Basic 專案中,方案節點只有在已選取選項對話方塊、專案和方案、一般中的 [永遠顯示方案] 核取方塊時,才會出現在 [方案總管] 中。
在 [加入新的專案] 對話方塊頂端的下拉式方塊中,確定已選取 [.NET Framework 4]。
在 [加入新的專案] 對話方塊中展開 [Visual C#] 或 [Visual Basic] 節點,然後按一下 [擴充性] 節點。
在專案範本清單中,選取 [C# 專案範本] 或 [Visual Basic 專案範本]。
在 [名稱] 方塊中,輸入 SiteColumnProjectTemplate。
按一下 [確定]。
Visual Studio 隨即將 [SiteColumnProjectTemplate] 專案加入至方案。
從專案刪除 Class1 程式碼檔。
如果您建立了 Visual Basic 專案,請一併從該專案刪除下列檔案:
MyApplication.Designer.vb
MyApplication.myapp
Resources.Designer.vb
Resources.resx
Settings.Designer.vb
Settings.settings
若要建立擴充功能專案
在 [方案總管] 中,以滑鼠右鍵按一下方案節點,指向 [加入],然後按一下 [新增專案]。
在 [加入新的專案] 對話方塊頂端的下拉式方塊中,確定已選取 [.NET Framework 4]。
在 [加入新的專案] 對話方塊中展開 [Visual C#] 或 [Visual Basic] 節點,然後按一下 [Windows]。
選取 [類別庫] 專案範本。
在 [名稱] 方塊中,輸入 ProjectItemTypeDefinition。
按一下 [確定]。
Visual Studio 隨即將 [ProjectItemTypeDefinition] 專案加入至方案,然後開啟預設的 Class1 程式碼檔。
從專案刪除 Class1 程式碼檔。
設定擴充功能專案
加入程式碼檔案和組件參考,以設定擴充功能專案。
若要設定專案
在 ProjectItemTypeDefinition 專案中,加入名為 SiteColumnProjectItemTypeProvider 的新程式碼檔。
在 [專案] 功能表上,按一下 [加入參考]。
在 [.NET] 索引標籤上,按住 CTRL,然後按一下下列組件,再按一下 [確定]:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
定義新的 SharePoint 專案項目類型
建立實作 ISharePointProjectItemTypeProvider 介面的類別,以定義新專案項目類型的行為。 在您要定義新的專案項目類型時實作此介面。
若要定義新的 SharePoint 專案項目類型
在 ProjectItemTypeDefinition 專案中,開啟 SiteColumnProjectItemTypeProvider 程式碼檔。
將這個檔案中的程式碼取代為下列程式碼,然後儲存檔案。
Imports System Imports System.Diagnostics Imports System.ComponentModel Imports System.ComponentModel.Composition Imports Microsoft.VisualStudio.SharePoint Namespace Contoso.SharePointProjectItems.SiteColumn ' Export attribute: Enables Visual Studio to discover and load this extension. ' SharePointProjectItemType attribute: Specifies the ID for the new project item type. This string must match the value of the ' Type attribute of the ProjectItem element in the .spdata file for the project item. ' SiteColumnProjectItemTypeProvider class: Defines a new type of project item. <Export(GetType(ISharePointProjectItemTypeProvider))> _ <SharePointProjectItemType("Contoso.SiteColumn")> _ Partial Friend Class SiteColumnProjectItemTypeProvider Implements ISharePointProjectItemTypeProvider Private WithEvents projectItemTypeDefinition As ISharePointProjectItemTypeDefinition ' Configures the behavior of the project item type. Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _ Implements ISharePointProjectItemTypeProvider.InitializeType projectItemTypeDefinition.SupportedDeploymentScopes = _ SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All Me.projectItemTypeDefinition = projectItemTypeDefinition End Sub ' Creates a custom property for this project item. Private Sub ProjectItemPropertiesRequested(ByVal Sender As Object, ByVal e As SharePointProjectItemPropertiesRequestedEventArgs) _ Handles projectItemTypeDefinition.ProjectItemPropertiesRequested Dim properties As SiteColumnProperties = Nothing ' If the properties object already exists, get it from the project item's annotations. If False = e.ProjectItem.Annotations.TryGetValue(properties) Then ' Otherwise, create a new properties object and add it to the annotations. properties = New SiteColumnProperties(e.ProjectItem) e.ProjectItem.Annotations.Add(properties) End If e.PropertySources.Add(properties) End Sub End Class Friend Class SiteColumnProperties Private projectItem As ISharePointProjectItem Private Const TestPropertyId As String = "Contoso.SiteColumnProperty" Private Const PropertyDefaultValue As String = "Default custom property value." Friend Sub New(ByVal projectItem As ISharePointProjectItem) Me.projectItem = projectItem End Sub 'The property gets or sets a simple string value. <DisplayName("Example Property")> _ <DescriptionAttribute("This is an example property for site column project items.")> _ <DefaultValue(PropertyDefaultValue)> _ Public Property ExampleProperty As String Get Dim propertyValue As String = Nothing ' Get the current property value if it already exists; otherwise, return a default value. If False = projectItem.ExtensionData.TryGetValue(TestPropertyId, propertyValue) Then propertyValue = PropertyDefaultValue End If Return propertyValue End Get Set(ByVal value As String) If value <> PropertyDefaultValue Then ' Store the property value in the ExtensionData property of the project item. ' Data in the ExtensionData property persists when the project is closed. projectItem.ExtensionData(TestPropertyId) = value Else ' Do not save the default value. projectItem.ExtensionData.Remove(TestPropertyId) End If End Set End Property End Class End Namespaceusing System; using System.Diagnostics; using System.ComponentModel; using System.ComponentModel.Composition; using Microsoft.VisualStudio.SharePoint; namespace Contoso.SharePointProjectItems.SiteColumn { // Enables Visual Studio to discover and load this extension. [Export(typeof(ISharePointProjectItemTypeProvider))] // Specifies the ID for the new project item type. This string must match the value of the // Type attribute of the ProjectItem element in the .spdata file for the project item. [SharePointProjectItemType("Contoso.SiteColumn")] // Defines a new type of project item. internal class SiteColumnProjectItemTypeProvider : ISharePointProjectItemTypeProvider { // Implements IProjectItemTypeProvider.InitializeType. Configures the behavior of the project item type. public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition) { projectItemTypeDefinition.SupportedDeploymentScopes = SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web; projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All; // Handle event to create a custom property for this project item. projectItemTypeDefinition.ProjectItemPropertiesRequested += projectItemTypeDefinition_ProjectItemPropertiesRequested; } // Creates a custom property for this project item. void projectItemTypeDefinition_ProjectItemPropertiesRequested(object sender, SharePointProjectItemPropertiesRequestedEventArgs e) { SiteColumnProperties properties = null; // If the Properties object already exists, get it from the project item's annotations. if (!e.ProjectItem.Annotations.TryGetValue(out properties)) { // Otherwise, create a new Properties object and add it to the annotations. properties = new SiteColumnProperties(e.ProjectItem); e.ProjectItem.Annotations.Add(properties); } e.PropertySources.Add(properties); } } internal class SiteColumnProperties { // This class defines the property behavior. private ISharePointProjectItem projectItem; private const string propertyId = "Contoso.SiteColumnProperty"; private const string propertyDefaultValue = "Default custom property value."; internal SiteColumnProperties(ISharePointProjectItem projectItem) { this.projectItem = projectItem; } // The property gets or sets a simple string value. [DisplayName("Example Property")] [DescriptionAttribute("This is an example property for site column project items.")] [DefaultValue(propertyDefaultValue)] public string ExampleProperty { get { string propertyValue; // Get the current property value if it already exists; otherwise, return a default value. if (!projectItem.ExtensionData.TryGetValue(propertyId, out propertyValue)) { propertyValue = propertyDefaultValue; } return propertyValue; } set { if (value != propertyDefaultValue) { // Store the property value in the ExtensionData property of the project item. // Data in the ExtensionData property persists when the project is closed. projectItem.ExtensionData[propertyId] = value; } else { // Do not save the default value. projectItem.ExtensionData.Remove(propertyId); } } } } }
建立 Visual Studio 專案範本
若要讓開發人員建立包含網站欄專案項目的新 SharePoint 專案,請建立專案範本。 SharePoint 專案範本包含 Visual Studio 中所有專案所需的檔案,例如 .csproj 或 .vbproj 和 .vstemplate 檔案及 SharePoint 專案特定的檔案。 如需詳細資訊,請參閱為 SharePoint 專案項目建立項目範本和專案範本。
在此程序中,建立空的 SharePoint 專案以產生 SharePoint 專案特定的檔案,然後將這些檔案加入至 SiteColumnProjectTemplate 專案,讓它們可包含在從這個專案產生的範本中。 也一併設定 SiteColumnProjectTemplate 專案檔以指定專案範本出現在 [加入新的專案] 對話方塊中的位置。
若要為專案範本建立檔案
以系統管理員權限啟動 Visual Studio 的第二個執行個體。
建立新的空 SharePoint 專案,並命名為 BaseSharePointProject。 在 [SharePoint 自訂精靈] 中,請勿選取 [部署為陣列方案] 選項。
將 [空元素] 項目加入至專案。 將該項目命名為 Field1。
儲存專案,然後關閉第二個 Visual Studio 執行個體。
在開啟 SiteColumnProjectItem 專案的 Visual Studio 執行個體中,以滑鼠右鍵按一下 [方案總管] 中的 [SiteColumnProjectTemplate] 節點。
按一下 [加入] 功能表上的 [現有項目]。
在 [加入現有項目] 對話方塊中,按一下副檔名的下拉式清單,然後選取 [所有檔案 (*.*)]。
巡覽至包含 BaseSharePointProject 專案的目錄。 在這個資料夾中選取 key.snk,然後按一下 [加入]。
注意事項在本逐步解說中,您所建立的專案範本會以相同的 key.snk 檔案來簽署每一個使用範本建立的專案。 若要了解如何擴充這個範例來為每個專案執行個體建立新的 key.snk 檔案,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)。
重複步驟 5-8,在 BaseSharePointProject 目錄中從指定的子資料夾加入下列檔案:
\Field1\Elements.xml
\Field1\SharePointProjectItem.spdata
\Features\Feature1\Feature1.feature
\Features\Feature1\Feature1.Template.xml
\Package\Package.package
\Package\Package.Template.xml
直接將這些檔案加入至 SiteColumnProjectTemplate 專案,請勿在專案中重新建立 Field1、Features 或 Package 子資料夾。 如需這些檔案的詳細資訊,請參閱為 SharePoint 專案項目建立項目範本和專案範本。
若要設定開發人員在加入新的專案對話方塊中探索專案範本的方式
以滑鼠右鍵按一下 [方案總管] 中的 [SiteColumnProjectTemplate] 節點,然後按一下 [卸載專案]。 如果您接到儲存任何檔案變更的提示,請按一下 [是]。
再次以滑鼠右鍵按一下 [SiteColumnProjectTemplate] 節點,然後按一下 [編輯 SiteColumnProjectTemplate.csproj] 或 [編輯 SiteColumnProjectTemplate.vbproj]。
在專案檔中尋找下列 VSTemplate 項目。
<VSTemplate Include="SiteColumnProjectTemplate.vstemplate">以下列 XML 取代這個 VSTemplate 項目。
<VSTemplate Include="SiteColumnProjectTemplate.vstemplate"> <OutputSubPath>SharePoint\SharePoint14</OutputSubPath> </VSTemplate>OutputSubPath 項目會在您建置專案時用以建立專案範本的路徑中,指定其他資料夾。 這裡指定的資料夾,可確保專案範本只有在客戶於 [加入新的專案] 對話方塊中按一下 [SharePoint] 節點下的 [2010] 節點時才能使用。
儲存並關閉檔案。
以滑鼠右鍵按一下 [方案總管] 中的 [SiteColumnProjectTemplate] 專案,然後按一下 [重新載入專案]。
編輯專案範本檔
編輯 SiteColumnProjectTemplate 專案中的下列檔案,以定義專案範本的行為:
AssemblyInfo.cs 或 AssemblyInfo.vb
Elements.xml
SharePointProjectItem.spdata
Feature1.feature
Package.package
SiteColumnProjectTemplate.vstemplate
ProjectTemplate.csproj 或 ProjectTemplate.vbproj
在下列程序中,您會將可取代的參數加入至若干的這些檔案。 可取代的參數是以貨幣符號 ($) 字元開頭及結尾的語彙基元。 當使用者使用這個專案範本建立新專案時,Visual Studio 會自動在新專案中將這些參數取代為特定值。 如需詳細資訊,請參閱可置換的參數。
若要編輯 AssemblyInfo.cs 或 AssemblyInfo.vb 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 AssemblyInfo.cs 或 AssemblyInfo.vb 檔案。
將下列陳述式加入至這個檔案的最上方。
Imports System.Securityusing System.Security;SharePoint 專案的 [沙箱化方案] 屬性設為 [True] 時,Visual Studio 會將 System.Security.AllowPartiallyTrustedCallersAttribute 加入至 AssemblyInfo 程式碼檔案。 但是,專案範本中的 AssemblyInfo 程式碼檔案預設不會匯入 System.Security 命名空間。 您必須加入這個 using 或 Imports 陳述式,以免發生編譯錯誤。
儲存並關閉檔案。
若要編輯 Elements.xml 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 Elements.xml 檔案。
以下列 XML 取代檔案的內容。
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="https://schemas.microsoft.com/sharepoint/"> <Field ID="{$guid5$}" Name="$safeprojectname$" DisplayName="$projectname$" Type="Text" Group="Custom Columns"> </Field> </Elements>新的 XML 會加入一個 Field 項目,其定義網站欄的名稱、它的基底型別和要在組件庫中列出網站欄的群組。 如需這個檔案內容的詳細資訊,請參閱欄位定義結構描述 (英文)。
儲存並關閉檔案。
若要編輯 SharePointProjectItem.spdata 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 SharePointProjectItem.spdata 檔案。
以下列 XML 取代檔案的內容。
<?xml version="1.0" encoding="utf-8"?> <ProjectItem Type="Contoso.SiteColumn" DefaultFile="Elements.xml" xmlns="https://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel"> <Files> <ProjectItemFile Source="Elements.xml" Target="$safeprojectname$\" Type="ElementManifest" /> </Files> </ProjectItem>新的 XML 程會對檔案進行下列變更:
將 ProjectItem 項目的 Type 屬性變更為與傳遞至專案項目定義 (您之前在本逐步解說中建立的 SiteColumnProjectItemTypeProvider 類別) 上的 SharePointProjectItemTypeAttribute 相同的字串。
從 ProjectItem 項目移除 SupportedTrustLevels 和 SupportedDeploymentScopes 屬性。 這些屬性值是不必要的,因為信任層級和部署範圍會在 ProjectItemTypeDefinition 專案的 SiteColumnProjectItemTypeProvider 類別中指定。
如需 .spdata 檔案內容的詳細資訊,請參閱 SharePoint 專案項目結構描述參考。
儲存並關閉檔案。
若要編輯 Feature1.feature 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 Feature1.feature 檔案。
以下列 XML 取代檔案的內容。
<?xml version="1.0" encoding="utf-8"?> <feature xmlns:dm0="https://schemas.microsoft.com/VisualStudio/2008/DslTools/Core" dslVersion="1.0.0.0" Id="$guid4$" featureId="$guid4$" imageUrl="" solutionId="00000000-0000-0000-0000-000000000000" title="Site Column Feature1" version="" deploymentPath="$SharePoint.Project.FileNameWithoutExtension$_$SharePoint.Feature.FileNameWithoutExtension$" xmlns="https://schemas.microsoft.com/VisualStudio/2008/SharePointTools/FeatureModel"> <projectItems> <projectItemReference itemId="$guid2$" /> </projectItems> </feature>新的 XML 程會對檔案進行下列變更:
將 feature 項目的 Id 和 featureId 屬性值變更為 $guid4$。
將 projectItemReference 項目的 itemId 屬性值變更為 $guid2$。
如需 .feature 檔案的詳細資訊,請參閱為 SharePoint 專案項目建立項目範本和專案範本。
儲存並關閉檔案。
若要編輯 Package.package 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 Package.package 檔案。
以下列 XML 取代檔案的內容。
<?xml version="1.0" encoding="utf-8"?> <package xmlns:dm0="https://schemas.microsoft.com/VisualStudio/2008/DslTools/Core" dslVersion="1.0.0.0" Id="$guid3$" solutionId="$guid3$" resetWebServer="false" name="$safeprojectname$" xmlns="https://schemas.microsoft.com/VisualStudio/2008/SharePointTools/PackageModel"> <features> <featureReference itemId="$guid4$" /> </features> </package>新的 XML 程會對檔案進行下列變更:
將 package 項目的 Id 和 solutionId 屬性值變更為 $guid3$。
將 featureReference 項目的 itemId 屬性值變更為 $guid4$。
如需 .package 檔案的詳細資訊,請參閱為 SharePoint 專案項目建立項目範本和專案範本。
儲存並關閉檔案。
若要編輯 SiteColumnProjectTemplate.vstemplate 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 SiteColumnProjectTemplate.vstemplate 檔案。
以下列 XML 的其中一個區段取代檔案的內容: 如果建立 Visual C# 專案範本,請使用下列 XML。
<?xml version="1.0" encoding="utf-8"?> <VSTemplate Version="3.0.0" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005" Type="Project"> <TemplateData> <Name>Site Column</Name> <Description>Creates a new site column in SharePoint</Description> <FrameworkVersion>3.5</FrameworkVersion> <ProjectType>CSharp</ProjectType> <CreateNewFolder>true</CreateNewFolder> <CreateInPlace>true</CreateInPlace> <ProvideDefaultName>true</ProvideDefaultName> <DefaultName>SiteColumn</DefaultName> <LocationField>Enabled</LocationField> <EnableLocationBrowseButton>true</EnableLocationBrowseButton> <PromptForSaveOnCreation>true</PromptForSaveOnCreation> <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp> <Icon>SiteColumnProjectTemplate.ico</Icon> <SortOrder>1000</SortOrder> </TemplateData> <TemplateContent> <Project TargetFileName="SharePointProject1.csproj" File="ProjectTemplate.csproj" ReplaceParameters="true"> <ProjectItem ReplaceParameters="true" TargetFileName="Properties\AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Features\Feature1\Feature1.feature">Feature1.feature</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Features\Feature1\Feature1.Template.xml">Feature1.template.xml</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Package\Package.package">Package.package</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Package\Package.Template.xml">Package.Template.xml</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Field1\SharePointProjectItem.spdata">SharePointProjectItem.spdata</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Field1\Elements.xml" OpenInEditor="true">Elements.xml</ProjectItem> <ProjectItem ReplaceParameters="false" TargetFileName="key.snk">key.snk</ProjectItem> </Project> </TemplateContent> </VSTemplate>如果建立 Visual Basic 專案範本,請使用下列 XML。
<?xml version="1.0" encoding="utf-8"?> <VSTemplate Version="3.0.0" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005" Type="Project"> <TemplateData> <Name>Site Column</Name> <Description>Creates a new site column in SharePoint</Description> <FrameworkVersion>3.5</FrameworkVersion> <ProjectType>VisualBasic</ProjectType> <CreateNewFolder>true</CreateNewFolder> <CreateInPlace>true</CreateInPlace> <ProvideDefaultName>true</ProvideDefaultName> <DefaultName>SiteColumn</DefaultName> <LocationField>Enabled</LocationField> <EnableLocationBrowseButton>true</EnableLocationBrowseButton> <PromptForSaveOnCreation>true</PromptForSaveOnCreation> <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp> <Icon>SiteColumnProjectTemplate.ico</Icon> <SortOrder>1000</SortOrder> </TemplateData> <TemplateContent> <Project TargetFileName="SharePointProject1.vbproj" File="ProjectTemplate.vbproj" ReplaceParameters="true"> <ProjectItem ReplaceParameters="true" TargetFileName="My Project\AssemblyInfo.vb">AssemblyInfo.vb</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Features\Feature1\Feature1.feature">Feature1.feature</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Features\Feature1\Feature1.Template.xml">Feature1.template.xml</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Package\Package.package">Package.package</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Package\Package.Template.xml">Package.Template.xml</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Field1\SharePointProjectItem.spdata">SharePointProjectItem.spdata</ProjectItem> <ProjectItem ReplaceParameters="true" TargetFileName="Field1\Elements.xml" OpenInEditor="true">Elements.xml</ProjectItem> <ProjectItem ReplaceParameters="false" TargetFileName="key.snk">key.snk</ProjectItem> </Project> </TemplateContent> </VSTemplate>新的 XML 程會對檔案進行下列變更:
將 Name 項目設定為 [Site Column] 值 (這是出現在 [加入新的專案] 對話方塊中的名稱)。
為每個專案執行個體中包含的每個檔案加入 ProjectItem 項目。
如需 .vstemplate 檔案內容的詳細資訊,請參閱 Visual Studio 範本結構描述參考。
儲存並關閉檔案。
若要編輯 ProjectTemplate.csproj 或 ProjectTemplate.vbproj 檔案
在 SiteColumnProjectTemplate 專案中,按兩下 [方案總管] 中的 ProjectTemplate.csproj 檔案或 ProjectTemplate.vbproj 檔案。
以下列 XML 的其中一個區段取代檔案的內容: 如果建立 Visual C# 專案範本,請使用下列 XML。
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{$guid1$}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>$safeprojectname$</RootNamespace> <AssemblyName>$safeprojectname$</AssemblyName> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <ProjectTypeGuids>{BB1F664B-9266-4fd6-B973-E1E44974B511};{14822709-B5A1-4724-98CA-57A101D1B079};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup> <SignAssembly>true</SignAssembly> <AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <ItemGroup> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Data" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Web" /> <Reference Include="System.Xml" /> <Reference Include="System.Xml.Linq" /> <Reference Include="Microsoft.SharePoint" /> <Reference Include="Microsoft.SharePoint.Security" /> </ItemGroup> <ItemGroup> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <ItemGroup> <None Include="Field1\SharePointProjectItem.spdata"> <SharePointProjectItemId>{$guid2$}</SharePointProjectItemId> </None> <None Include="Field1\Elements.xml" /> </ItemGroup> <ItemGroup> <None Include="key.snk" /> <None Include="Package\Package.package"> <PackageId>{$guid3$}</PackageId> </None> <None Include="Package\Package.Template.xml"> <DependentUpon>Package.package</DependentUpon> </None> <None Include="Features\Feature1\Feature1.feature"> <FeatureId>{$guid4$}</FeatureId> </None> <None Include="Features\Feature1\Feature1.Template.xml"> <DependentUpon>Feature1.feature</DependentUpon> </None> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets" /> </Project>如果建立 Visual Basic 專案範本,請使用下列 XML。
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="https://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProductVersion> </ProductVersion> <SchemaVersion> </SchemaVersion> <ProjectGuid>{$guid1$}</ProjectGuid> <OutputType>Library</OutputType> <RootNamespace>$safeprojectname$</RootNamespace> <AssemblyName>$safeprojectname$</AssemblyName> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <ProjectTypeGuids>{BB1F664B-9266-4fd6-B973-E1E44974B511};{D59BE175-2ED0-4C54-BE3D-CDAA9F3214C8};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids> <OptionExplicit>On</OptionExplicit> <OptionCompare>Binary</OptionCompare> <OptionStrict>Off</OptionStrict> <OptionInfer>On</OptionInfer> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <DefineDebug>true</DefineDebug> <DefineTrace>true</DefineTrace> <OutputPath>bin\Debug\</OutputPath> <WarningLevel>4</WarningLevel> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> <DefineDebug>false</DefineDebug> <DefineTrace>true</DefineTrace> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <UseVSHostingProcess>false</UseVSHostingProcess> </PropertyGroup> <PropertyGroup> <SignAssembly>true</SignAssembly> <AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <ItemGroup> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Data" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Xml" /> <Reference Include="System.Xml.Linq" /> <Reference Include="Microsoft.SharePoint" /> <Reference Include="Microsoft.SharePoint.Security" /> </ItemGroup> <ItemGroup> <Import Include="Microsoft.VisualBasic" /> <Import Include="System" /> <Import Include="System.Collections" /> <Import Include="System.Collections.Generic" /> <Import Include="System.Data" /> <Import Include="System.Diagnostics" /> <Import Include="System.Linq" /> <Import Include="System.Xml.Linq" /> <Import Include="Microsoft.SharePoint" /> <Import Include="Microsoft.SharePoint.Security" /> </ItemGroup> <ItemGroup> <Compile Include="My Project\AssemblyInfo.vb" /> </ItemGroup> <ItemGroup> <AppDesigner Include="My Project\" /> </ItemGroup> <ItemGroup> <None Include="Features\Feature1\Feature1.feature"> <FeatureId>{$guid4$}</FeatureId> </None> <None Include="Field1\SharePointProjectItem.spdata"> <SharePointProjectItemId>{$guid2$}</SharePointProjectItemId> </None> <None Include="key.snk" /> <None Include="Package\Package.package"> <PackageId>{$guid3$}</PackageId> </None> <None Include="Package\Package.Template.xml"> <DependentUpon>Package.package</DependentUpon> </None> </ItemGroup> <ItemGroup> <None Include="Features\Feature1\Feature1.Template.xml"> <DependentUpon>Feature1.feature</DependentUpon> </None> <None Include="Field1\Elements.xml" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets" /> </Project>新的 XML 程會對檔案進行下列變更:
變更 TargetFrameworkVersion 項目以指定 .NET Framework 3.5。
加入 SignAssembly 和 AssemblyOriginatorKeyFile 項目以簽署專案輸出。
為 SharePoint 專案所使用的組件參考加入新的 Reference 項目。
為專案中的每個預設檔案 (例如 Elements.xml 和 SharePointProjectItem.spdata) 加入新的項目。
儲存並關閉檔案。
建立 VSIX 套件以部署專案範本
若要部署擴充功能,請在 SiteColumnProjectItem 方案中使用 VSIX 專案,以建立 VSIX 套件。 首先,修改 VSIX 專案包含的 source.extension.vsixmanifest 檔案,來設定 VSIX 套件。 接著,建置方案來建立 VSIX 套件。
若要設定和建立 VSIX 套件
在 [方案總管] 中,按兩下 SiteColumnProjectItem 專案中的 [source.extension.vsixmanifest] 檔案。
Visual Studio 會在資訊清單編輯器中開啟檔案。 source.extension.vsixmanifest 檔案是所有 VSIX 套件所需要 extension.vsixmanifest 檔案的基準。 如需這個檔案的詳細資訊,請參閱VSIX Extension Schema Reference。
在 [產品名稱] 方塊中,輸入 Site Column。
在 [作者] 方塊中,輸入 Contoso。
在 [說明] 方塊中,輸入 A SharePoint project for creating site columns。
在編輯器的 [內容] 區段中,按一下 [加入內容] 按鈕。
在 [加入內容] 對話方塊中,按一下 [選取內容類型] 下拉式方塊,然後選取 [專案範本]。
注意事項這個值對應於 extension.vsixmanifest 檔案中的 ProjectTemplate 項目。 此項目會識別 VSIX 套件中包含專案範本的子資料夾。 如需詳細資訊,請參閱 ProjectTemplate Element (VSX Schema)。
按一下 [選取來源] 底下的 [專案] 選項按鈕,並選取其旁邊清單方塊中的 [SiteColumnProjectTemplate]。
按一下 [確定]。
重新按一下 [加入內容] 按鈕。
在 [加入內容] 對話方塊的 [選取內容類型] 清單方塊中,選取 [MEF 元件]。
注意事項這個值對應於 extension.vsixmanifest 檔案中的 MefComponent 項目。 這個項目指定 VSIX 套件中的擴充組件名稱。 如需詳細資訊,請參閱 MEFComponent Element (VSX Schema)。
在 [選取來源] 底下選取 [專案] 選項按鈕,並選取其旁邊清單方塊中的 [ProjectItemTypeDefinition]。
按一下 [確定]。
在 [建置] 功能表上,按一下 [建置方案]。 確定專案編譯時未發生錯誤。
測試專案範本
您現在可以測試專案範本。 首先,在 Visual Studio 的實驗執行個體中開始偵錯 SiteColumnProjectItem 方案。 接著,在 Visual Studio 的實驗執行個體中測試 [網站欄] 專案。 最後,建置並執行 SharePoint 專案,確認網站欄功能正常。
若要開始偵錯方案
以系統管理員權限重新啟動 Visual Studio,並開啟 SiteColumnProjectItem 方案。
開啟 SiteColumnProjectItemTypeProvider 程式碼檔案,然後將中斷點加入至 InitializeType 方法內的第一行程式碼中。
按 F5 鍵啟動偵錯作業。
Visual Studio 會將擴充功能安裝至 %UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\Contoso\Site Column\1.0,並啟動 Visual Studio 的實驗執行個體。 您將會在 Visual Studio 的這個執行個體中測試專案項目。
若要在 Visual Studio 中測試專案
在 Visual Studio 的實驗執行個體中,指向 [檔案] 功能表上的 [新增],然後按一下 [專案]。
展開 [Visual C#] 或 [Visual Basic] (視專案範本所支援的語言而定),然後展開 [SharePoint] 節點,再按一下 [2010]。
在專案範本的清單中,選取 [網站欄]。
在 [名稱] 方塊中輸入 SiteColumnTest。
按一下 [確定]。 新的專案會出現在 [方案總管] 中,並具有名稱為 [Field1] 的專案項目。
確認另一個 Visual Studio 執行個體中的程式碼在您之前於 InitializeType 方法中設定的中斷點停止。 按 F5 繼續偵錯專案。
按一下 [方案總管] 中的 [Field1] 節點,然後按 F4。 [屬性] 視窗隨即開啟。
確認 [Example Property] 屬性出現在屬性清單中。
若要在 SharePoint 中測試網站欄
按一下 [方案總管] 中的 [SiteColumnTest] 節點。
在 [屬性] 視窗中,按一下 [網站 URL] 屬性旁邊的文字方塊,然後輸入 https://localhost。 這會指定要用於偵錯的開發電腦上的本機 SharePoint 網站。
注意事項[網站 URL] 屬性預設是空的,因為網站欄專案範本在建立時並未提供用來收集這個值的精靈。 若要了解如何加入要求開發人員提供這個值的精靈,然後在新專案中設定這個屬性,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)。
請按 F5。 網站欄會封裝並部署至專案的 [網站 URL] 屬性指定的 SharePoint 網站。 Web 瀏覽器會開啟此網站的預設頁面。
注意事項如果顯示 [已停用指令碼偵錯] 對話方塊,請按一下 [是] 繼續進行專案偵錯。
按一下 [網站動作] 功能表上的 [網站設定]。
按一下 [組件庫] 底下的 [網站欄]。
在網站欄清單中,確認有一個 [Custom Columns] 群組,其中包含名為 [SiteColumnTest] 的欄。
關閉 Web 瀏覽器。
清理開發電腦
在您完成測試專案之後,請從 Visual Studio 的實驗執行個體中移除專案範本。
若要清理開發電腦
在 Visual Studio 的實驗執行個體中,按一下 [工具] 功能表上的 [擴充管理員]。
[擴充管理員] 對話方塊隨即開啟。
在擴充功能清單中,按一下 [網站欄],然後按一下 [解除安裝]。
在所顯示的對話方塊中,按一下 [是],確認您要解除安裝擴充功能。
按一下 [立即重新啟動] 完成解除安裝。
關閉 Visual Studio 的兩個執行個體 (Visual Studio 的實驗執行個體,以及其中有開啟 SiteColumnProjectItem 專案的執行個體)。
後續步驟
完成本逐步解說之後,您可以將精靈加入至專案範本。 當使用者建立網站欄專案時,精靈會詢問使用者要用於偵錯的網站 URL 以及是否將新方案沙箱化,精靈會藉此資訊設定新專案。 精靈也會收集欄的相關資訊 (例如基底型別和要在網站欄組件庫中列出欄的群組),並將此資訊加入至新專案中的 Elements.xml 檔案。 如需詳細資訊,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)。