首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

SharePoint 2010 ListData.svc 實戰:WCF 查詢、新增、更新、刪除列表資料

     編輯:關於.NET

SharePoint 2010 內建 RESTful WCF 服務 ListData.svc(位於 /_vti_bin/ListData.svc),支援 LINQ 查詢與 CRUD 操作。本文以「客戶」與「城市」兩個列表為例,示範在 Console 應用程式中加入服務參考後的完整操作範式。

一、準備工作

  • 建立 SharePoint 網站,新增「城市」列表(欄位:城市、國家)與「客戶」列表(欄位:客戶、城市〈查閱至城市列表〉)。
  • Visual Studio 專案 → 加入服務參考 → 位址輸入 https:///_vti_bin/ListData.svc → 命名空間 ListDataService。
加入服務參考

二、初始化 DataContext


static ListDataService.工作組網站DataContext Ctx;
static void Main()
{
    Ctx = new ListDataService.工作組網站DataContext(
        new Uri("https://sp2010u/it/_vti_bin/listdata.svc"));
    Ctx.Credentials = System.Net.CredentialCache.DefaultCredentials; // Windows 整合驗證
    // 或 Ctx.Credentials = new NetworkCredential("user", "pwd", "domain");
}

三、查詢資料(LINQ to REST)


// 關聯查詢:客戶 + 城市 + 國家
static void ListCustomerAndCityDetails()
{
    var q = from c in Ctx.客戶
            where c.城市 != null && c.城市.國家 != null
            select new { c.客戶, City = c.城市.城市, Country = c.城市.國家 };
    foreach (var r in q)
        Console.WriteLine($"客戶 {r.客戶} 生活在 {r.Country} 的 {r.City}");
}

// 簡單查詢
static void ListCustomers()
{
    foreach (var c in Ctx.客戶)
        Console.WriteLine($"{c.Id} - 客戶 {c.客戶}");
}

Fiddler 捕獲的實際 HTTP 請求:


GET /it/_vti_bin/listdata.svc/客戶()?$filter=(城市 ne null) and (城市/國家 ne null)
    &$expand=城市&$select=客戶,城市/城市,城市/國家
Fiddler 抓包

OData 查詢選項($filter、$expand、$select)自動轉換為 REST URL,極具可讀性。

四、新增資料


static void AddCustomer(string cityName, string customerName)
{
    var city = Ctx.城市.Where(c => c.城市 == cityName).FirstOrDefault()
             ?? BuildNewCity(cityName); // 不存在則建立

    var client = new ListDataService.客戶Item
    {
        城市 = city,
        城市Id = city.Id, // 必須同時指定導覽屬性與 FK
        客戶 = customerName
    };
    Ctx.AddTo客戶(client);
    Ctx.SaveChanges(); // 送出 POST 請求
}

static ListDataService.城市Item BuildNewCity(string name)
{
    var city = new ListDataService.城市Item { 城市 = name };
    Ctx.AddTo城市(city);
    Ctx.SaveChanges();
    return Ctx.城市.Where(c => c.城市 == name).FirstOrDefault();
}

五、刪除資料


static void DeleteCustomer(int id)
{
    try
    {
        var client = Ctx.客戶.Where(c => c.Id == id).FirstOrDefault();
        if (client != null)
        {
            Ctx.DeleteObject(client);
            Ctx.SaveChanges(); // 送出 DELETE 請求
        }
    }
    catch (DataServiceQueryException)
    {
        Console.WriteLine("找不到該客戶!");
    }
}

六、更新資料


static void ModifyCustomerSetNewCity(int id, string newCityName)
{
    var client = Ctx.客戶.Where(c => c.Id == id).FirstOrDefault();
    if (client == null) throw new ApplicationException("目標客戶不存在!");

    var city = Ctx.城市.Where(c => c.城市 == newCityName).FirstOrDefault()
             ?? BuildNewCity(newCityName);

    client.城市 = city;
    client.城市Id = city.Id; // 同新增:雙重指定
    Ctx.UpdateObject(client); // 標記為修改
    Ctx.SaveChanges(); // 送出 MERGE/PUT 請求
}

七、關鍵注意事項

  1. 查閱欄位雙重指定:設定導覽屬性(client.城市)與外鍵 ID(client.城市Id)兩者皆需賦值,否則更新不生效。
  2. 認證:預設使用 Windows 整合驗證;跨網域/非網域環境需傳遞 NetworkCredential。
  3. 錯誤處理:SaveChanges 可能拋出 DataServiceRequestException,內含批次回應細節,需解析 ResponseBatch 取得具體錯誤。
  4. 分頁:預設單次回傳 1000 筆,大量資料需迴圈讀取 DataServiceQuery<T>.Skip/Take。

結語

ListData.svc 為 SharePoint 2010 提供了標準化、可程式化的資料存取介面,配合 LINQ 語法極大降低開發門檻。掌握 OData 查詢選項、實體狀態管理(Add/Update/DeleteObject)、查閱欄位雙重指定規則,即可高效實現各類整合案例。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved