SharePoint 2013 APP开发示例 (一)List 读写 - 面向对象网,web开发,Sharepoint,学习,对象 - 面向对象技术开发

面向对象技术开发

会员投稿 投稿指南 站长资讯通告:
您的位置: 首页 > Web开发 > SharePoint > 正文

SharePoint 2013 APP开发示例 (一)List 读写

来源: www.bianceng.cn 阅读:

在这个示例里,我们将创建一个页面测试 SharePoint APP的权限。这个页面有二个按钮,一个从documents里读数据,一个往documents里写数据:

1. 打开Visual Studio 2012,创建一个新的 Sharepoint 2013 app: PermissionTest,选择 Sharepoint-hosted,点击Finish

\

2. 打开Default.aspx :

引入knockoutjs

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js" ></script>

加入二个按钮:

<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
   App Permission Test
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderMain" runat="server">
   <button data-bind="click: readList">Press here to read from Documents.</button>
   <br  />
   <button data-bind="click: writeList">Press here to write to Documents.</button>
</asp:Content>

3. 打开APP.js, 加入下面js:

/// <reference path="knockout-3.0.0.debug.js"  />
$(function () {
    ko.applyBindings(new testPermissionsViewModel());
});
   
function testPermissionsViewModel() {
    var self = this;
    self.result = null;
   
    self.readList = function () {
        var context = new SP.ClientContext.get_current();
        var hostWebContext = new SP.AppContextSite(context, decodeURIComponent(getQueryStringParameter("SPHostUrl")));
        self.result = hostWebContext.get_web().get_lists().getByTitle("Documents").getItems(new SP.CamlQuery.createAllItemsQuery());
        context.load(self.result);
        context.executeQueryAsync(
            Function.createDelegate(self, self.onSuccess),
            Function.createDelegate(self, self.onFail)
            );
    }
   
    self.writeList = function () {
        var context = new SP.ClientContext.get_current();
        var hostWebContext = new SP.AppContextSite(context, decodeURIComponent(getQueryStringParameter("SPHostUrl")));
        self.result = hostWebContext.get_web().get_lists().getByTitle("Documents").getItemById(1);
        self.result.set_item("Title", "My update book title");
        self.result.update();
        context.executeQueryAsync(
            Function.createDelegate(self, self.onSuccess),
            Function.createDelegate(self, self.onFail)
            );
    }
   
    self.onSuccess = function () {
        if (self.result instanceof SP.ListItemCollection) {
            if (!self.result.get_item(0)) {
                alert("Success but no return");
            }
            else {
                alert("Success, first title = " + self.result.get_item(0).get_item("Title"));
            }
        }
        else {
            alert("Success");
        }
    }
   
    self.onFail = function (sender, args) {
        alert("Failed: " + args.get_message());
    }
}
   
   
function getQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

Tags:
相关文章列表: