2013年10月30日 星期三

How to use ListData.svc To Access SharePoint 2013 in Window Form (如何使用ListData.svc存取SharePoint 2013)

ListData.svc 是 WCF Data Service提供在SharePoint 2013對外的其中一個服務,此服務最主要的目的是讓外部應該程式可以透過ListData.svc快速存取或控管SharePoint 2013的清單(List)中的資料,此ListData.svc 在SharePoint 2010時開始可以使用,目前sharePoint 2013雖然有提供諸多的其他REST的API來存取,但是使用ListData.svc 依舊可以快速取用清單資料!

好處是假設以往已經有SharePoint 2010 的客製化應該程式,有使用此ListData.svc 服務,那麼在升級至SharePoine 2013時,也可以快速無縫升級!

以下來介紹開發步驟,首先我們先來看一下在SharePoint 2013上的ListData.svc ,路徑為http://sp/sites/IntroSpApps/_vti_bin/ListData.svc

image

1.先建立一個Windows Application 應用程式,然後把http://sp/sites/IntroSpApps/_vti_bin/ListData.svc加入服務參考,並且更改Namespace名稱,如以下畫面:

image

2.加入參考後,會自動產生對應的proxy,以及相關的assembly,如:

Microsoft.Data.Edm、Microsoft.Data.OData、Microsoft.Data.Service.Client等等

image

3. 打開Windows 專案的Data Sources視窗,可以看到根據SharePoint 2013指定網站所產生出來的清單

image

4. 接著我們只想要針對其中一個清單進行存取,新增一個Data Source,選擇Object

image

5.挑選MyListItem (在SharePoint網站上清單名稱為:MyList)

image

6.完成後,可以看到清單中完整的欄位相關資料,如下圖所示,

image

7.接著,我們想要在Windows Form存取清單資料,可以直接從Data Source將MyListItem拖曳至Form1中,此時工具會自動產生對應的DataGridView等控制項

image

8. 先建立DataContext物件,且在Form_Load中加入載入SharePoint 2013 清單資料的相關程式碼:

image

執行測試,可以看到已經順利透過將ListData.svc 將資料取出到Windows Form應用程式中

image

9.假設想要在Windows Form應用程式中維護MyList的資料的話,可以透過ListData.svc 將資料同步更新回SharePoint 2013 ,程式碼如下:

image

在SharePoint 2013 上MyList資料的呈現!

image