sharepoint 部署_如何在Sharepoint中创建和部署自定义字段类型

sharepoint 部署

This example will use a custom field type to create a drop down list populated from a Sharepoint List.

本示例将使用自定义字段类型来创建从Sharepoint List填充的下拉列表。

There are four main components to building a custom field&

建立自定义字段有四个主要组成部分,

"      Field Class* This must inherit from an existing SPField class. e.g. SPFieldText. It handles custom validation for the field, as well as defining the Field Control used to display it.

“ Field Class *这必须继承自现有的SPField类。例如SPFieldText。它处理该字段的自定义验证,并定义用于显示该字段的Field控件。

"      User Control Rendering Template.  This defines the control to be used to display the custom field.

用户控件呈现模板。它定义了用于显示自定义字段的控件。

"      Field Control Class. This contains the code-behind for the user control file, and defines how the control is rendered.

字段控件类。它包含用户控件文件的背后代码,并定义控件的呈现方式。

"      Field Type Definition File. This contains the information that SharePoint needs to correctly render the field, as well as information about the assembly that contains the compiled field type.

字段类型定义文件。其中包含SharePoint正确呈现字段所需的信息,以及有关包含已编译字段类型的程序集的信息。

The example demonstrates a custom field type designed to list all entries within an example list entitled Organisation List.

该示例演示了一个定制字段类型,该类型旨在列出示例列表中名为Organization List的所有条目。

1.现场班 (1. Field Class)

Each instance of this class represents a separate field based on the custom field type. It must inherit from one of the SPField subtypes, a list of which can be found at the end of this document. The most commonly used subtype is SPFieldText, and this is used in the following example code.

此类的每个实例都基于自定义字段类型表示一个单独的字段。 它必须继承自SPField子类型之一,其子列表可以在本文档的末尾找到。 最常用的子类型是SPFieldText,在以下示例代码中将使用它。

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;

namespace Company.Application.Controls
{
    [CLSCompliant(false)]
    [Guid("01d5cd28-741b-4229-8d47-24e5a068b5af")]
    public class OrganisationDropDownField : SPFieldText
    {
        public OrganisationDropDownField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }
        
        public OrganisationDropDownField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        public override BaseFieldControl FieldRenderingControl
        {
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                BaseFieldControl fieldControl = new OrganisationDropDownFieldControl();
                fieldControl.FieldName = this.InternalName;

                return fieldControl;
            }
        }

        public override string GetValidatedString(object value)
        {
            if (Required && String.IsNullOrEmpty(value.ToString()))
            {
                throw new SPFieldValidationException("No Organisation selected");
            }
            return base.GetValidatedString(value);
        }
    }
}

Here the BaseFieldControl is set to use a custom drop down control called OrganisationDropDownFieldControl, defined in the Field Control Class below. In the example some custom validation is also included within the overridden GetValidatedString method, to ensure a value is selected when mandatory.

在这里,BaseFieldControl设置为使用名为OrganisationDropDownFieldC的自定义下拉控件 ontrol,在下面的字段控制类中定义。 在该示例中,重写的GetValidatedString方法中还包含一些自定义验证,以确保在强制性时选择一个值。

2.用户控件渲染模板 (2. User Control Rendering Template)

In order to render the drop down control, a user control is required, consisting of an XML rendering template and a code behind file.

为了呈现下拉控件,需要一个用户控件,该控件由XML渲染模板和文件背后的代码组成。

The rendering templated is defined within an .ascx file, and in the example is little more than an asp dropdownlist control, as follows:

模板化的渲染是在.ascx文件中定义的,在此示例中仅是一个asp dropdownlist控件,如下所示:

<%@ Control Language="C#" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %>

<SharePoint:RenderingTemplate ID="OrganisationDropDownFieldControl" runat="server">
  <Template>
    <asp:DropDownList ID="OrganisationSelector" runat="server" />
  </Template>
</SharePoint:RenderingTemplate> 

The ID of the rendering template element matches the name of the code behind class for this user control, while the ID of the drop down list matches the name of the DropDownList property within the control class.

呈现模板元素的ID与该用户控件的类背后的代码名称匹配,而下拉列表的ID与控件类中的DropDownList属性的名称匹配。

3.现场控制课 (3. Field Control Class)

The field control class is the code behind file for the rendering template, and is compiled alongside the Field Class in the same assembly. It is this class that is used to populate the drop down list with its data, as well as handling the current value.

字段控件类是呈现模板文件后面的代码,并且在同一程序集中与字段类一起编译。 该类用于用其数据填充下拉列表以及处理当前值。

