viernes, 19 de julio de 2013

Insert Item in SharePoint list using javascript

The next post show the way to insert an item in sharepoint list using javascript.

Pre Requisites.
one sharepoint list.

1. In the main page, insert a content editor


2. Now, in a notepad insert the next code

Code


 Item Title  
 <br/>  
 <input id="Text1" type="text"/>  
 <br/>  
 <INPUT TYPE="button" VALUE="CrearLista" onClick="createListItem()">  
 <script type="text/javascript">  
 function createListItem()   
 {  
   var siteUrl = '/sites/ejemplossharepoint';  
   var Context = new SP.ClientContext(siteUrl);  
   var oList = Context.get_web().get_lists().getByTitle('ItemsSharePoint');  
   var itemCreateInfo = new SP.ListItemCreationInformation();  
   this.oListItem = oList.addItem(itemCreateInfo);  
   oListItem.set_item('Title', document.getElementById('Text1').value);  
   oListItem.update();  
   Context.load(oListItem);  
   Context.executeQueryAsync(Function.createDelegate(this, this.Succeeded), Function.createDelegate(this, this.Failed));  
 }  
 function Succeeded()   
 {  
   alert('Item number:' + oListItem.get_id() + ' was created');  
 }  
 function Failed(sender, args)   
 {  
   alert('Error:' + args.get_message() + '\n' + args.get_stackTrace());  
 }  
 </script>  

3. Save the document as "Code".


4. Create a document library with the name CodeLibrary and add the file "Code".



5. Copy the file link.


6. Edit the content editor and paste the file link.


7. test and verify.




easily =D

Regards.
Oscar Miguel Dominguez Acevedo.

No hay comentarios:

Publicar un comentario