共用方式為


逐步解說:使用專案範本建立網站欄專案項目 (第 1 部分)

SharePoint 專案是一個或多個 SharePoint 專案項目的容器。您可以建立自己的 SharePoint 專案項目類型產生關聯它們所擴充 Visual Studio 中的 SharePoint 專案系統與專案範本。在本逐步解說中,您將建立定義用於建立網站欄的專案項目類型,然後建立可用來建立包含網站欄專案項目之新專案的專案範本。

本逐步解說將示範下列工作:

  • 建立為網站欄定義新的 SharePoint 專案項目類型的 Visual Studio 擴充功能。此專案項目類型包含會出現 [屬性] 視窗中的簡單自訂屬性。

  • 為專案項目建立 Visual Studio 專案範本。

  • 建置 Visual Studio 擴充功能 (VSIX) 套件以部署專案範本和擴充功能組件。

  • 偵錯和測試專案項目。

這是獨立的逐步解說。完成本逐步解說之後,您可以將精靈加入至專案範本來增強專案項目。如需詳細資訊,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)

注意事項注意事項

您可以從下列位置取得本逐步解說所含之完成的專案、程式碼和其他檔案:https://go.microsoft.com/fwlink/?LinkId=191369 (英文)。

必要條件

開發電腦上需要下列元件才能完成此逐步解說:

了解下列概念有助於完成此逐步解說 (但非必要):

  • SharePoint 中的網站欄。如需詳細資訊,請參閱 (英文)。

  • Visual Studio 中的專案範本。如需詳細資訊,請參閱Visual Studio 範本簡介

建立專案

若要完成這個逐步解說,您必須建立三個專案:

  • VSIX 專案。這個專案會建立 VSIX 套件,以部署網站欄專案項目和專案範本。

  • 專案範本專案。這個專案會建立可用來建立包含網站欄專案項目之新 SharePoint 專案的專案範本。

  • 類別庫專案。這個實作 Visual Studio 擴充功能的專案會定義網站欄專案項目的行為。

從建立這些專案開始進行此逐步解說。

