Different Ways To Add Item In SharePoint 2013 List

Posted: December 22, 2015 in SharePoint2013

Introduction 

In this article we explore in Microsoft SharePoint provider different ways to add item in SharePoint list.

Out-of-box- Add an item to a list:

New item

  1. Navigate to the site containing the list for which you want to add an item.
  2. Select Settings > Site contents and then in the appropriate list section, select the name of the list.
  3. Select the Items tab, and then in the New group select New Item.
  4. Select Save.

Using C#(server object model): Add Item programmatically to SharePoint List using C#.

  1. //Step to Add new list item programmatically to SharePoint List using C#
  2. using(SPSite site = new SPSite(SPContext.Current.Site.Url))
  3. {
  4.     Using(SPWeb web = site.OpenWeb())
  5.     {
  6.         SPList list = web.Lists[“DemoList”];
  7.         SPListItem item = list.Items.Add();
  8.         item[“Title”] = “using C# :Add new list item programmatically”;
  9.         item.Update();
  10.     }
  11. }

Using C#(Client object model): Add Item Programmatically to SharePoint List using CSOM.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6. using Microsoft.SharePoint.Client;
  7. namespace CreateListItem
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string siteUrl = http://servername:2525/”;
  14.             ClientContext clientContext = new ClientContext(siteUrl);
  15.             List oList = clientContext.Web.Lists.GetByTitle(“DemoList”);
  16.             ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
  17.             ListItem oListItem = oList.AddItem(listCreationInformation);
  18.             oListItem[“Title”] = “Add item in SharePoint List using CSOM”;
  19.             oListItem.Update();
  20.             clientContext.ExecuteQuery();
  21.         }
  22.     }
  23. }

SharePoint Web Services (jQuery): Add a new item list. The jQuery Ajax function is used to POST the data to the Lists.asmx web service.

  1. “text/javascript”>
  2. //The status parameter is a string which can be for example success or error. Finally in the ready event of the document, we’ll hook up the click event of the button so the CreateNewItem function is called, with the value of the textbox as the parameter.
  3.         $(document).ready(function() {
  4.             $(“#newTaskButton”).click(function() {
  5.                 CreateNewItem(“Add Item in List with jQuery and the SharePoint Web Services”);
  6.         });
  7.         });
  8.     function CreateNewItem(title) {
  9.         var batch =
  10.             “> \
  11.             “1\” Cmd=\”New\”> \
  12.                 “Title\”>” + title + ” \
  13.              \
  14.         “;
  15.         var soapEnv =
  16.             “?> \
  17.         “http://www.w3.org/2001/XMLSchema-instance\” \
  18.             xmlns:xsd=\http://www.w3.org/2001/XMLSchema\” \
  19.             xmlns:soap=\http://schemas.xmlsoap.org/soap/envelope/\”> \
  20.            \
  21.             “http://schemas.microsoft.com/sharepoint/soap/\”> \
  22.               DemoList \
  23.                \
  24.                 ” + batch + “ \
  25.              \
  26.            \
  27.         “;
  28.         $.ajax({
  29.             url: _spPageContextInfo.webAbsoluteUrl+“/_vti_bin/lists.asmx”,
  30.             beforeSend: function(xhr) {
  31.                 xhr.setRequestHeader(“SOAPAction”,
  32.                 http://schemas.microsoft.com/sharepoint/soap/UpdateListItems”);
  33.             },
  34.             type: “POST”,
  35.             dataType: “xml”,
  36.             data: soapEnv,
  37.             complete: processResult,
  38.             contentType: “text/xml; charset=utf-8”
  39.         });
  40.     }
  41.     //The jQuery ajax function call has a complete option which points to a function, in this function you can process the result as follows:
  42.     function processResult(xData, status) {
  43.         alert(status);
  44.     }

SharePoint REST API: SharePoint REST API and JQuery to Create SharePoint list item.

Reference to latest jquery.min.js.

  1. “text/javascript”>
  2.     function _createListItem( listItems, success, failure) {
  3.         $.ajax({
  4.             url: _spPageContextInfo.webAbsoluteUrl+ “/_api/web/lists/getbytitle(‘DemoList’)/items”,
  5.             type: “POST”,
  6.             contentType: “application/json;odata=verbose”,
  7.             data: JSON.stringify(listItems),
  8.             headers: {
  9.                 “Accept”“application/json;odata=verbose”,
  10.                 “X-RequestDigest”: $(“#__REQUESTDIGEST”).val()
  11.             },
  12.             success: function (data) {
  13.                 success(data);
  14.             },
  15.             error: function (data) {
  16.                 failure(data);
  17.             }
  18.         });
  19.     }
  20.         $(document).ready(function() {
  21.             var item = {
  22.                 “__metadata”: { “type”: itemType },
  23.                 “Title”“Add Item in List using REST API”
  24.             }
  25.             _createListItem(item);
  26.         });

SharePoint PowerShell: Adding list items using PowerShell SharePoint.

  1. #Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
  2. Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
  3. #Variables that we are going to use for list editing
  4. $webURL = http://yoursiteName”
  5. $listName = “Demo List”
  6. #Get the SPWeb object and save it to a variable
  7. $web = Get-SPWeb $webURL
  8. #Get the SPList object to retrieve the “Demo List”
  9. $list = $web.Lists[$listName]
  10. #Create a new item
  11. $newItem = $list.Items.Add()
  12. #Add properties to this list item
  13. $newItem[“Title”] = “Add item in sharepoint List Using SharePoint PowerShell”
  14. #Update the object so it gets saved to the list
  15. $newItem.Update()

Final result shows all the Added Items:

Demolist

Advertisements
Comments
  1. […] Different Ways To Add Item In SharePoint 2013 List // Sharepoint Rocks […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s