西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開(kāi)發(fā)其它知識(shí) → SharePoint 2013 怎么自定義字段圖文教程

SharePoint 2013 怎么自定義字段圖文教程

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:霖雨時(shí)間:2013/12/7 17:41:29字體大小:A-A+

作者:霖雨點(diǎn)擊:13次評(píng)論:0次標(biāo)簽: SharePoint2013

  • 類型:翻譯工具大。4.4M語(yǔ)言:多國(guó)語(yǔ)言[中文] 評(píng)分:8.3
  • 標(biāo)簽:
立即下載

SharePoint使用的優(yōu)勢(shì),就在于開(kāi)箱即用、快速搭建,SharePoint自身為我們提供了很多字段類型,已經(jīng)很豐富了。但是,在實(shí)際應(yīng)用中,我們還需要一些功能特殊的字段,下面,我們簡(jiǎn)單介紹下字段的開(kāi)發(fā),大家了解以后,可以按照需求擴(kuò)展自己的字段類型。

1、新建項(xiàng)目,選擇SharePoint 2013 空項(xiàng)目,如下圖:

2、選擇調(diào)試網(wǎng)站和解決方案類型,如下圖:

3、添加新項(xiàng),類,這個(gè)是用來(lái)定義字段的,如下圖:

4、添加新項(xiàng),類,這個(gè)是用來(lái)編寫字段展示的,如下圖:

5、添加映射文件夾,如下圖:

6、選擇映射文件夾,這個(gè)文件夾,添加的是CustomFieldControl.cs的前臺(tái)文件,如下圖:

7、添加映射文件夾,選擇Xml,這個(gè)是字段的描述文件,如下圖:

8、為xml目錄下添加一個(gè)xml文件,用來(lái)寫字段的描述文件,如下圖:

9、在CONTROLTEMPLATES文件夾下,添加用戶控件,用來(lái)寫CustomFieldControl.cs的前臺(tái)文件,因?yàn)檫@樣,比較好進(jìn)行字段展示,如下圖:

10、刪除沒(méi)用的cs文件,最后的如下圖

11、為字段類CustomField.cs添加方法,如下圖:

12、字段類CustomField.cs完整代碼,有點(diǎn)長(zhǎng),關(guān)鍵代碼有注釋,如下:

CustomField Class

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Web;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using Microsoft.SharePoint;
10 using Microsoft.SharePoint.WebControls;
11
12 namespace SP2013CustomField
13 {
14 class CustomField : SPFieldText
15 {
16 public CustomField(SPFieldCollection fields, string fieldName)
17 : base(fields, fieldName)
18 {
19 }
20
21 public CustomField(SPFieldCollection fields, string typeName, string displayName)
22 : base(fields, typeName, displayName)
23 {
24 }
25
26 public override string DefaultValue //設(shè)置字段的默認(rèn)值
27 {
28 get
29 {
30 return "http://";
31 }
32 }
33
34 public override BaseFieldControl FieldRenderingControl //關(guān)聯(lián)字段展示控件
35 {
36 get
37 {
38 BaseFieldControl fc = new CustomFieldControl();
39 fc.FieldName = this.InternalName;
40 return fc;
41 }
42 }
43
44 public override string GetValidatedString(object value)//驗(yàn)證字段是否符合要求
45 {
46 string StartStr = this.GetCustomProperty("CustomFieldProperty").ToString().ToLower();//獲得字段屬性
47 string StartValue = string.Empty;
48 if (value.ToString().Length > StartStr.Length)
49 {
50 StartValue = value.ToString().ToUpper().Substring(0, StartStr.Length).ToLower();
51 }
52 // this.Required是否必填項(xiàng)的值
53 if (this.Required == true || value == null || StartStr != StartValue)
54 {
55 throw new SPFieldValidationException("該字段必須以" + StartStr + "開(kāi)頭");//將不符合要求的錯(cuò)誤拋出來(lái),以小紅字顯示在欄的下面
56 }
57 return base.GetValidatedString(value);
58 }
59 }
60 }

13、為字段展示控件類CustomFieldControl.cs添加方法,如下圖:

14、附CustomFieldControl.cs完整代碼,如下:

