標籤:style blog http io ar color os 使用 sp
1、建立Silverlight時,選擇Silverlight 4,不要選擇版本5,試了很久版本5都調用不了,自己也不知道什麼原因,Google也沒找到答案,後來嘗試版本4,可以調用;
至於Host the Silverlight application是否勾選沒有影響;
2、Silverlight中使用用戶端指令碼,和Net中不一樣,要添加下面兩個引用;
3、兩個需要的引用,在伺服器上位置為C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\ClientBin,如:
4、然後在Silverlight的cs檔案中添加引用,如下:
using Microsoft.SharePoint.Client;
然後添加讀取List的代碼,如下:
public string url = "http://server:80";Web web;List list;ListItemCollection itemColl;string listName = "Silverlight";string camlStr = "";private void btn_ShowMessage_Click(object sender, RoutedEventArgs e){ getdata();}public void getdata(){ try { ClientContext context = new ClientContext(url); web = context.Web; list = web.Lists.GetByTitle(listName); CamlQuery query = new CamlQuery(); query.ViewXml = camlStr; itemColl = list.GetItems(query); context.Load(itemColl); context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnSuccessed), new ClientRequestFailedEventHandler(OnFailed)); } catch (Exception ex) { MessageBox.Show(ex.Message); }}private void OnSuccessed(Object sender, ClientRequestSucceededEventArgs args){ this.Dispatcher.BeginInvoke(LoadData);}private void LoadData(){ String data = string.Empty; foreach (ListItem item in itemColl) { data += item["Title"].ToString() + "\r\n"; } lb_Message.Content = data;}private void OnFailed(Object sender, ClientRequestFailedEventArgs args){ this.Dispatcher.BeginInvoke(delegate() { MessageBox.Show("Get Data Failed"); });}
5、前台頁面添加Silverlight展示WebPart,添加完畢如:
6、點擊ShowMessage,執行用戶端物件模型的方法,結果如:
總 結
自己對於Silverlight不是很熟悉,只是小有瞭解SharePoint,所以有問題之處還請大家指正。嘗試了很多次,才發現如何能在Silverlight中使用SharePoint用戶端物件模型,希望分享給有需要的人,給大家一個參考吧。
好了,就到這裡,休息。。休息一下。。
SharePoint 2013 Silverlight中使用Net用戶端物件模型