using System;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Company.Application.Controls
{
    [CLSCompliant(false)]
    [Guid("dfbce0dc-3215-4352-b65f-25a83e8b427b")]
    public class OrganisationDropDownFieldControl : BaseFieldControl
    {
        protected DropDownList OrganisationSelector;

        protected override string DefaultTemplateName
        {
            get
            {
                return "OrganisationDropDownFieldControl";
            }
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return OrganisationSelector.SelectedValue;
            }
            set
            {
                EnsureChildControls();
                OrganisationSelector.SelectedValue = (string)this.ItemFieldValue;
            }
        }

        protected override void CreateChildControls()
        {
            if (Field == null || ControlMode == SPControlMode.Display)
                return;

            base.CreateChildControls();

            OrganisationSelector = (DropDownList)TemplateContainer.FindControl("OrganisationSelector");

            if (OrganisationSelector == null)
            {
                throw new ApplicationException("Error: Cannot load .ascx file");
            }

            if (OrganisationSelector.Items.Count == 0)
            {
                if (SPContext.Current.Site != null)
                {
                    SPList orgList = SPContext.Current.Site.RootWeb.Lists["Organisations List"];
                    if (orgList == null)
                    {
                        throw new ApplicationException("Organisations List not found");
                    }

                    OrganisationSelector.Items.Add(String.Empty);
                    foreach (SPItem org in orgList.Items)
                    {
                        if (org["Title"] == null)
                        {
                            continue;
                        }
                        string orgTitle = org["Title"].ToString();
                        OrganisationSelector.Items.Add(new ListItem(orgTitle, orgTitle));
                    }
                }
            }
        }
    }
}

The class must inherit from BaseFieldControl (or one of the classes in Windows SharePoint Services that derive from it), and be modified as follows:

该类必须继承自BaseFieldControl(或从其派生的Windows SharePoint Services中的类之一),并进行如下修改:

"      Include a .Net control within its properties of the desired type, with a name matching the ID of the corresponding control within the template. The example field includes a DropDownList control called OrganisationSelector.

在所需类型的属性中包括一个.Net控件,其名称与模板中相应控件的ID匹配。示例字段包括一个称为OrganisationSelector的DropDownList控件。

"      Override the DefaultTemplateName property with the name of rendering template

“使用呈现模板的名称覆盖DefaultTemplateName属性

"      Override the value property to enable the getting and setting of the current value of the control.

“覆盖value属性,以启用控件的当前值的获取和设置。

"      Override the CreateChildControls method to populate the control. For the example control, this is where the drop down list is populated with values from the Organisations List.

重写CreateChildControls方法以填充控件。对于示例控件,在这里使用组织列表中的值填充下拉列表。

This class must be compiled into the same assembly as the Field Class.

该类必须编译为与Field Class相同的程序集。

4.字段类型定义文件 (4. Field Type Definition File)

The final requirement for the custom control is a field type definition file, which holds the metadata behind the field itself.

自定义控件的最终要求是字段类型定义文件,该文件将元数据保存在字段本身后面。

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">OrganisationDropDownField</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">Organisation</Field>
    <Field Name="TypeShortDescription">Organisation</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="ShowInListCreate">TRUE</Field>
    <Field Name="ShowInSurveyCreate">TRUE</Field>
    <Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
    <Field Name="ShowInColumnTemplateCreate">TRUE</Field>
    <Field Name="SQLType">ntext</Field>
    <Field Name="FieldTypeClass">Company.Application.Controls.OrganisationDropDownField, Company.Application.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7389d5f4596c3c69 </Field>
    <RenderPattern Name="DisplayPattern">
      <Switch>
        <Expr>
          <Column/>
        </Expr>
        <Case Value="">
        </Case>
        <Default>
        </Default>
      </Switch>
    </RenderPattern>
  </FieldType>
</FieldTypes>

The TypeName element must match the name of the field control, while ParentType must match the SPField subtype used within the Field Class. Note the FieldTypeClass element that must match the name of the Field Class itself, along with its assembly details.

TypeName元素必须与字段控件的名称匹配,而ParentType必须与在Field Class中使用的SPField子类型匹配。 请注意,FieldTypeClass元素必须与Field Class本身的名称及其程序集详细信息匹配。

5.部署自定义字段类型 (5. Deploying the custom field types)

The Field Class and Field Control Class must be compiled into a signed assembly, which must be placed in the GAC ( a custom field binary cannot be installed into the Application bin directory).

必须将Field Class和Field Control Class编译成一个已签名的程序集,该程序集必须放在GAC中(不能将自定义字段二进制文件安装到Application bin目录中)。

In order to have the relevant files deployed automatically by the solution, the rendering template and field type definition file must be added to the VS project using the same folder path as will be used by SharePoint.

为了使解决方案自动部署相关文件,必须使用SharePoint将使用的相同文件夹路径将呈现模板和字段类型定义文件添加到VS项目中。

Therefore, add a new folder under the VS project with a name of 12. Below that, add another folder called Template and within that add two more folders: ControlTemplates and XML. Place the rendering template in the ControlTemplates folder, and add the field type definition file to the XML folder.