CustomFieldControl Class

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Web;
7 using System.Web.UI;
8 using System.Web.UI.WebControls;
9 using Microsoft.SharePoint;
10 using Microsoft.SharePoint.WebControls;
11
12 namespace SP2013CustomField
13 {
14 class CustomFieldControl : BaseFieldControl
15 {
16 public TextBox tbStart;
17 public Image myImage;
18
19 //獲取控件的值
20 public override object Value
21 {
22 get
23 {
24 EnsureChildControls();
25 if (tbStart != null)
26 {
27 return tbStart.Text;
28 }
29 else
30 {
31 return null;
32 }
33 }
34 set
35 {
36 EnsureChildControls();
37 if (tbStart != null)
38 {
39 tbStart.Text = (String)value;
40 }
41 }
42 }
43
44 //重寫默認(rèn)模板
45 protected override string DefaultTemplateName
46 {
47 get
48 {
49 if (this.ControlMode == SPControlMode.Display)
50 {
51 return this.DisplayTemplateName;
52 }
53 else
54 {
55 return "DefaultCustomFieldControl";
56 }
57 }
58 }
59
60 public override string DisplayTemplateName
61 {
62 get
63 {
64 return "DisplayCustomFieldControl";
65 }
66 set
67 {
68 base.DisplayTemplateName = value;
69 }
70 }
71
72 //重寫控件生成方法
73 protected override void CreateChildControls()
74 {
75 base.CreateChildControls();
76 if (this.Field != null)
77 {
78 this.myImage = (Image)TemplateContainer.FindControl("myImage");
79 this.tbStart = (TextBox)TemplateContainer.FindControl("tbStart");
80 }
81 if (this.ControlMode == SPControlMode.Display)
82 {
83 string strHeight = base.Field.GetCustomProperty("Height").ToString();
84 string strWidth = base.Field.GetCustomProperty("Width").ToString();
85 if (myImage != null)
86 {
87 myImage.ImageUrl = this.ItemFieldValue.ToString();
88 myImage.Width = Convert.ToInt32(strWidth);
89 myImage.Height = Convert.ToInt32(strHeight);
90 }
91 }
92 }
93 }
94 }

15、CustomFieldControl.cs類的前臺(tái)文件,如下圖:

16、CustomFieldControl.cs前臺(tái)文件完整代碼,如下:

<%@ Control Language="C#" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:RenderingTemplate ID="DefaultCustomFieldControl" runat="server">
    <Template>
        <asp:TextBox ID="tbStart" runat="server" />
    </Template>
</SharePoint:RenderingTemplate>
<SharePoint:RenderingTemplate ID="DisplayCustomFieldControl" runat="server">
    <Template>
        <asp:Image ID="myImage" runat="server" />
    </Template>
</SharePoint:RenderingTemplate>

17、設(shè)置字段的描述文件,主要是字段的定義、字段屬性,如下圖:

18、字段描述文件完整xml,如下:

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">自定義單行文本</Field>
    <Field Name="ParentType">Text</Field>
    <Field Name="TypeDisplayName">必須有特定標(biāo)識(shí)開(kāi)頭的單行文本</Field>
    <Field Name="TypeShortDescription">自定義單行文本</Field>
    <Field Name="UserCreatable">TRUE</Field>
    <Field Name="ShowOnListCreate">TRUE</Field>
    <Field Name="ShowOnSurveyCreate">TRUE</Field>
    <Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
    <Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
<Field Name="FieldTypeClass">SP2013CustomField.CustomField, SP2013CustomField, Version=1.0.0.0, Culture=neutral, PublicKeyToken=42c0b47fe35d0f54</Field>
//字段屬性,如下
    <PropertySchema>
      <Fields>
        <Field Name="CustomFieldProperty" DisplayName="設(shè)置起始標(biāo)識(shí)" MaxLength="255" Type="Text"></Field>
        <Field Name="Height" DisplayName="圖片高度" MaxLength="255" Type="Text"></Field>
        <Field Name="Width" DisplayName="圖片寬度" MaxLength="255" Type="Text"></Field>
      </Fields>
    </PropertySchema>
  </FieldType>
</FieldTypes>

19、在列表里添加欄,可以添加屬性,如下圖:

20、新建一條項(xiàng)目,圖片欄的驗(yàn)證,如下圖:

21、展示頁(yè)面,如下圖:

22、查看項(xiàng)目頁(yè)面,不顯示url,在圖片控件中顯示,如下圖:

總 結(jié)

自定義字段,主要有字段定義、字段控件、字段控件前臺(tái)、字段描述文件等組成,其中,字段前臺(tái)文件并非必須,可以添加Render將控件輸出,但是不好控制排版,所以復(fù)雜的字段需要前臺(tái)展示。

其開(kāi)發(fā)過(guò)程也不復(fù)雜,基本就是搭建開(kāi)發(fā)模型,將各個(gè)部分創(chuàng)建,然后為各個(gè)部分添加代碼,建議先編寫簡(jiǎn)單控件,部署沒(méi)有問(wèn)題再添加復(fù)雜功能,以免出錯(cuò)不好調(diào)試。當(dāng)然,調(diào)試附加相應(yīng)w3wp.exe進(jìn)程即可。

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過(guò)難過(guò)
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)