Client Object Model
我们知道Microsoft SharePoint 2010 提供了三个新的客户端 API,这些 API 允许您通过在浏览器中执行的脚本、在 .NET 托管应用程序中执行的代码(Microsoft .NET Framework 3.5 或更高版本)或在 Microsoft Silverlight 2.0 应用程序中执行的代码与 SharePoint 网站进行交互。
这里我们来看看如何在Sharepoint网站中引入ECMAScript(JavaScript、JScript) 客户端对象模型。
首先是几点说明:
1、ECMAScript对象模型只能用于Sharepoint站点。也就是说你不能通过传统的Asp.net网站使用ECMAScript对象模型来跨网站访问Sharepoint网站资源。
2、与上面类似,你也不能在一个Sharepoint站点上使用ECMAScript对象模型来跨网站访问其它Sharepoint网站的资源。(如:从Sharepoint网站 url: MySite.MySharepoint.com通过ECMAScript对象模型去访问另一个Sharepoint网站 url: YourSite.YourSharepoint.com上的资源)
3、你可以把JQuery与ECMAScript对象模型直接结合使用而无需多余的其它准备工作。你只需要在你的Sharepoint项目中引用JQuery.js就行了。
4、当你在Sharepoint网站的Webpart页面或者application页面(aspx page)上使用ECMAScript对象模型时,你只需要引用SP.js就可以了。<SharePoint:ScriptLink
Name="SP.js" runat="server" OnDemand="true"
Localizable="false"
/>
5、如果你打算通过ECMAScript对象模型来修改Sharepoint网站上的相关资源,你需要加入FormDigest标签。<SharePoint:FormDigest
runat="server"
/>
6、 在sp.js被加载后才执行你的JavaScript代码。
有时你想要执行的Javascript代码功能会基于sp.js文件,如果sp.js没有加载,你所定义的Javascript代码就会报错。因此,我们需要人为介入,保证sp.js在我们定义的Javascript功能之前先行加载。 为达到此目的,我们需要使用ExecuteOrDelayUntilScriptLoaded功能。代码如下
ExecuteOrDelayUntilScriptLoaded(myjsfucntion, "sp.js");
7、 在ECMAScript对象模型中使用JQuery
ECMAScript对象模型与JQuery没有任何冲突,你只需要加入JQuery的引用即可,唯一需要注意的就是如果你想要在Page Load Event中执行Javascript,你必须把这个 Javascript的功能段放置到ExecuteOrDelayUntilScriptLoaded中(如上面所示)
如果添加JQuery可以参考Sharepoint学习笔记—Delegate Control--在Sharepoint页面添加jQuery
可以使用新的客户端对象模型来检索特定 SharePoint 客户端对象(ClientObject,或者 ECMAScript(JavaScript、JScript) 中的
ClientObject (该链接可能指向英文页面))的属性,或检索子对象及其关联的属性或集合中的子项。对象模型使用显式数据检索语义,在检索正常工作的数据时,必须遵循这些语义。客户端对象模型不保留在客户端上本地检索的数据。
执行数据检索涉及以下过程:
-
使用 ClientContext() 构造函数 (JavaScript:
ClientContext(serverRelativeUrl) (该链接可能指向英文页面)) 返回客户端请求的上下文。
-
使用对象模型指定要检索的对象或要从中检索数据的对象。
-
通过就地加载或可查询加载来加载对象或集合,就地加载使用 Load<T>(T, []) (JavaScript:
load(clientObject) (该链接可能指向英文页面)) 方法返回特定的对象、集合或数据;可查询加载使用
LoadQuery() (JavaScript:
loadQuery(clientObjectCollection, exp) (该链接可能指向英文页面)) 方法返回对象的可枚举集合。
-
通过在客户端对象模型的 .NET 托管版本中调用同步 ExecuteQuery() 方法执行查询,或者在 Silverlight 版本中,当代码从不修改用户界面
(UI) 的线程执行时,执行查询。在对象模型的 JavaScript 版本中,或者当 Silverlight 版本修改 UI 时,数据检索过程将使用异步回调模型,并且您必须调用
ExecuteQueryAsync(ClientRequestSucceededEventHandler, ClientRequestFailedEventHandler) 方法 (JavaScript:
executeQueryAsync(succeededCallback, failedCallback) (该链接可能指向英文页面)),该方法包括用于处理成功或失败请求的方法参数。当执行查询的方法调用返回(同步或异步)时,您可以访问从中启动查询的对象上的查询结果。
可通过两种方法对客户端对象模型使用基于 LINQ 的查询:通过就地加载和通过可查询加载。可查询加载在另一个结果数组中返回数据,而就地加载在对象本身中返回数据。
就地加载会发出加载对象及其数据的请求,并通过多个请求保留对象标识。可以通过客户端上下文的
Load<T>(T, []) 方法执行就地加载。以下示例加载当前网站的所有列表及其所有默认属性。执行查询后,代码将可以访问整个列表集合和默认的列表属性:
clientContext.Load(clientContext.Web.Lists);
clientContext.ExecuteQuery();
在可查询加载中,查询与返回的结果分隔开。与就地加载不同,在可查询加载中,不会保留对象标识,因此您的代码必须自己维护返回的对象。以下示例使用查询语法返回网站的所有列表。
var query = from list
in clientContext.Web.Lists
select list;
var result = clientContext.LoadQuery(query);
clientContext.ExecuteQuery();
数据检索:
http://msdn.microsoft.com/zh-cn/library/ee539350(v=office.14).aspx
分享到:
相关推荐
For lhqdyy9 Extract InfoPath Forms Data To Excel Using The .NET Client Object Model
Infragistic UltraWebGrid _Client-Side Object Model – Grid Object UltraWebGrid客户端对象模型手册 转自:vbchina.net
DOM Scripting: Web Design with JavaScript and the Document Object Model gives you everything you need to start using JavaScript and the Document Object Model to enhance your web pages with client-...
ECMAScript Client Object Model . 152 Client Object Model by Examples 158 Lists and Items . 159 Document Libraries and Files 165 SOAP Services 168 The REST API 171 Querying for Data with .NET and LINQ ...
Microsoft Silverlight 5 Building Rich Enterprise Dashboards Chapter 1, The Silverlight Technology, is an overview of the Silverlight...SharePoint Client Object Model to access data hosted in SharePoint.
使用ClientObjectModel操纵SharePoint的数据,详情请关照博客http://blog.csdn.net/ghostbear
Chapter 5: Client Object Model, ADO.NET Services, and Custom Services Chapter 6: Managing SharePoint Data: Part 1 Chapter 7: Managing SharePoint Data: Part 2 Chapter 8: Enterprise Content Management ...
This causes problems when you want a connected object model in your server, but you can't send that object model to the client because the serialization library will try to send the entire object ...
Clientside JavaScript and CSS Reference Introduction Concepts Object Model List of Files Customizing Known Issues References/Documentation Links
Chapter 8: The Browser Object Model .201 Chapter 9: Client Detection 229 Chapter 10: The Document Object Model 261 Chapter 11: DOM Levels 2 and 3 .317 Chapter 12: Events .365 Chapter 13: Scripting ...
oriented programingpowerful aspects of function expressionsBrowser Object Model allowing interaction with the browser itselfdetecting the client and its capabilitiesDocument Object Model (DOM) objects...
///Serves as the base object for all the editors included in the client-side object model. /// /// ///Gets the unique, hierarchically-qualified identifier for the control. Value: The fully-...
Browser Object Model allowing interaction with the browser itself detecting the client and its capabilities Document Object Model (DOM) objects available in DOM Level 1 how DOM Levels 2 and 3 ...
Windows Management Instrumentation (WMI) Remote Protocol is a Distributed Component Object Model (DCOM), as specified in [MS-DCOM], a client/server–based framework that provides an open and automated...
Chapter 30: Concurrency, distribution, client-server and the Internet 951 30.1 A SNEAK PREVIEW 951 30.2 THE RISE OF CONCURRENCY 953 30.3 FROM PROCESSES TO OBJECTS 956 30.4 INTRODUCING CONCURRENT ...
歡迎來到Client/Server的世界 ...瞭解Transaction Model 正確的使用資料集元件(TTable,TQuery和TStoredProc) Cached Update和UpdateSQL的建立 企業物件(business object) 未來Client/Server架構的演變
The Client's Model of Object Creation Exceptions Object Implementations Providing an Implementation Interface versus Implementation Hierarchies Implementation Type Checking Implementing a Server Using...
LWM2M defines a simple object model and a number of interfaces and operations for device management. This document mainly introduces how to use the LWM2M function of Quectel BC95 module through AT ...
Server Data Scenarios Populating a Document with Data on the Server Using ServerDocument and ASP.NET A Handy Client-Side ServerDocument Utility The ServerDocument Object Model ...
Server Data Scenarios Populating a Document with Data on the Server Using ServerDocument and ASP.NET A Handy Client-Side ServerDocument Utility The ServerDocument Object Model ...