Problem Statement
Insert an item into a SharePoint list by using the JavaScript Object Model (JSOM).
Solution
Note
Use the attached document for graphical representation.
Step 1
Create a SharePoint List named “Login Details”.
Step 2
Go to setting and create a column named "UserName", as shown in the below table.
Step 3
Create a second column as "Password".
sr no
|
Column Name
|
Column Type
|
1
|
UserName
|
Single Line of text
|
2
|
Password
|
Single Line of text
|
Step 4
Add a new page to the site – To add a new page, click on the gear icon on the top left side of the page and click “Add a Page”.
Step 5
Name the page as “JSOM” and click "Create".
Step 6
The JSOM page will open in edited mode. Click on Insert >> Media and Content >> Script Editor, as shown below and click on the "Add" button.
Step 7
Click on "Edit web part" and then, click on "Edit snippet".
Step 8
Paste the following code in the code editor window.
a. Be careful to insert your home site URL in the below code.
var siteUrl = 'your site.com/teams/Yout site';
- <html>
- <head>
- <Title>JSOM To insert Record</Title>
- <body>
- <div>
- <table cellpadding="10">
- <tr>
- <td colspan="2" align="center">
- <label style="width: 250px; height: 50px; font-size: large; font: bold;">Login Details</label>
- </td>
- </tr>
- <tr>
- <td>
- <label style="width: 150px; height: 80px; font-size: large; font: bold;">User Name</label></td>
- <td>
- <input type="text" id="txtUserName" style="width: 150px;" /></td>
- </tr>
- <tr>
- <td>
- <label style="width: 150px; height: 80px; font-size: large; font: bold;">Password</label></td>
- <td>
- <input type="text" id="txtPassword" style="width: 150px;" /></td>
- </tr>
- <tr>
- <td>
- <input type="button" id="btnSave" value="Submit" onclick="InsertintGetList();" />
- </td>
- <td>
- <input type="button" id="btnCancel" value="Cancel" onclick="ClearData();" />
- </td>
- </tr>
- </table>
- </div>
- </body>
- </html>
- <script type="text/javascript">
- var siteUrl = 'your site.com/teams/Yout site';
- function InsertintGetList() {
- var UserName = document.getElementById('txtUserName').value;
- var Password = document.getElementById('txtPassword').value;
- var clientContext = new SP.ClientContext(siteUrl);
- var GetList = clientContext.get_web().get_lists().getByTitle('LoginDetails');
- var itemCreateInfo = new SP.ListItemCreationInformation();
- this.GetListItem = GetList.addItem(itemCreateInfo);
- GetListItem.set_item('UserName', UserName);
- GetListItem.set_item('Password', Password);
- GetListItem.update();
- clientContext.load(GetListItem);
- clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
- }
- function onQuerySucceeded() {
- alert('Record Inserted');
- document.getElementById("txtUserName").value = "";
- document.getElementById("txtPassword").value = "";
- }
- function onQueryFailed(sender, args) {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
- function ClearData() {
- document.getElementById('txtUserName').value = "";
- document.getElementById('txtPassword').value = "";
- }
- </script>
-
Step 9
Insert the record and click on the "Save" button.
Check the list "Login Details" with new updated Item.
Happy Coding!