
- SharePoint 教程
- SharePoint - 首頁
- SharePoint - 概述
- SharePoint - 型別
- SharePoint - 功能
- SharePoint - 設定環境
- SharePoint - 建立網站集
- SharePoint - API
- SharePoint - 中心管理
- SharePoint - 應用模型
- SharePoint - 整合選項
- SharePoint - 開發工具
- SharePoint - 列表功能
- 其他列表功能
- SharePoint - 自定義列表
- SharePoint - 庫
- SharePoint - Web 部件
- 網站列和內容型別
- SharePoint - 資料
- SharePoint - 伺服器物件模型
- SharePoint - 客戶端物件模型
- SharePoint - REST API
- SharePoint - 功能和元素
- SharePoint - 功能/事件接收器
- SharePoint - Azure 平臺
- SharePoint - 打包和部署
- SharePoint - 沙盒解決方案
- SharePoint - 應用
- SharePoint 有用資源
- SharePoint - 快速指南
- SharePoint - 資源
- SharePoint - 討論
SharePoint - 伺服器物件模型
在本節中,我們將瞭解 SharePoint 伺服器物件模型。當您編寫將在 SharePoint 上下文中執行的程式碼時,可以使用 SharePoint 伺服器物件模型。一些常見的示例包括頁面或 Web 部件中的程式碼隱藏、功能或列表後面的事件處理程式、計時器作業等。
伺服器物件模型的功能
以下是伺服器物件模型的關鍵功能
如果您正在同一個應用程式池中程式設計 ASP.NET 應用程式(SharePoint 使用該應用程式池),則可以使用伺服器物件模型。
如果您正在開發客戶端應用程式(例如控制檯或 Windows 窗體或 WPF 應用程式,這些應用程式將在 SharePoint 伺服器上執行),則可以使用伺服器物件模型。
您無法使用伺服器物件模型遠端連線到 SharePoint 伺服器。
當您想要使用伺服器物件模型時,請引用 **Microsoft.SharePoint** 程式集。還有其他程式集構成了伺服器物件模型,但 Microsoft.SharePoint 是主要的程式集。
您最常使用的核心型別對映到您作為終端使用者使用的元件,因此諸如網站集、網站、列表、庫和列表項之類的內容由 SPSite、SPWeb、SPList、SPDocumentLibrary 和 SPListItem 型別表示。
表示網站集的型別和伺服器物件模型是 SPSite,表示 SharePoint 網站的伺服器物件模型型別是 SPWeb。因此,當您從終端使用者術語轉換為開發人員術語時,只需進行這種心理對映即可。
現在,當您第一次開始使用 SharePoint 時,可能會感到困惑,因為“網站”一詞含義非常廣泛,在終端使用者和開發人員的詞彙表中具有相反的含義,更不用說 Web 詞彙表了。
讓我們來看一個伺服器物件模型的簡單示例。
**步驟 1** - 開啟 Visual Studio 並從 **檔案 → 新建 → 專案** 選單選項建立一個新專案。
**步驟 2** - 在左側窗格中從 **模板 → Visual C#** 選擇 Windows,然後在中間窗格中選擇控制檯應用程式。輸入專案的名稱,然後單擊“確定”。
**步驟 3** - 建立專案後,右鍵單擊解決方案資源管理器中的專案,然後選擇 **新增 → 引用**。

**步驟 4** - 在左側窗格中選擇 **程式集 → 擴充套件**,然後在中間窗格中選中 Microsoft.SharePoint,然後單擊“確定”按鈕。
現在再次右鍵單擊解決方案資源管理器中的專案,然後選擇“屬性”。

**步驟 5** - 單擊左側窗格中的 **生成** 選項卡,然後取消選中 **首選 32 位** 選項。

