Client Object Model

Source: Internet
Author: User
Tags silverlight
Client Object Model

We know that Microsoft SharePoint 2010 provides three new client APIs that allow you to run scripts in your browser.. net.. NET Framework 3.5 or later) or code executed in the Microsoft Silverlight 2.0 APPLICATION interacts with the SharePoint website.
Here we will look at how to introduce the ecmascript (JavaScript, JScript) Client object model in the SharePoint website.
The first is a few notes:
1. The ecmascript object model can only be used for Sharepoint sites. That is to say, you cannot use the ecmascript object model of the traditional Asp.net website to access SharePoint website resources across websites.
2. Similarly, you cannot use the ecmascript object model on a Sharepoint site to access resources of other SharePoint sites across websites. (For example, access resources on another SharePoint website URL: yoursite.yoursharepoint.com through the ecmascript object model)
3. You can use jquery and ecmascript object models directly without additional preparation work. You only need to reference jquery. js in your SharePoint project.

4. When you use the ecmascript object model on the webpart page or application page (ASPX page) of the SharePoint website, you only need to reference sp. js. <SharePoint: scriptlink
Name = "sp. js" runat = "server" OnDemand = "true"
Localizable = "false"
/>
5. If you plan to use the ecmascript object model to modify resources on the SharePoint site, you need to add the formdigest tag. <SharePoint: formdigest
Runat = "server"
/>

6. Execute your JavaScript code only after sp. JS is loaded.
Sometimes the JavaScript code you want to execute is based on the sp. js file. If sp. JS is not loaded, the JavaScript code you define will report an error. Therefore, we need human intervention to ensure that sp. JS is loaded before the JavaScript function we define. To achieve this, we need to use the executeordelayuntilscriptloaded function. The Code is as follows:

Executeordelayuntilscriptloaded (myjsfucntion, "sp. js ");

7. Use jquery in the ecmascript Object Model
The ecmascript object model does not conflict with jquery. You only need to add jquery references. The only thing you need to note is that if you want to execute Javascript in the page load event, you must place this JavaScript function segment in executeordelayuntilscriptloaded (as shown above)

If you want to add jquery, refer to SharePoint Study Notes-delegate control -- add jquery on the SharePoint page

 

You can use the new client object model to retrieve specific Sharepoint client objects (clientobject, or in ecmascript (JavaScript, JScript ).
Clientobject (the link may point to an English page), or retrieves sub-objects and their associated attributes or sub-items in the set. The object model uses explicit data retrieval semantics, which must be followed when retrieving normal data. The client object model does not retain the data retrieved locally on the client.

Data Retrieval involves the following processes:

  1. Use clientcontext () to construct a function (javascript:
    Clientcontext (serverrelativeurl) (the link may point to an English page) returns the context of the client request.

  2. Use the object model to specify the object to be retrieved or the object from which to retrieve data.

  3. Objects or collections can be loaded by local loading or query loading, and load <t> (T, []) (javascript:
    Load (clientobject) (this link may point to an English page) method to return specific objects, sets, or data; query, load, and use
    Loadquery () (javascript:
    Loadquery (clientobjectcollection, exp) (this link may point to an English page) method returns an enumerated set of objects.

  4. Call the synchronous executequery () method in the. NET managed version of the client object model to execute the query, or in the Silverlight version, when the code never modifies the user interface
    When the (UI) thread executes the query. In the Javascript version of the object model, or when the Silverlight version modifies the UI, the data retrieval process uses an asynchronous callback model, and you must call
    Executequeryasync (clientrequestsucceedeventhandler, clientrequestfailedeventhandler) method (javascript:
    Executequeryasync (succeededcallback, failedcallback) (this link may point to an English page). This method includes method parameters used to process successful or failed requests. When the query method is returned (synchronous or asynchronous), you can access the query results of the objects from which the query is initiated.

Local Loading and queryable Loading

You can use the following two methods to query the client object model: Local Loading and queryable loading. You can query and load the returned data in another result array, and load the data in the object itself to return the data.

In-Place loading will send a request to load the object and its data, and retain the object identity through multiple requests. You can use the client Context
The load <t> (T, []) method executes local loading. The following example loads all lists of the current website and all its default attributes. After the query is executed, the code can access the entire list set and default list attributes:

C # copy
clientContext.Load(clientContext.Web.Lists); clientContext.ExecuteQuery();

In queryload, the query is separated from the returned results. Unlike local loading, object identifiers are not retained during query loading. Therefore, your code must maintain the returned objects. The following example uses the query syntax to return all the lists of websites.

C # VB Replication
var query = from list     in clientContext.Web.Lists     select list;var result = clientContext.LoadQuery(query);clientContext.ExecuteQuery();

 

Data Retrieval:

Http://msdn.microsoft.com/zh-cn/library/ee539350 (V = office.14). aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.