(安西) 发表于 2015-9-28 12:32:42

Sharepoint ECMAScript

前言
  本文完全原创,转载请说明出处,希望对大家有用。
  本篇博客是个人总结,一方面以便日后查看,另一方面希望能为其他人提供一些便利。



阅读目录


[*]加载必要文件
[*](Get,Update,Delete,Add) ListItem
[*]使用SPQuery



正文


加载必要文件

在使用SharePoint ECMAScript之前,我们需要引入几个JS文件:


[*]  SP.js

[*]  SP.Core.js

[*]  SP.Runtime.js



  SP.js:包含主要的能够用来获取sharepoint数据的对象,如ClientContext、Web、List、Listitem等
  SP.Core.js:包含一些实用对象和帮助对象,能够用来完成基本的任务,如HTML编码、URL处理、XML字符串操作等
  SP.Runtime.js:包含了核心的ECMAScript运行时的对象,封装了客户端对象模型和服务器进行交互处理的所有细节
  SP.UI.<**>.js:一系列以SP.UI打头的文件提供了UI处理方面的扩展功能,例如SP.UI.Dialog.js包含了对Dialog Framework的支持
  SP.Ribbon.js:包含了对Ribbon开发的支持
  如果此时使用的是系统默认母板页,就不需要再次引用这些文件。如果使用的是自定义母板页,需要引入这些文件,或者在母板页里引用。
  SharePoint提供了js的Debug版本,便于开发,但正式环境需要引用非Debug版本。
  另外,如果有修改SharePoint Content Database数据库的代码,需要加入FormDigest:



<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="_Layouts/sp.runtime.js"></script>
<script type="text/javascript" src="_Layouts/sp.js"></script>
<script type="text/javascript">
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(function () {
var clientContext;
var listCreationInfo;
var web;
var list;
clientContext = SP.ClientContext.get_current();
web = clientContext.get_web();
list = web.get_lists().getByTitle('CustomList');
clientContext.load(list);
clientContext.executeQueryAsync(function () { alert("Success!") }, function () { alert("Request failed") });
}, "sp.js");
});
</script>
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
</asp:Content>
  


Manage ListItem
  GetItemByItemID



       function (itemid) {
var clientContext;
var listCreationInfo;
var web;
var list;
clientContext = SP.ClientContext.get_current();
web = clientContext.get_web();
list = web.get_lists().getByTitle('CustomList');;
This.item = list.getItemById(itemId);
clientContext.load(list);
clientContext.executeQueryAsync(function () { alert("Success!") }, function () { alert("Request failed") });
}
  Add a new item to list



                clientContext = SP.ClientContext.get_current();
web = clientContext.get_web();
list = web.get_lists().getByTitle('CustomList');
var listItemInfo = new SP.ListItemCreationInformation();
// add the item to the list
var listItem = list.addItem(listItemInfo);
listItem.set_item('Title', "Success");
This.item = list.getItemById(itemId);
var LookupTestValue = new SP.FieldLookupValue();
LookupTestValue.set_lookupId(employeeId);
listItem.set_item('LookupTest', LookupTestValue);
listItem.update();
clientContext.executeQueryAsync(function () { alert("Success!") }, function () { alert("Request failed") });
  Delete a item from list



            function deleteItem(itemId) {
var clientContext = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('CustomList');
var item = list.getItemById(itemId);
item.deleteObject();
clientContext.executeQueryAsync(function () { alert("Success!") }, function () { alert("Request failed") });
}

使用SPQuery



            function get_itemByQuery(title) {
try {
var clientContext = new SP.ClientContext.get_current();
this.web = context.get_web();
var list = this.web.get_lists().getByTitle('CustomList');
var query = '<View Scope=\'RecursiveAll\'>' +
'<Query>' +
'<Where>' +
'<Eq>' +
'<FieldRef Name=\'Title\' />' +
'<Value Type=\'LookUp\'>' + title + '</Value>' +
'</Eq>' +
'</Where>' +
'</Query>' +
'</View>';
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(query);
this.Queryitems = list.getItems(camlQuery);
clientContext.load(this.Queryitems);
clientContext.executeQueryAsync(function () { alert("Success!") }, function () { alert("Request failed") });
}
catch (e) {
alert("error occurred" + e.toString());
}
}
页: [1]
查看完整版本: Sharepoint ECMAScript