Wednesday 23 July 2014

SharePoint 2013 App create an item in a list using custom page with CSOM JavaScript


Solution:
I am going to do this using a App.
Steps:
Create a SharePoint hosted App.
Add Site Columns in the List(Right click on the solution, select Site column..)
Add content type(Right click on the solution, select Content type..)
Add site columns in the content type
Now create a List(Right click on the solution, Select List..)
Add content type to list..
Now go to pages, Add new page. Ex: MyCustomNewItem.aspx
Under : PlaceHolderMain
Add a input textbox.
Ex: <input type="text" name="txtTitle" id="txtTitle"></input>
Add all your list fields controls...example, other text boxes, dropdowns and check boxes..
Now open Apps.js under Scripts
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var list = web.get_lists(); //gets the collection of lists
function createItem() {
// Retrieve the list and add an item to it.
var targetList = list.getByTitle("MyCustomList");
//create a new item object
var listItemCreation = new SP.ListItemCreationInformation();
//add the item to the list using addItem method
var newItem = targetList.addItem(listItemCreation);
var listItemTitle = document.getElementById("txtTitle").value;
//Similarly..add code to read all input fields...
//using set_item method u can set the values to the item fields
newItem.set_item('Title', listItemTitle);
//Similarly..add code to assign fields...
newItem.update();
context.load(newItem);
context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);
}
function ItemCreationSuccess() {
alert("Item got created");
}
function ItemCreationFail(sender, args) {
alert('Failed to create a new item. Error:' + args.get_message());
}
Now deploy the app, you will get Default.aspx page,
Now change to MyCustomNewItem.aspx
You will see your page..
Solution:
I am going to do this using a App.
Steps:
Create a SharePoint hosted App.
Add Site Columns in the List(Right click on the solution, select Site column..)
Add content type(Right click on the solution, select Content type..)
Add site columns in the content type
Now create a List(Right click on the solution, Select List..)
Add content type to list..
Now go to pages, Add new page. Ex: MyCustomNewItem.aspx
Under : PlaceHolderMain
Add a input textbox.
Ex: <input type="text" name="txtTitle" id="txtTitle"></input>
Add all your list fields controls...example, other text boxes, dropdowns and check boxes..
Now open Apps.js under Scripts
var context = SP.ClientContext.get_current(); //gets the current context
var web = context.get_web(); //gets the web object
var list = web.get_lists(); //gets the collection of lists
function createItem() {
// Retrieve the list and add an item to it.
var targetList = list.getByTitle("MyCustomList");
//create a new item object
var listItemCreation = new SP.ListItemCreationInformation();
//add the item to the list using addItem method
var newItem = targetList.addItem(listItemCreation);
var listItemTitle = document.getElementById("txtTitle").value;
//Similarly..add code to read all input fields...
//using set_item method u can set the values to the item fields
newItem.set_item('Title', listItemTitle);
//Similarly..add code to assign fields...
newItem.update();
context.load(newItem);
context.executeQueryAsync(ItemCreationSuccess, ItemCreationFail);
}
function ItemCreationSuccess() {
alert("Item got created");
}
function ItemCreationFail(sender, args) {
alert('Failed to create a new item. Error:' + args.get_message());
}
Now deploy the app, you will get Default.aspx page,
Now change to MyCustomNewItem.aspx
You will see your page..