SharePoint 2013 App Development Sample (a) List read-write

Source: Internet
Author: User
Tags bind split visual studio

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]; }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.