本文讲述如何在SharePoint 2013/2010 中使用Silverlight client object model.
SharePoint 2013/2010 提供了client object model,涵盖了多种版本: .net, Javascript, Silverlight等。2013 增加一个新的Object Model: Microsoft.SharePoint.Client.Phone.dll.
所有这些Client Model的Dll都可以在C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\ClientBin中找到。另外2013增加了Client object model 对Workflow, UserProfiles, TranslationServices,Search,Taxonomy 等services 的操作支持。
这里主要讲的Silverlight client object model, Silverlight OM一般有两种用途:
1. 开发Silverlight 的webpart
2. 与 Silverlight application集成,在Silverlight application 中访问SharePoint 对象
以上两种除安全性和Web part支持属性设置外,代码上没有太大的区别。
Web part传给silverlight 的参数可以在编辑webpart 属性时设置在"Custom Initialization Parameters",以逗号分隔
在silverlight代码中下列代码来获取初始化参数
webUrl = HttpUtility.UrlDecode(App.Current.Host.InitParams["WebUrl"]);
安全性上,Silverlight application使用Silverlight OM的安全性要求更高,需要配置ClientAccessPolicy.xml (存放到对应的网站的根目录下 如C:\inetpub\wwwroot\wss\VirtualDirectories\80)
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Silverlight 的webpart 因为也是由SharePoint web appliation 所host,因此不需要以上设置。
如果Silverlight application不做安全设置将引发如下错误:
{System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at Microsoft.SharePoint.Client.ClientRuntimeContext.SynchronousExecutor.Invoke()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at SilverlightFlexi.MainPage.CreateListCallback(Object s)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}
言归正卷, 这里说下Silverlight application种使用Silverlight OM的大致步聚:
1. 配置 目标Moss 网站的 ClientAccessPolicy.xml
2. 在Silverlight application 工程中引入 Microsoft.SharePoint.Client.Silverlight.dll, Microsoft.SharePoint.Client.Silverlight.Runtime.dl(如果需要使用Workflow, UserProfiles, TranslationServices,Search,Taxonomy 等services,也需要引入其他dll 如Microsoft.SharePoint.Client.Taxonomy.Silverlight.dll)
3. 在Silverlight application中创建一个测试页面
MainPage.xaml:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<RichTextBox x:Name="logTextBox" ></RichTextBox>
</Grid>
</UserControl>
MainPage.xaml.csMainPage.xaml.csMainPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
namespace SilverlightApplication1
{
public partial class MainPage : UserControl
{
Site site;
Web web;
ListCollection collList;
IEnumerable<List> listInfo;
public MainPage()
{
InitializeComponent();
ClientContext clientContext = new ClientContext("http://mossserver/");
clientContext.PendingRequest.WebRequest.UseDefaultCredentials = true;
site = clientContext.Site;
web = site.OpenWeb("");
// 在使用SharePoint client端的对象是都要先load,然后执行ExecuteQueryAsync, 否则或引发"property not initialized" 错误
clientContext.Load(site);
clientContext.Load(web);
collList = web.Lists;
// 可以用Include,Except来 过滤字段, Where 过滤记录
listInfo = clientContext.LoadQuery(collList.Include(
list => list.Title,
list => list.Fields.Include(
field => field.Title).Where(
field => field.Required == true
&& field.Hidden != true)));
clientContext.ExecuteQueryAsync(succeededCallback, failedCallback);
}
private void failedCallback(object sender, ClientRequestFailedEventArgs args)
{
MessageBox.Show(args.ErrorDetails.ToString());
}
private void succeededCallback(object sender, ClientRequestSucceededEventArgs args)
{
AppendTextToLogTextBox("web.Title:" + web.Title);
AppendTextToLogTextBox("web.Url:" + web.Url);
foreach (var list in listInfo)
{
StringBuilder sb = new StringBuilder();
sb.Append(list.Title);
foreach (var field in list.Fields)
{
sb.Append(";");
sb.Append(field.Title);
}
AppendTextToLogTextBox(sb.ToString());
}
}
private void AppendTextToLogTextBox(string message)
{
// Deployment.Current.Dispatcher 是Silverlight中的线程模型中提供的一个安全的基于事件的调度器,所以它一般被后台线程所调用,用于操作UI界面(一般只能由UI线程操作)
// 如果不采用这种方式,而直接在UI线程意外的线程去更新UI,会引发“cross-thread access exception”
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Paragraph prgParagraph = new Paragraph();
Bold bldText = new Bold();
bldText.Inlines.Add(new Run() { Text = message });
prgParagraph.Inlines.Add(bldText);
this.logTextBox.Blocks.Add(prgParagraph);
});
}
}
}