First create SharePoint Hosted App using Visual Studio 2012 and add below given html control in default.aspx page
< div>
< p id="message1">
initializing...
< /p>
< /div>
< div>
< p id="percentHTML">
initializing..................
< /p>
< /div>
< p id="hostWebURL">< /p>
< p id="appWebURL">< /p>
< div id="message">
< /div>
< p id="currentWebText">< /p>
< ul id="listTree">
< /ul>
< div id="addItemPanel" style='display: none'>
Item Title:
< input type="text" name="itemTitle" title="Item Title" id="itemTitle" />
< a href="javascript:createListItem()">Create List Item< /a>
< /div>
Replace App.js as below
'use strict';
var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var oWebsite;
var collList;
var clientContext;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
getUserName();
//drawTable();
retrieveWebSiteProperties();
});
// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess() {
$('#message1').text('Hello ' + user.get_title());
}
// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
alert('Failed to get user name. Error:' + args.get_message());
}
function retrieveWebSiteProperties() {
clientContext = new SP.ClientContext.get_current();
oWebsite = clientContext.get_web();
clientContext.load(oWebsite, 'Title', 'Created');
collList = oWebsite.get_lists();
clientContext.load(collList, 'Include(Title, Id)');
clientContext.executeQueryAsync(
Function.createDelegate(this, onQuerySucceeded),
Function.createDelegate(this, onQueryFailed)
);
}
function onQuerySucceeded(sender, args) {
var message = 'Title: ' + oWebsite.get_title() +
' Created: ' + oWebsite.get_created();
document.getElementById("message").innerText = message;
//display lists
var listEnumerator = collList.getEnumerator();
var listTree = document.getElementById("listTree");
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
var listTitle = oList.get_title();
var li = document.createElement("li");
li.id = listTitle;
li.appendChild(document.createTextNode(listTitle));
listTree.appendChild(li);
if (listTitle == "CustomList1") {
getListItems();
}
}
}
function getListItems() {
//alert('getting list items');
var oList = clientContext.get_web().get_lists().getByTitle('CustomList1');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'< View>< Query>< Where>< Geq>< FieldRef Name=\'ID\'/>' +
'< Value Type=\'Number\'>1< /Value>< /Geq>< /Where>< /Query>' +
'< RowLimit>25< /RowLimit>< /View>'
);
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onItemsGetQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
function createListItem() {
var oList = clientContext.get_web().get_lists().getByTitle('CustomList1');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', document.getElementById("itemTitle").value);
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onItemCreateQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onItemsGetQuerySucceeded() {
var listItemEnumerator = collListItem.getEnumerator();
var cl = document.getElementById("CustomList1");
while (cl.childNodes.length > 0) {
cl.removeChild(cl.childNodes[0]);
}
cl.textContent = cl.id;
var ul = document.createElement("ul");;
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var li = document.createElement("li");
li.appendChild(document.createTextNode("Title:" + oListItem.get_item('Title') + " Title: " + oListItem.get_item('Title')));
ul.appendChild(li);
}
cl.appendChild(ul);
var div = document.createElement("div");
div.innerHTML = document.getElementById("addItemPanel").innerHTML;
cl.appendChild(div);
}
function onItemCreateQuerySucceeded() {
//alert('Item created: ' + oListItem.get_id());
getListItems();
}
No comments:
Post a Comment