SharePoint 项目是包含一个或多个 SharePoint 项目项的容器。在 Visual Studio 中,您可以通过创建您自己的 SharePoint 项目项类型,然后将其与某个项目模板相关联,从而扩展 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 SDK。本演练使用 SDK 中的**“VSIX 项目”**模板来创建 VSIX 包以部署项目项。有关更多信息,请参见扩展 Visual Studio 中的 SharePoint 工具。
了解以下概念很有用,但对于完成本演练并不是必需的:
SharePoint 中的网站栏。有关更多信息,请参见列。
Visual Studio 中的项目模板。有关更多信息,请参见Visual Studio 模板介绍。
创建项目
若要完成本演练,您需要创建以下三个项目:
一个 VSIX 项目。此项目创建 VSIX 包来部署网站栏项目项和项目模板。
一个项目模板项目。此项目创建一个项目模板,该项目模板可用于创建包含网站栏项目项的新 SharePoint 项目。
一个类库项目。此项目实现一个定义网站栏项目项的行为的 Visual Studio 扩展。
从创建项目开始本演练。
创建 VSIX 项目
启动 Visual Studio。
在菜单栏上,选择**“文件”,“新建**、“项目”。
在 新建项目 对话框顶部,确保 .NET Framework 4.5 在 .NET Framework 的版本列表中选择。
外接 Visual Basic 或 visual C# 节点,然后选择 扩展性 节点。
说明
只有在安装 Visual Studio 2012 SDK 之后,“扩展性”节点才可用。有关更多信息,请参见本主题前面的系统必备部分。
在项目模板列表中,选择 VSIX 项目。
在 名称 框中,输入 SiteColumnProjectItem,然后选择 确定 按钮。
Visual Studio 将“SiteColumnProjectItem”项目添加到**“解决方案资源管理器”**中。
创建项目模板项目
在 解决方案资源管理器,请打开解决方案节点的快捷菜单上,选择 添加,然后选择 新建项目。
说明
在 Visual Basic 项目中,仅当在General, Projects and Solutions, Options Dialog Box中选中“总是显示解决方案”复选框时,解决方案节点才会出现在“解决方案资源管理器”中。
在 新建项目 对话框顶部,确保 .NET Framework 4.5 在 .NET Framework 的版本列表中选择。
外接 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.5 在 .NET Framework 的版本列表中选择。
外接 visual C# 或 Visual Basic 节点,选择 Windows 节点,然后选择 类库 模板。
在 名称 框中,输入 ProjectItemTypeDefinition 然后选择 确定 按钮。
Visual Studio 将**“ProjectItemTypeDefinition”**项目添加到解决方案中,并打开默认的 Class1 代码文件。
从项目中删除 Class1 代码文件。
配置扩展项目
添加代码文件和程序集引用以配置扩展项目。
配置项目
在 ProjectItemTypeDefinition 项目中,添加名为 SiteColumnProjectItemTypeProvider的代码文件。
在菜单栏上,依次选择 项目,添加引用。
在 引用管理器- ProjectItemTypeDefinition 对话框中,展开 程序集 节点,选择 框架 节点,然后选择 System.ComponentModel.Composition 复选框。
选择 扩展 节点,请在 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 项目文件指定项模板位置出现在 新建项目 对话框。
为项目模板创建文件
开始 Visual Studio 第二个实例使用管理凭据的。
创建名为 BaseSharePointProject的 SharePoint 2010 项目。
重要事项
在 SharePoint 自定义向导,不要选择 部署为场解决方案 选项按钮。
向项目中添加"空元素添加到项目,然后将项目命名为 字段 1。
保存项目,然后关闭 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、功能或包子文件夹。有关这些文件的更多信息,请参见为 SharePoint 项目项创建项模板和项目模板。
配置开发人员如何查看"新建项目"对话框的项目模板
在 解决方案资源管理器,打开 SiteColumnProjectTemplate 项目节点的快捷菜单,然后选择 卸载项目。如果提示您保存对任何文件的更改,请选择 是 按钮。
再次打开 SiteColumnProjectTemplate 节点的快捷菜单,然后选择 Edit SiteColumnProjectTemplate.csproj 或 Edit SiteColumnProjectTemplate.vbproj。
在项目文件中,找到以下 VSTemplate 元素。
<VSTemplate Include="SiteColumnProjectTemplate.vstemplate">
用以下 XML 替换此元素。
<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.Security
using System.Security;
当 SharePoint 项目的**“沙盒解决方案”属性设置为“True”** 时,Visual Studio 会将 System.Security.AllowPartiallyTrustedCallersAttribute 添加到 AssemblyInfo 代码文件中。但默认情况下,项目模板中的 AssemblyInfo 代码文件不会导入 System.Security 命名空间。必须添加此 using 或 Imports 语句以防止编译错误。
保存并关闭文件。
编辑 Elements.xml 文件
在 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 元素,该元素定义网站栏的名称、基类型以及将在其中列出库中的网站栏的组。有关此文件的内容的更多信息,请参见字段定义架构。
保存并关闭文件。
编辑 SharePointProjectItem.spdata 文件
在 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 项目项架构参考。
保存并关闭文件。
编辑 Feature1.feature 文件
在 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 项目项创建项模板和项目模板。
保存并关闭文件。
编辑 Package.package 文件
在 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 项目项创建项模板和项目模板。
保存并关闭文件。
编辑 SiteColumnProjectTemplate.vstemplate 文件
在 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 模板架构参考。
保存并关闭文件。
编辑 ProjectTemplate.csproj 或 ProjectTemplate.vbproj 文件
在 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>
- 如果要创建 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 中,没有 4.5。
添加 SignAssembly 和 AssemblyOriginatorKeyFile 元素对项目输出进行签名。
添加程序集的 Reference 元素引用 SharePoint 项目。
添加每个默认文件的元素在项目,如 Elements.xml 和 SharePointProjectItem.spdata。
保存并关闭文件。
创建 VSIX 包以部署项目模板
若要部署扩展,请使用 SiteColumnProjectItem 解决方案中的 VSIX 项目来创建 VSIX 包。首先,通过修改 VSIX 项目中包含的 source.extension.vsixmanifest 文件来配置 VSIX 包。然后,通过生成解决方案来创建 VSIX 包。
配置并创建 VSIX 包
在 解决方案资源管理器,在 SiteColumnProjectItem 项目中,打开在清单编辑器中的 source.extension.vsixmanifest 文件。
source.extension.vsixmanifest 文件是所有 VSIX 包必需的 extension.vsixmanifest 文件的基础。有关此文件的更多信息,请参见VSIX 扩展架构参考。
在 产品名称 框中,输入 网站栏。
在 作者 框中,输入 Contoso。
在 说明 框中,输入 创建网站栏一个 SharePoint 项目。
选择 资产 选项卡,然后选择 新建 按钮。
添加新资产 对话框打开。
在 类型 列表中,选择 Microsoft.VisualStudio.ProjectTemplate。
说明
此值对应于 extension.vsixmanifest 文件中的 ProjectTemplate 元素。此元素标识包含项目模板的 VSIX 包中的子文件夹。有关更多信息,请参见ProjectTemplate Element (VSX Schema)。
在 源 列表中,选择 当前解决方案中的项目。
在 项目 列表,然后选择 SiteColumnProjectTemplate,然后选择 确定 按钮。
再次选择 新建 按钮。
添加新资产 对话框打开。
在 类型 列表中,选择 Microsoft.VisualStudio.MefComponent。
说明
此值对应于 extension.vsixmanifest 文件中的 MefComponent 元素。此元素指定 VSIX 包中的扩展程序集的名称。有关更多信息,请参见MEFComponent Element。
在 源 列表中,选择 当前解决方案中的项目。
在 项目 列表中,选择 ProjectItemTypeDefinition,然后选择 确定 按钮。
在菜单栏上,依次选择 Build,生成解决方案,然后确保项目在编译时不会出错。
测试项目模板
现在您可以对项目模板进行测试了。首先,在 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 然后选择 确定 按钮。
在 解决方案资源管理器,将出现一个新名为 字段 1的项目项。
验证另一个 Visual Studio 实例的代码在 InitializeType 方法设置的断点处停止,然后选择 F5 键继续调试项目。
在 解决方案资源管理器,选择 字段 1 节点,然后选择 F4 键。
这将打开**“属性”**窗口。
在属性列表,以确认属性 示例属性 显示。
在 SharePoint 中测试网站栏
在 解决方案资源管理器,选择 SiteColumnTest 节点。
在 属性 窗口中,在位于 网站 URL 属性旁边的文本框中,输入 https://localhost。
此步骤将在要用于调试的开发计算机上指定本地 SharePoint 网站。
说明
网站 URL 默认情况下属性为空,因为"网站栏"项目模板用于收集此值不提供了向导,在创建项目时。若要了解如何添加一个要求开发人员提供此值然后在新项目中配置此属性的向导,请参见演练:使用项目模板创建网站栏项目项(第 2 部分)。
选择 F5 键。
网站栏进行打包并部署到 SharePoint。在项目的 网站 URL 属性指定站点。该浏览器将打开此网站的默认页。
说明
如果 脚本调试被禁用 出现对话框,请选择 是 按钮继续调试项目。
在 网站操作 菜单中,选择 站点设置。
在 站点设置 页上,在 库 下列表中,选择 网站列 链接。
在网站的列,请验证 自定义栏 组包含一个名为 SiteColumnTest的列。
关闭该浏览器。
清理开发计算机
测试完项目之后,从 Visual Studio 的实验实例中移除项目模板。
清理开发计算机
在 Visual Studio 的实验实例中,在菜单栏上,依次选择 工具,扩展和更新。
扩展和更新 对话框打开。
在扩展列表中,选择 网站栏 扩展,然后选择 卸载 按钮。
在出现的对话框中,选择 是 按钮以确认您要卸载该扩展。
选择 关闭 按钮来卸载。
关闭 Visual Studio 的实验实例 (和 SiteColumnProjectItem 解决方案处于打开状态的实例) 两个实例 Visual Studio。
后续步骤
完成本演练后,可以向项目模板添加向导。当用户创建“网站栏”项目时,该向导将要求用户提供网站 URL 以用于调试,并选择新解决方案是否为沙盒解决方案,然后该向导将使用这些信息配置新项目。该向导还会收集有关列的信息(如基类型和将在其中列出网站栏库中的列的组),并将这些信息添加到新项目的 Elements.xml 文件中。有关更多信息,请参见演练:使用项目模板创建网站栏项目项(第 2 部分)。