In this example, we will create a page to test the permissions of the SharePoint app. This page has two buttons, one reads data from documents and one writes data to documents:
1. Open Visual Studio 2012, create a new Sharepoint 2013 app:permissiontest, select sharepoint-hosted, click Finish
2. Open default.aspx:
Introduction of Knockoutjs
<script type= "Text/javascript" src= "Https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js" ></ Script>
Add two buttons:
<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. Open App.js, add the following 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, 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]; }
}