**步驟 6** - 現在返回到 **Program.cs** 檔案,並將其替換為以下程式碼。
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SharePointData { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; Console.WriteLine(web.Title); var lists = web.Lists; foreach (SPList list in lists) { Console.WriteLine("\t" + list.Title); } Console.ReadLine(); } } } }
**注意** - 在上面的程式碼中,首先建立了一個新的 SPSite 物件。這是一個可處置物件,因此它是在 using 語句中建立的。SPSite 建構函式接受網站集的 URL,在您的情況下,該 URL 將有所不同。
var **web = site.RootWeb** 將獲取網站集的根目錄。
我們可以使用 web.Lists 獲取列表並列印列表項的標題。
編譯並執行上述程式碼後,您將看到以下輸出:
SharePoint Tutorials appdata Authors Composed Looks Contacts Course Documents Courses Documents List Template Gallery Master Page Gallery Site Assets Site Pages Solution Gallery Style Library Theme Gallery User Information List Web Part Gallery
您可以看到這些標題是解決方案庫、樣式庫、表單模板。這些是 SharePoint 內部使用的列表。因此,與其顯示所有列表,不如只顯示使用者通常會看到的列表。
因此,與其獲取整個列表集合,不如獲取所有未隱藏的列表。我們可以使用如下所示的連結查詢來實現。
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; Console.WriteLine(web.Title); var lists = from SPList list in web.Lists where list.Hidden == false select list; foreach (SPList list in lists) { Console.WriteLine("\t" + list.Title); } Console.ReadLine(); } } } }
編譯並執行上述程式碼後,您將看到以下輸出:
SharePoint Tutorials Authors Contacts Course Documents Courses Documents Site Assets Site Pages Style Library
您可以看到,這將返回所有未隱藏的列表。
讓我們來看另一個簡單的示例,我們還將在其中顯示有關列表項的一些資訊。
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; Console.WriteLine(web.Title); var lists = from SPList list in web.Lists where list.Hidden == false select list; foreach (SPList list in lists) { Console.WriteLine("\t" + list.Title); var items = list.Items.OfType<SPListItem>().Take(5); var isDocLib = list is SPDocumentLibrary; foreach (SPListItem item in items) { var value = isDocLib ? item.Name : item.Title; Console.WriteLine("\t\t" + value); } } Console.ReadLine(); } } } }
編譯並執行上述程式碼後,您將看到以下輸出:
SharePoint Tutorials Authors Muhammad Waqas Mark Upston Allan Bommer Andy Onian Contacts Waqas Upston Bommer Course Documents Sample1.docx Sample2.docx Sample3.docx Courses SharePoint Tutorials C# Tutorials ASP.Net Tutorials NHibernate Tutorials Documents Site Assets Site Pages Home.aspx How To Use This Library.aspx Style Library
列表資料
當您第一次建立列表時,它始終具有一個標題列。預設情況下,此標題列允許訪問列表項上下文或編輯控制元件塊選單。
由於每個列表都以一個列(標題)開頭,因此 **SPListItem** 型別將其公開為屬性。對於並非每個列表都通用的列,您可以透過 **SpListItem** 型別的索引器訪問它們。
您可以向索引器傳遞幾條資訊,但最常見的是 **列**。列表設定中的終端使用者可以更改此名稱。您不希望使用此名稱,因為該名稱可能會發生更改。
第二個是 **內部名稱**,它是在建立此列表時設定的,並且永遠不會更改。當您訪問列值時,這是您要使用的名稱。
讓我們來看一個簡單的示例,我們將在其中檢索“作者”列表,如下所示:

在此示例中,我們將獲取“作者”列表,然後將“工資/費率”提高某個值。因此,對於“工資/費率”列,我們將使用 **內部名稱**。
**步驟 1** - 轉到伺服器資源管理器;右鍵單擊 **SharePoint 連線**,然後選擇“新增連線...” 指定 URL,然後單擊“確定”。

**步驟 2** - 展開 **SharePoint 教程 → 列表庫 → 列表 → 作者 → 欄位 → 工資/費率** 欄位。右鍵單擊 **工資/費率**,然後選擇“屬性”。您將在“屬性”視窗中看到 **內部名稱**。