若要建立 VSIX 專案

  1. 啟動 Visual Studio。

  2. 在功能表列上,選擇 [檔案], [新增], [Project]。

  3. 在 [新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 .NET Framework 版本的清單中選取。

  4. 展開 [Visual Basic] 或 [Visual C#] 節點,然後選取 [擴充性] 節點。

    注意事項注意事項

    [擴充性] 節點只有在安裝 Visual Studio 2012 SDK 時才可使用。如需詳細資訊,請參閱本主題稍早討論的<必要條件>一節。

  5. 在專案範本清單中,選取 [VSIX 專案]。

  6. 在 [Name]方塊中,輸入 SiteColumnProjectItem專案,然後選取 [確定] 按鈕。

    Visual Studio 會將 SiteColumnProjectItem 專案加入至 [方案總管]。

若要建立專案範本專案

  1. 在 [方案總管],開啟方案節點的捷徑功能表,選擇[新增],然後選取 [新增專案]。

    注意事項注意事項

    在 Visual Basic 專案中,方案節點只有在已選取General, Projects and Solutions, Options Dialog Box中的 [永遠顯示方案] 核取方塊時,才會出現在 [方案總管] 中。

  2. 在 [新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 .NET Framework 版本的清單中選取。

  3. 展開 [Visual C#] 或 [Visual Basic] 節點,然後選取 [擴充性] 節點。

  4. 在專案範本清單中,選取 [C# 專案範本] 或 [Visual Basic 專案範本]範本。

  5. 在 [Name]方塊中,輸入 SiteColumnProjectTemplate,然後選取 [確定] 按鈕。

    Visual Studio 隨即將 [SiteColumnProjectTemplate] 專案加入至方案。

  6. 從專案刪除 Class1 程式碼檔。

  7. 如果您建立了 Visual Basic 專案,請一併從該專案刪除下列檔案:

    • MyApplication.Designer.vb

    • MyApplication.myapp

    • Resources.Designer.vb

    • Resources.resx

    • Settings.Designer.vb

    • Settings.settings

若要建立擴充功能專案

  1. 在 [方案總管],開啟方案節點的捷徑功能表,選擇[新增],然後選取 [新增專案]。

  2. 在 [新增專案] 對話方塊的頂端,確定 .NET Framework 4.5 .NET Framework 版本的清單中選取。

  3. 展開 Visual C# 或 [Visual Basic]] 節點,然後選取 [視窗] 節點,然後選取 [類別庫] 範本。

  4. 在 [Name]方塊中,輸入 ProjectItemTypeDefinition 然後選取 [確定] 按鈕。

    Visual Studio 隨即將 [ProjectItemTypeDefinition] 專案加入至方案,然後開啟預設的 Class1 程式碼檔。

  5. 從專案刪除 Class1 程式碼檔。

設定擴充功能專案

加入程式碼檔案和組件參考,以設定擴充功能專案。

若要設定專案

  1. 在 ProjectItemTypeDefinition 專案中,加入名為 SiteColumnProjectItemTypeProvider的程式碼檔。

  2. 在功能表列上,選擇 [Project], [新增參考]。

  3. 參考管理員- ProjectItemTypeDefinition 對話方塊中,展開節點,然後選取 [組件][Framework] 節點,然後選取並核取方塊。

  4. 選取 [延伸] 節點,在 Microsoft.VisualStudio.SharePoint 組件旁邊的核取方塊,然後選取 [確定] 按鈕。

定義新的 SharePoint 專案項目類型

建立實作 ISharePointProjectItemTypeProvider 介面的類別,以定義新專案項目類型的行為。在您要定義新的專案項目類型時實作此介面。

若要定義新的 SharePoint 專案項目類型

  • 在 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 Namespace
    
    using 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 專案檔指定專案範本位置出現在 [新增專案] 對話方塊。

若要為專案範本建立檔案

  1. 開始 Visual Studio 第二個執行個體具有管理認證的。

  2. 建立名為 Test BaseSharePointProject的 SharePoint 專案。

    重要事項重要事項

    在 [SharePoint 自訂精靈],不要選取 [部署為陣列方案] 選項按鈕。

  3. 將 [空元素] 項目加入至專案,然後命名項目 Field1。

  4. 儲存專案,然後關閉 Visual Studio第二個執行個體。

  5. 在中開啟 SiteColumnProjectItem 專案的方案,在 [方案總管]Visual Studio 的執行個體中,開啟 [SiteColumnProjectTemplate]專案節點的捷徑功能表,選擇[新增],然後選取 [現有項目]。

  6. 在 [新增現有項目] 對話方塊,請開啟副檔名清單,然後選取 [所有檔案 (*.*)。]。

  7. 在包含 BaseSharePointProject 專案的目錄,選取 key.snk 檔案,然後選取 [新增] 按鈕。

    注意事項注意事項

    在這個逐步解說中,您可以使用範本,建立使用相同的 key.snk 檔案簽署每個專案建立的專案範本。若要了解如何擴充這個範例會為每個專案執行個體的不同的 key.snk 檔案,請參閱 逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)

  8. 重複步驟 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、功能或封裝子資料夾。如需這些檔案的詳細資訊,請參閱為 SharePoint 專案項目建立項目範本和專案範本

設定開發人員如何尋找 [新增專案] 對話方塊中的專案範本

  1. 在 [方案總管],開啟 [SiteColumnProjectTemplate]專案節點的捷徑功能表,然後選取 [卸載專案]。如果系統提示您儲存對任何檔案的變更,請選擇 [] 按鈕。

  2. 再次開啟 [SiteColumnProjectTemplate]節點的捷徑功能表,然後選取 [編輯 SiteColumnProjectTemplate.csproj]或 [編輯 SiteColumnProjectTemplate.vbproj]。

  3. 在專案檔中,找出下列 VSTemplate 項目。

    <VSTemplate Include="SiteColumnProjectTemplate.vstemplate">
    
  4. 以下列 XML 取代項目。

    <VSTemplate Include="SiteColumnProjectTemplate.vstemplate">
      <OutputSubPath>SharePoint\SharePoint14</OutputSubPath>
    </VSTemplate>
    

    OutputSubPath 項目會在您建置專案時用以建立專案範本的路徑中,指定其他資料夾。這裡指定的資料夾確保專案範本可供使用,只有在用戶 [新增專案] 開啟對話方塊時, [SharePoint] 展開節點,然後選取 [2010] 節點。

  5. 儲存並關閉檔案。

  6. 在 [方案總管],開啟 [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 檔案

  1. 在 SiteColumnProjectTemplate 專案中,開啟 AssemblyInfo.cs 或 AssemblyInfo.vb 檔案中,然後在頂端加入下列陳述式:

    Imports System.Security
    
    using System.Security;
    

    SharePoint 專案的 [沙箱化方案] 屬性設為 [True] 時,Visual Studio 會將 System.Security.AllowPartiallyTrustedCallersAttribute 加入至 AssemblyInfo 程式碼檔案。然而,預設專案範本中的 AssemblyInfo 程式碼檔不 System.Security 匯入命名空間。您必須加入這個 using 或 Imports 陳述式,以免發生編譯錯誤。

  2. 儲存並關閉檔案。

若要編輯 Elements.xml 檔案

  1. 在 SiteColumnProjectTemplate 專案中,以下列 XML 取代 Elements.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 項目,其定義網站欄的名稱、它的基底型別和要在組件庫中列出網站欄的群組。如需這個檔案內容的詳細資訊,請參閱欄位定義結構描述 (英文)。

  2. 儲存並關閉檔案。

若要編輯 SharePointProjectItem.spdata 檔案

  1. 在 SiteColumnProjectTemplate 專案中,以下列 XML 取代 SharePointProjectItem.spdata 檔案的內容。

    <?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 專案項目結構描述參考

  2. 儲存並關閉檔案。

若要編輯 Feature1.feature 檔案

  1. 在 SiteColumnProjectTemplate 專案中,以下列 XML 取代 Feature1.feature 檔案的內容。

    <?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 專案項目建立項目範本和專案範本

  2. 儲存並關閉檔案。

若要編輯 Package.package 檔案

  1. 在 SiteColumnProjectTemplate 專案中,以下列 XML 取代 Package.package 檔案的內容。

    <?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 專案項目建立項目範本和專案範本

  2. 儲存並關閉檔案。

若要編輯 SiteColumnProjectTemplate.vstemplate 檔案

  1. 在 SiteColumnProjectTemplate 專案中,以 XML 的下列其中一個區段取代 SiteColumnProjectTemplate.vstemplate 檔案的內容。

    • 如果建立 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 項目對值 [網站資料行]。(這個名稱會出現 [新增專案] 對話方塊)。

    • 將每 ProjectItem 項目在每個專案執行個體中的 filethat 的。

    • 使用命名空間「https://schemas.microsoft.com/developer/vstemplate/2005」。在這個方案的其他專案檔使用「https://schemas.microsoft.com/developer/msbuild/2003」命名空間。因此, XML 結構描述警告訊息產生,不過,您可以忽略它們在這個逐步解說。

    如需 .vstemplate 檔案內容的詳細資訊,請參閱 Visual Studio 範本結構描述參考

  2. 儲存並關閉檔案。

若要編輯 ProjectTemplate.csproj 或 ProjectTemplate.vbproj 檔案

  1. 在 SiteColumnProjectTemplate 專案中,以 XML 的下列其中一個區段取代檔案 ProjectTemplate.csproj 或 ProjectTemplate.vbproj 檔案的內容。

    • 如果建立 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>
    
    1. 如果您建立了 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 中,沒有 4.5。

    • 將 SignAssembly 和 AssemblyOriginatorKeyFile 項目簽署專案輸出。

    • 將項目 Reference 供 SharePoint 專案所使用的組件參考。

    • 加入每份文件的預設項目在專案中,例如 Elements.xml 和 SharePointProjectItem.spdata。

  2. 儲存並關閉檔案。

建立 VSIX 套件以部署專案範本

若要部署擴充功能,請在 SiteColumnProjectItem 方案中使用 VSIX 專案,以建立 VSIX 套件。首先,修改 VSIX 專案包含的 source.extension.vsixmanifest 檔案,來設定 VSIX 套件。接著,建置方案來建立 VSIX 套件。

若要設定和建立 VSIX 套件

  1. 在 [方案總管],在 [SiteColumnProjectItem]專案,請在資訊清單編輯器中的 source.extension.vsixmanifest 檔案。

    source.extension.vsixmanifest 檔案是所有 VSIX 套件所需要的 extension.vsixmanifest 檔案的基準。如需這個檔案的詳細資訊,請參閱VSIX 擴充結構描述參考

  2. 在 [產品名稱]方塊中,輸入 網站資料行。

  3. 在 [作者]方塊中,輸入 Contoso。

  4. 在 [Description]方塊中,輸入 建立網站欄的 SharePoint 專案。

  5. 選取 [資產] 索引標籤,然後選取 [新增] 按鈕。

    [將新的屬性] 對話方塊隨即開啟。

  6. 在 [型別] 清單中,選取 [Microsoft.VisualStudio.ProjectTemplate]。

    注意事項注意事項

    這個值對應於 extension.vsixmanifest 檔案中的 ProjectTemplate 項目。此項目會識別 VSIX 套件中包含專案範本的子資料夾。如需詳細資訊,請參閱ProjectTemplate Element (VSX Schema)

  7. 在 [Source] 清單中,選取 [在目前方案中的專案]。

  8. 在 [Project] 清單,然後選擇 [SiteColumnProjectTemplate],然後選取 [確定] 按鈕。

  9. 再次選取 [新增] 按鈕。

    [將新的屬性] 對話方塊隨即開啟。

  10. 在 [型別] 清單中,選取 [Microsoft.VisualStudio.MefComponent]。

    注意事項注意事項

    這個值對應於 extension.vsixmanifest 檔案中的 MefComponent 項目。這個項目指定 VSIX 套件中的擴充組件名稱。如需詳細資訊,請參閱MEFComponent Element

  11. 在 [Source] 清單中,選取 [在目前方案中的專案]。

  12. 在 [Project] 清單中,選取 [ProjectItemTypeDefinition],然後選取 [確定] 按鈕。

  13. 在功能表列上,選擇, [組建][建置方案],然後確定專案在編譯時未發生任何錯誤。

測試專案範本

您現在可以測試專案範本。首先,在 Visual Studio 的實驗執行個體中開始偵錯 SiteColumnProjectItem 方案。接著,在 Visual Studio 的實驗執行個體中測試 [網站欄] 專案。最後,建置並執行 SharePoint 專案,確認網站欄功能正常。

若要開始偵錯方案

  1. 重新啟動以管理認證的 Visual Studio,然後開啟 SiteColumnProjectItem 方案。

  2. 在 SiteColumnProjectItemTypeProvider 程式碼檔案,請將中斷點加入至 InitializeType 方法的第一行程式碼,然後選取 [F5]索引鍵開始偵錯。

    Visual Studio 會將擴充功能安裝至 %UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\Contoso\Site Column\1.0,並啟動 Visual Studio 的實驗執行個體。您將會在 Visual Studio 的這個執行個體中測試專案項目。

若要在 Visual Studio 中測試專案

  1. 在 Visual Studio 的實驗執行個體,在功能表列上,選擇 [檔案], [新增], [Project]。

  2. 展開 [Visual C#] 或 [Visual Basic] 節點 (視專案範本) 支援的語言, [SharePoint] 展開節點,然後選取 [2010] 節點。

  3. 在專案範本清單中,選取 [網站資料行] 範本。

  4. 在 [Name]方塊中,輸入 SiteColumnTest 然後選取 [確定] 按鈕。

    在 [方案總管],新的專案會出現在與名為 [Field1] 的專案項目。

  5. 確認在另一個 Visual Studio 執行個體中的程式碼在您之前於方法 InitializeType ,然後選取 [F5]索引鍵繼續偵錯專案的中斷點停止。

  6. 在 [方案總管],選取 [Field1]節點,然後選取 [F4]索引鍵。

    [屬性] 視窗隨即開啟。

  7. 在屬性清單中,確認屬性 [屬性範例] 外觀。

若要在 SharePoint 中測試網站欄

  1. 在 [方案總管],選取 [SiteColumnTest]節點。

  2. 在 [屬性] 視窗,請在 [網站 URL] 屬性旁邊的文字方塊中,輸入 https://localhost。

    這個步驟中您要用於偵錯的開發電腦指定本機 SharePoint 網站。

    注意事項注意事項

    [網站 URL] 屬性是空的預設情況下,因為網站欄專案範本提供收集此值不會提供精靈,當專案建立時。若要了解如何加入要求開發人員提供這個值的精靈,然後在新專案中設定這個屬性,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)

  3. 選取 [F5]索引鍵。

    網站欄在專案的 [網站 URL] 屬性指定的 SharePoint 網站封裝並部署。這個瀏覽器開啟此網站的預設網頁。

    注意事項注意事項

    如果 [已停用指令碼偵錯] 對話方塊出現時,請選擇 [] 按鈕繼續偵錯專案。

  4. 在 [[網站動作] 功能表中,選擇 [網站設定]。

  5. 在 [設置設定] 網頁,在 [圖庫] 清單底下,選取 [網站欄] 連結。

  6. 在網站欄清單中,確認 [自訂資料行] 群組包含名為 [SiteColumnTest] 的資料行。

  7. 結束這個瀏覽器。

清理開發電腦

在您完成測試專案之後,請從 Visual Studio 的實驗執行個體中移除專案範本。

若要清理開發電腦

  1. 在 Visual Studio 的實驗執行個體,在功能表列上,選擇, [工具][副檔名和更新]。

    [副檔名和更新] 對話方塊隨即開啟。

  2. 在擴充功能清單中,選取 [網站資料行] 副檔名,然後選取 [解除安裝] 按鈕。

  3. 在出現的對話方塊中,選取 [] 按鈕確認您要解除安裝擴充功能。

  4. 選取 [要關閉嗎] 按鈕完成解除安裝。

  5. 關閉 Visual Studio 的實驗執行個體和 SiteColumnProjectItem 方案已開啟) 的兩個執行個體執行 Visual Studio。

後續步驟

完成本逐步解說之後,您可以將精靈加入至專案範本。當使用者建立網站欄專案時,精靈會詢問使用者要用於偵錯的網站 URL 以及是否將新方案沙箱化,精靈會藉此資訊設定新專案。精靈也會收集欄的相關資訊 (例如基底型別和要在網站欄組件庫中列出欄的群組),並將此資訊加入至新專案中的 Elements.xml 檔案。如需詳細資訊,請參閱逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)

請參閱

工作

逐步解說:使用專案範本建立網站欄專案項目 (第 2 部分)

概念

定義自訂 SharePoint 專案項目類型

為 SharePoint 專案項目建立項目範本和專案範本

儲存 SharePoint 專案系統擴充功能的資料

讓自訂資料與 SharePoint 工具擴充功能產生關聯