CRUD Operations using JavaScript in SharePoint-Hosted Add-In (Lists)

You can either continue this article or use the below code in your own way. This code is written in Apps.js file.

Below are the sample JavaScript codes for lists operations:

var hostWebUrl;
var appWebUrl;
var ctx ;
var appCtxSite;
var web;

$(document).ready(function () {
    hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
    appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
    ctx = new SP.ClientContext(appWebUrl);
    appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
    web = appCtxSite.get_web();
    getAllLists();
});

// function to delete a list
function deleteList() {
    var list = web.get_lists().getByTitle('My Contacts');
    list.deleteObject();
    ctx.executeQueryAsync(
        Function.createDelegate(this, function () { alert("List deleted successfully");}),
        Function.createDelegate(this, this.onFail));
}

//function to update an existing list
function updateList() {
    var list = web.get_lists().getByTitle('My Contacts');
    list.set_description('My Personal Contacts');
    list.update();
    ctx.load(list);
    ctx.executeQueryAsync(Function.createDelegate(this, function () {
        alert("Title: " + list.get_title() + " , Description: " + list.get_description());
    }), Function.createDelegate(this, this.onFail));
}

// function to create new list in the host web 
function createList()
{
    var listInfo = new SP.ListCreationInformation();
    listInfo.set_title('My Contacts');
    listInfo.set_templateType(SP.ListTemplateType.contacts);
    this.list = web.get_lists().add(listInfo);
    ctx.load(list);
    ctx.executeQueryAsync(
        Function.createDelegate(this, function () {
            alert(this.list.get_title() + " new list created!");
        }),
        Function.createDelegate(this, this.onFail));
}

// function to get all List names in the host web
function getAllLists() {
    this.listColl = web.get_lists();
    ctx.load(this.listColl);
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.onGetAllListsSucceed),
        Function.createDelegate(this, this.onFail));
}

// success method of getAllLists function
function onGetAllListsSucceed() {
    //var list = '';
    var listEnumerator = this.listColl.getEnumerator();
    while(listEnumerator.moveNext()){
        var list = listEnumerator.get_current();
        document.getElementById("message").innerHTML = document.getElementById("message").innerHTML + list.get_title() + "</br>";
    }
}

// generic Fail method
function onFail()
{
    alert("Failed!" + arguments[1].get_message());
}

// function to get the specific list properties
function getSpecificListProperties() {
    this.listColl = web.get_lists();
    ctx.load(listColl, 'Include(Title, Id)');
    ctx.executeQueryAsync(
        Function.createDelegate(this, this.onGetSelectedListProperties),
        Function.createDelegate(this, this.onFail));
}

// success method of getSpecificListProperties function
function onGetSelectedListProperties()
{
    var listEnumerator = this.listColl.getEnumerator();
    while (listEnumerator.moveNext()) {
        var list = listEnumerator.get_current();
        document.getElementById("message").innerHTML = document.getElementById("message").innerHTML +
            list.get_title() + " -- " + list.get_id().toString() + "</br>";
    }
}

// function to manage the query string parameters and return SPHostURL or SPAppWebUrl as per the request.
function manageQueryStringParameter(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];
        }
    }
}

App Package (.APP)

A SharePoint App is deployed as App Package file with .APP as an extension. SharePoint Solutions used to have .WSP as an extension and are .CAB files. SharePoint Apps are not .CAB files, they are simply .ZIP files. Just replace .APP extension with .ZIP. Open this folder in Windows Explorer and you will many files under it including AppManifest file and other files needed for app deployment.

The AppManifest file is an XML file and the most important one as it contains lots of information like App title, unique id, start page security settings etc. Below is a sample of the AppManifest file.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<!–Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9–>
<App xmlns=”http://schemas.microsoft.com/sharepoint/2012/app/manifest&#8221;
Name=”SampleSharePointApp”
ProductID=”{78abc125-ef63-6789-abcd-1ab23ef7878b}”
Version=”1.0.0.0″
SharePointMinVersion=”15.0.0.0″>
<Properties>
<Title>Sample SharePoint App</Title>
<StartPage>~appWebUrl/Pages/Default.aspx?{StandardTokens}</StartPage>
</Properties>
<AppPrincipal>
<Internal />
</AppPrincipal>
</App>

When an app is deployed to the SharePoint Site, it appears on the Site Contents page. There are three types of web included in an app: host web, app web and remote web.

Host Web

Every app will have a host web. Host Web refers to the SharePoint Site in which app will be installed. Host Web contains elements like:

  • App Parts: These are Client Web Parts same as Web Parts. But their logic does not reside on server. Their logic reside on client web browser or on remote web site.
  • Custom Actions: These are custom ribbon and menu options.
  • Redirect Page: When a user navigates to and fro from an app, this redirect page help user to redirect to the appropriate page in an app. This is based on the AppManifest file’s configuration.

App Web

App Web is a separate SharePoint site that is deployed to host the components of an app. It included lists, libraries, SharePoint Pages, Style Sheets and Client Side Scripts. SharePoint Pages does not contain the server side code blocks, but only the server controls.

App Web is deployed to SharePoint farm but not in the same domain as rest of the site. T is deployed using a specialized site template, App Site Template. Pages of this site use app.master master page by default.

Remote Web

Remote Web is normal ASP.NET web site which is designed to deploy outside of SharePoint like in Windows Azure. Here server-side code is allowed. Remote Web can make secure calls to host web and app web whenever needed.

NOTE: Remote Web does not run within SharePoint and hence does not have access to SharePoint Server Object Model. It uses SharePoint’s client-side APIs. CSOM for .NET can be used by server-side code in remote web and JavaScript CSOM is available to the pages served from a remote site.

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started