Content Editor Webpart(三)使用JSOM

来源:互联网 发布:java源文件的扩展名是 编辑:程序博客网 时间:2024/06/05 19:27

JSOM是SharePoint 提供的一种客户端API。开发人员只需要使用Javescript,就可以实现和SharePoint的交互,非常方便。


首先按照 (Content Editor Webpart(一)引用JQuery) 中的说明,引入JQuery。然后在Content Editor中添加代码。

比如要获取site的title 和description。

function retrieveWebSite(siteUrl) {    var clientContext = new SP.ClientContext(siteUrl);    this.oWebsite = clientContext.get_web();    clientContext.load(this.oWebsite);    clientContext.executeQueryAsync(        Function.createDelegate(this, this.onQuerySucceeded),         Function.createDelegate(this, this.onQueryFailed)    );}


和Server OM不同的是,客户端OM,必须要先调用Load方法,再调用execute方法,才去与服务器交互。executeQueryAsync方法,定义了两个事件,一个是处理成功的情况,一个处理失败的情况。

比如,成功情况的处理函数为:

function onQuerySucceeded(sender, args) {    alert('Title: ' + this.oWebsite.get_title() +         ' Description: ' + this.oWebsite.get_description());}

完成的代码如下:

<div style="height: 200px;">   <script type="text/javascript" src="/sites/apps/Style%20Library/jquery-1.10.2.min.js"></script><script>            function retrieveWebSite(siteUrl) {    var clientContext = new SP.ClientContext(siteUrl);    this.oWebsite = clientContext.get_web();    clientContext.load(this.oWebsite);    clientContext.executeQueryAsync(        Function.createDelegate(this, this.onQuerySucceeded),         Function.createDelegate(this, this.onQueryFailed)    );}function onQuerySucceeded(sender, args) {    alert('Title: ' + this.oWebsite.get_title() +         ' Description: ' + this.oWebsite.get_description());}    function onQueryFailed(sender, args) {    alert('Request failed. ' + args.get_message() +         '\n' + args.get_stackTrace());}   </script>    <button id="#getInfo" onclick="retrieveWebSite(‘https://server/sites/site/internal’)">Get Site Info </button></div>


做完之后的效果:



0 0
原创粉丝点击