码迷,mamicode.com
首页 > Windows程序 > 详细

Rest Api how to retrieve list items from SharePoint Online

时间:2015-06-03 11:53:02      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:rest

We will learn how to call Rest Api in SharePoint Online, here is the requirment:

There is a customer list named "Customers", we will output the title of all items in the list.

Introduce Rest Api:

The request examples in this article assume that you’re using the cross-domain library (SP.RequestExecutor.js) to make cross-domain requests, so they use SP.AppContextSite in the endpoint URI. See How to: Access SharePoint 2013 data from apps using the cross-domain library for more information.

技术分享

Note: make sure implement the sp.requestextcutor.js in page.

 Implement in detail:

1. create a custom list

技术分享

2. New a project in Napa, add a page and implement  the js

'use strict';

var currentcontext;
var hostcontext;
var hostweb;
var hostUrl;
var appUrl;

(function() {

	// This code runs when the DOM is ready and creates a context object which is 
	// needed to use the SharePoint object model
	$(document).ready(function() {
		hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
		currentcontext = new SP.ClientContext.get_current();
		hostcontext = new SP.AppContextSite(currentcontext, hostUrl);
		hostweb = hostcontext.get_web();
        appUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
		
		getListItems();	
	});

	function getListItems() {
		var executor = new SP.RequestExecutor(appUrl);
		executor.executeAsync({
			url: appUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Customers')/getitems?@target='" + hostUrl + "'",
			method: "POST",
			body: "{ 'query' : {'__metadata': { 'type': 'SP.CamlQuery' }, 'ViewXml': '<View><Query><Where></Where></Query></View>' } }",
			headers: {
				"accept": "application/json; odata=verbose",
				"content-type": "application/json; odata=verbose"
			},
			success: function(data, req, text) {
				console.log(JSON.parse(data.body));
				var result = JSON.parse(data.body).d.results;
				for (var i = result.length - 1; i >= 0; i--) {
					$(".mainContainer").append("<div>" + result[i].Title + "</div>");
				}
			},
			error: function(data, req, text) {
				console.log(data);
			}
		});
	}
})();


3. Debug the project, you will see the result



More:Lists and list items REST API reference










Rest Api how to retrieve list items from SharePoint Online

标签:rest

原文地址:http://blog.csdn.net/tristan_dong/article/details/46342461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!