**步驟 3** - 下面是一個根據“工資/費率”檢索“作者”並提高其“工資/費率”的簡單示例。
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; var list = web.Lists.TryGetList("Authors"); if (list == null) return; var items = list.Items; ShowItems(items); RaiseRates(items); Console.WriteLine("\nAfter Raise\n"); ShowItems(items); Console.ReadKey(); } } static void RaiseRates(SPListItemCollection items) { foreach (SPListItem item in items) { var employee = Convert.ToBoolean(item["Employee"]); var rate = Convert.ToDouble(item["Salary_x002f_Rate"]); var newRate = employee ? rate + 1 : rate + 0.1; item["Salary_x002f_Rate"] = newRate; item.Update(); } } static void ShowItems(SPListItemCollection items) { foreach (SPListItem item in items) { Console.WriteLine("Salary or rate for {0} is {1:c}", item.Title, item["Salary_x002f_Rate"]); } } } }
在上面的程式碼中,您可以看到我們有兩個方法:
一個是檢索列表,稱為 **ShowItems**,並且
另一個方法是提高費率,稱為 **RaiseRates()**。
編譯並執行上述程式碼後,您將看到以下輸出:
Salary or rate for Muhammad Waqas is $950.00 Salary or rate for Mark Upston is $15.00 Salary or rate for Allan Bommer is $20.00 Salary or rate for Andy Onian is $870.00 After Raise Salary or rate for Muhammad Waqas is $951.00 Salary or rate for Mark Upston is $15.10 Salary or rate for Allan Bommer is $20.10 Salary or rate for Andy Onian is $871.00
CAML 查詢
在上面的示例中,我們始終使用 foreach 迴圈遍歷專案,並且多次遍歷所有專案,並且我們始終都取回所有列,或者至少所有列都是可訪問的。
這實際上類似於在 SQL 查詢中執行 select* from 表名。
我們可以透過使用所謂的 **CAML 查詢** 來解決此問題。執行 CAML 查詢時,您有兩個選項:
如果只想查詢單個列表,可以使用 SPQuery 物件。
如果要查詢網站集中的多個列表,則可以使用 SPSiteDataQuery。
通常,當您執行 **SPSiteDataQuery** 時,您正在查詢特定型別的全部列表。
例如,我想查詢所有聯絡人列表等。SPSiteDataQuery 允許您確定範圍,因此您可以指示要查詢整個網站集、單個網站或網站及其所有子網站。
CAML 查詢的語法基本上是用 XML 格式描述的,需要一些時間才能習慣於構建此類查詢。
讓我們來看一個 CAML 查詢的簡單示例。在這裡,我們將建立一個 CAML 查詢來查詢“作者”列表中的資料。
using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerObjectModel { class Program { static void Main(string[] args) { using (var site = new SPSite("http://waqasserver/sites/demo")) { var web = site.RootWeb; var list = web.Lists.TryGetList("Authors"); if (list == null) return; var items = QueryItems(list); ShowItems(items); //RaiseRates(items); //Console.WriteLine("\nAfter Raise\n"); //ShowItems(items); Console.ReadKey(); } } static SPListItemCollection QueryItems(SPList list) { var query = new SPQuery(); query.ViewFields = "<FieldRef Name='Title' />" + "<FieldRef Name='Employee' />" + "<FieldRef Name='Salary_x002f_Rate' />"; query.Query = "<OrderBy>" + " <FieldRef Name='Salary_x002f_Rate' />" + "</OrderBy>" + "<Where>" + " <Eq>" + " <FieldRef Name='Employee' />" + " <Value Type='Boolean'>False</Value>" + " </Eq>" + "</Where>"; return list.GetItems(query); } static void RaiseRates(SPListItemCollection items) { foreach (SPListItem item in items) { var employee = Convert.ToBoolean(item["Employee"]); var rate = Convert.ToDouble(item["Salary_x002f_Rate"]); var newRate = employee ? rate + 1 : rate + 0.1; item["Salary_x002f_Rate"] = newRate; item.Update(); } } static void ShowItems(SPListItemCollection items) { foreach (SPListItem item in items) { Console.WriteLine("Salary or rate for {0} is {1:c}", item.Title, item["Salary_x002f_Rate"]); } } } }
我們使用了 CAML 查詢來獲取一些專案。在 **QueryItems** 方法中,您可以看到我們只檢索了那些不是“員工”的專案。
Salary or rate for Mark Upston is $15.10 Salary or rate for Allan Bommer is $20.10