因此,在VS项目下添加一个名称为12的新文件夹。在其下,添加另一个名为Template的文件夹,并在其中添加另外两个文件夹:ControlTemplates和XML。 将呈现模板放置在ControlTemplates文件夹中,然后将字段类型定义文件添加到XML文件夹中。

Next add a manifest.xml file that will tell sharepoint where to deploy the individual components, as follows:

接下来,添加一个manifest.xml文件,该文件将告诉sharepoint各个组件的部署位置,如下所示:

<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="F3A12E2E-31EF-478d-AC2A-E389704F281E">
  <Assemblies>
    <Assembly Location="Company.Application.Controls.dll" DeploymentTarget="GlobalAssemblyCache">
    </Assembly>
  </Assemblies>
  <TemplateFiles>
    <TemplateFile Location="XML\fldtypes_OrganisationField.xml" />
    <TemplateFile Location="ControlTemplates\OrganisationFieldControl.ascx" />
  </TemplateFiles>
</Solution>

Note the Assembly element that holds the name of the dll, with a DeploymentTarget of the GAC. The Template files entry will place the rendering template and field definition files in their relevant locations within the sharepoint hierarchy

请注意,包含dll名称和GAC的DeploymentTarget的Assembly元素。 模板文件条目会将渲染模板和字段定义文件放置在共享点层次结构中的它们的相关位置中

Because the assembly has been placed in the GAC, it will need to be manually added to the SafeControls section of each application using the control.

由于程序集已放置在GAC中,因此需要使用控件将其手动添加到每个应用程序的SafeControls部分。

<SafeControl Assembly="Company.Application.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7389d5f4596c3c69", Namespace="Company.Application.Controls" TypeName="*" Safe="True" />

Finally add a definition file to automatically include all relevant files into a .wsp SharePoint solution package.

最后,添加定义文件以自动将所有相关文件包含到.wsp SharePoint解决方案包中。

;WSP CAB Generation
.Set DiskDirectoryTemplate=CDROM
.Set CompressionType=MSZIP
.Set UniqueFiles=Off
.Set Cabinet=On

Manifest.xml
%assembly%

.Set DestinationDir="XML"
12\Template\XML\fldtypes_OrganisationField.xml

.Set DestinationDir="ControlTemplates"
12\Template\ControlTemplates\OrganisationFieldControl.ascx

This package can then be deployed using the STSADM utility.

然后可以使用STSADM实用程序部署此软件包。

SPField subtypes

SPField子类型

"      Microsoft.SharePoint.SPFieldAttachments    

“ Microsoft.SharePoint.SPFie ldAttachme nts

"      Microsoft.SharePoint.SPFieldBoolean    

“ Microsoft.SharePoint.SPFie ld布尔

"      Microsoft.SharePoint.SPFieldCalculated    

“ Microsoft.SharePoint.SPFie ld计算 编

"      Microsoft.SharePoint.SPFieldComputed    

“ Microsoft.SharePoint.SPFie ld计算

"      Microsoft.SharePoint.SPFieldCrossProjectLink    

“ Microsoft.SharePoint.SPFie ldCrossPro jectLink

"      Microsoft.SharePoint.SPFieldDateTime    

“ Microsoft.SharePoint.SPFie ldDateTime

"      Microsoft.SharePoint.SPFieldFile    

“ Microsoft.SharePoint.SPFie ld文件

"      Microsoft.SharePoint.SPFieldLookup    

“ Microsoft.SharePoint.SPFie ld查询

"      Microsoft.SharePoint.SPFieldMultiChoice    

“ Microsoft.SharePoint.SPFie ldMultiCho 冰

"      Microsoft.SharePoint.SPFieldMultiColumn    

“ Microsoft.SharePoint.SPFie ldMultiCol n

"      Microsoft.SharePoint.SPFieldMultiLineText    

“ Microsoft.SharePoint.SPFie ldMultiLin 电子文本

"      Microsoft.SharePoint.SPFieldNumber    

“ Microsoft.SharePoint.SPFie ldNumber

"      Microsoft.SharePoint.SPFieldPageSeparator    

“ Microsoft.SharePoint.SPFie ldPageSepa 执行者

"      Microsoft.SharePoint.SPFieldRecurrence    

“ Microsoft.SharePoint.SPFie ldRecurren 铈

"      Microsoft.SharePoint.SPFieldText    

“ Microsoft.SharePoint.SPFie ldText

"      Microsoft.SharePoint.SPFieldUrl

“ Microsoft.SharePoint.SPFie ldUrl

翻译自: https://www.experts-exchange.com/articles/307/How-to-create-and-deploy-a-custom-field-type-within-Sharepoint.html

sharepoint 部署

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值