SharePoint with JQuery笔记1

加载JQuery:

将JS文件加载到SharePoint环境中的方法


SharePoint Client Object Model基础:

1. 使用Client Object Model之前需要加载sp.js文件,加载方法:

SharePoint 2010:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $(document).ready(function(){  
  2.     ExecuteOrDelayUntilScriptLoaded(MyFunction, "sp.js");  
  3. });  
  4.   
  5. function MyFunction(){ ... }  


SharePoint 2013:

  1. $(document).ready(function(){  
  2.     SP.SOD.executeFunc("sp.js", "SP.ClientContext", MyFunction);  
  3. });  
  4.   
  5. function MyFunction(){ ... }  


2. 使用ClientContext对象获取当前SharePoint信息:

     获取当前ClientContext对象:

  1. var context = new SP.ClientContext.get_current();  

     获取当前web对象:

  1. var web = context.get_web();  
  2. context.load(web);  
  3. context.executeQueryAsync(SucceedFunction, FailedFunction); //执行异步查询获取web的信息,SucceedFunction和FailedFunction是回调函数。  

     获取title为“abc”的列表:

  1. var web = context.get_web();  
  2. var list = web.get_lists().getByTitle("abc");  
  3. context.load(list);  
  4. context.executeQueryAsync(SucceedFunction, FailedFunction); //执行异步查询获取list的信息  

     获取id为1的item:

  1. var item = list.getItemById("1");  

     获取所有的item:

  1. var web = context.get_web();  
  2. var list = web.get_lists().getByTitle("abc");  
  3. var allItems = list.getItems("");  
  4. context.load(allItems, 'include(ID, Title)'); //使用include指定获取的field信息  
  5. context.executeQueryAsync(SucceedFunction, FailedFunction); //执行异步查询获取所有的item  

     遍历所有item:

  1. var itemCollection = allItems.getEnumerator();  
  2. while(itemCollection.moveNext())  
  3. {  
  4.     var myItem = itemCollection.get_current();  
  5.     ... ...  
  6. }  

     获取item的field值:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var title = myItem.get_item('Title'); //参数是field的internal name  

     更新item的field值:

  1. myItem.set_item('Title', 'new title');  
  2. myItem.update();  

     删除一个item:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. myItem.deleteObject();  
  2. context.executeQueryAsync(SucceedFunction, FailedFunction);  

      处理异步查询的异常:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. function FailedFunction(sender, args){ //使用args参数获得具体的错误信息  
  2.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  3. }  

     向list中添加一条item:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. var context = new SP.ClientContext.get_current();  
  2. var web = context.get_web();  
  3. var list = web.get_lists().getByTitle("abc");  
  4. var listItemInfo = new SP.ListItemCreationInformation(); //创建一个ListItemCreationInformation对象  
  5. var listItem = list.addItem(listItemInfo); //添加item  
  6. listItem.set_item('Title', 'new item'); //设置item的title  
  7. listItem.update();  
  8. context.executeQueryAsync(SucceedFunction, FailedFunction); //执行异步查询,在list中创建item  

 

     使用ajax调用web service,获取“Task”列表中的items,并且遍历显示Title字段:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. function LoadItems() {  
  2.     var taskUrl = "/_vti_bin/ListData.svc/Task/"; //如果Task列表位于子站点sub中,这里需要使用/sub/_vti_bin/ListData.svc/Task/  
  3.   
  4.     $.ajax({  
  5.         type: 'GET',   
  6.         url: taskUrl,  
  7.         dataType: 'json',  
  8.         success: function(data) { //成功的返回值是data对象,其中的d的results属性包含item  
  9.             $.each(data.d.results, function(i, result) {  
  10.                 alert('Title: '+result.Title);  
  11.                 });  
  12.             });  
  13.         },  
  14.         error: function() {  
  15.             alert("error");  
  16.         }  
  17.         });  
  18. }  


     使用ajax的getJSON方法获取一条item的数据:

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. function LoadItem(id) {  
  2.     var itemUrl = "/_vti_bin/ListData.svc/Task(" + id + ")";  
  3.     $.getJSON(itemUrl, function(data) {  
  4.         alert($(data.d.Title));  
  5.     });  
  6. }  



posted on 2015-09-10 11:44  !无名之辈  阅读(175)  评论(0)    收藏  举报