Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |

Wednesday, January 23, 2013

List Operations on Sharepoint Apps: using ECMAScript

Below content will help to guide you on below things:
  1. How to get the host web url? 
  2. How to check whether the list exists or not? 
  3. How to create a list? 
  4. How to create fields? 
  5. How to add Items into the list? 
  6. How to Read items from the list?
Just replace the below code with your "app.js" file on office365(Napa tool) and see the magic.  Please give the required permission levels to do CRUD operations on the hosted site like read/write permissions otherwise get access denied error.


var context;
var web;
var user;
var listname = 'MyList';
var oList;

// This code runs when the DOM is ready. It ensures the SharePoint
// script file sp.js is loaded and then executes sharePointReady()
$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
});

// This function creates a context object which is needed to use the SharePoint object model
function sharePointReady() {
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    context = new SP.ClientContext.get_current();
    hostcontext = new SP.AppContextSite(context, hostUrl);
    web = hostcontext.get_web(); 
 getLists();
}

//To get the lists available list in web
function getLists()
{
 this.lists = web.get_lists();
 context.load(lists);
    context.executeQueryAsync(Function.createDelegate(this, this.IsListsExists), Function.createDelegate(this, this.onQueryFailed));
}

//Below method to check if the lists exists or not?
function IsListsExists()
{
 alert('Check Whether the lists exists or not?');
    var isListAvail = false;
    var listEnumerator = lists.getEnumerator();
    while (listEnumerator.moveNext()) {
        list = listEnumerator.get_current();
        if (list.get_title() == listname) {
            isListAvail = true;
   //Read list items from the lists
   getItems(); 
        }
    }
    if (!isListAvail) {
        //Create a new list if not exists
        createList();
  addItems();
    }
}

//Create a list
function createList() {    
 alert('create a list if not exists'+'-'+ listname);
    var listCreationInfo = new SP.ListCreationInformation();
    listCreationInfo.set_title(listname);
    listCreationInfo.set_templateType(SP.ListTemplateType.genericList);
    this.oList = web.get_lists().add(listCreationInfo);
    context.load(oList);

    //Add new fields to the list
    this.newColumn = oList.get_fields().addFieldAsXml("

No comments:

Post a Comment