天天看點

使用SharePoint對象SPFieldCollection擷取清單所有字段資訊

使用SharePoint對象模型的SPFieldCollection對象可以很容易的擷取到某個清單SPList中所有定義的字段資訊,主要包括資訊:字段類型(TypeDisplayName)、内部名稱(InternalName)、顯示名稱(Title,在字段内部,顯示名稱使用Title表示)、SchemaXml、ID等資訊。通過這些資訊可以具體的了解到此清單中字段的詳細資訊,可以更容易的通過對象模型操作此清單。

下面我們通過一段簡單的代碼,将通知(Announcements)清單中的所有定義的字段資訊使用GridView控件呈現出來。C#代碼如下:

01

using

System;

02

using

System.Web.UI;

03

using

System.Web.UI.WebControls;

04

using

System.Web.UI.WebControls.WebParts;

05

06

using

System.Collections.Specialized;

07

using

Microsoft.SharePoint;

08

09

namespace

Example.SharePoint.SPField

10

{

11

public

partial

class

SPFieldWebPartUserControl : UserControl

12

{

13

protected

void

Page_Load(

object

sender, EventArgs e)

14

{

15

}

16

17

/// <summary>

18

/// 根據SPWeb,清單名稱擷取清單中的所有字段

19

/// </summary>

20

/// <param name="web"></param>

21

/// <param name="listName"></param>

22

/// <returns></returns>

23

protected

SPFieldCollection GetAllSPFieldInfo(SPWeb web, 

string

listName)

24

{

25

SPFieldCollection fieldColl = 

null

;

26

if

(web != 

null

&& !

string

.IsNullOrEmpty(listName))

27

{

28

SPList list = web.Lists[listName];

29

fieldColl = list.Fields;

30

}

31

return

fieldColl;

32

}

33

34

35

/// <summary>

36

/// 送出

37

/// </summary>

38

/// <param name="sender"></param>

39

/// <param name="e"></param>

40

protected

void

Button1_Click(

object

sender, EventArgs e)

41

{

42

this

.gV.DataSource =

this

.GetAllSPFieldInfo(SPContext.Current.Web,

"Announcements"

);

43

this

.gV.DataBind();

44

}

45

}

46

}

上面的代碼中,主要是使用了SPList.Fields來擷取此清單中所有的字段,傳回的集合類型為:SPFieldCollection,這是一個集合類型,可以直接将此類型綁定到ASP.NET的GridView控件并顯示出來。

在這裡要注意,我們在按鈕事件中是使用了SPContext.Current.Web來擷取此代碼運作的目前網站。這樣做的好處是以後不管此代碼運作在任何的SharePoint環境中不需要做任何修改即可正常運作。

還有一點就是我們在上面的代碼中,使用了:

SPFieldCollection fieldColl = null;

來存儲SPList.Fields擷取的所有字段集合,這樣可以提高性能,将所有的字段暫存到SPFieldCollection集合類型中,這樣每次調用時不用再重新擷取。大大提高了讀取的速度。

上面的代碼是編寫在一個叫做SPFieldWebPart的可視Web部件中,使用者控件ASCX布局代碼如下:

01

<

div

>

02

03

<

asp:Button

ID

=

"Button1"

runat

=

"server"

Text

=

"Submit"

OnClick

=

"Button1_Click"

/></

div

>

04

05

<

div

>

06

07

<

asp:GridView

ID

=

"gV"

AutoGenerateColumns

=

"false"

runat

=

"server"

>

08

09

<

Columns

>

10

11

<

asp:BoundField

DataField

=

"TypeDisplayName"

HeaderText

=

"TypeDisplayName"

/>

12

13

<

asp:BoundField

DataField

=

"InternalName"

HeaderText

=

"InternalName"

/>

14

15

<

asp:BoundField

DataField

=

"Title"

HeaderText

=

"Title"

/>

16

17

<

asp:BoundField

DataField

=

"Scope"

HeaderText

=

"Scope"

/>

18

19

<

asp:BoundField

DataField

=

"ID"

HeaderText

=

"ID"

/>

20

21

<

asp:BoundField

DataField

=

"SchemaXml"

HeaderText

=

"SchemaXml"

/>

22

23

</

Columns

>

24

25

</

asp:GridView

>

26

27

</

div

>

将上面的可視Web部件使用VS2010部署到SharePoint環境中,如圖1所示:

使用SharePoint對象SPFieldCollection擷取清單所有字段資訊

圖1

圖1的截圖由于大小原因沒有截取完全,但可以從圖1上看出,我們想要的字段資訊已經列出來了,特别是前3個資訊:顯示名稱、内部名稱、字段類型,這些都是操作清單字段的必須資訊。

另外還有一個資訊也是特别重要的,它就是SchemaXml,圖1沒有截取到,由于此資訊太長了,我們将其中一個資訊找出來,如下:

<Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Type="Text" Name="Title" DisplayName="Title" Required="TRUE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Title" FromBaseType="TRUE" ColName="nvarchar1"/>

這段是Title字段的SchemaXml資訊,其中包括了Title字段的ID、類型、名稱、顯示名稱、是否必填、靜态名稱、基類型、以及在SQLServer資料庫中的清單。其實字段的SchemaXml就是SharePoint字段的定義内容。

繼續閱讀