Latest Posts

Update ListItem in SharePoint using JSOM

The example in this topic show how to use JSOM to Update ListItem in SharePoint
  • You can use Content Editor or Script Editor web part on your site, put Html and JSOM (Java Script Object Model) code and that's it. You can perform any action supported by JSOM APIs

How to execute JSOM code in SharePoint?

  • Note: This is just a sample, you can go ahead with your implementation.
  • Open your Site. Create a webpart page.
  • Put a Content Editor Web part and a Script Editor Web part on it.
  • Webpart page content editor and scipt editor
  • I have added below Html in Content Editor webpart. You can replace MainFunction with your actual method name.

    <button onclick="MainFunction();" type="button">Click me​</button>

  • And below references in Script Editor Webpart. You can keep jQuery reference as per your need. ScriptFile.js is a JavaScript file where you can keep below function i.e. your actual logic.

    <script src="https://code.jquery.com/jquery-2.2.4.js" type="text/javascript"></script>

    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>

    <script type="text/javascript" src="/_layouts/15/sp.js"></script>

    <script src="https://MyServer/sites/SiteCollection/style library/js/ScriptFile.js"></script>

  • Once you are done with setting your page, click on the button and your method should get executed.

  • var oListItems, clientContext;
  •  
  • function UpdateListItem() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     var oWeb = clientContext.get_web();
  •  
  •     // Specify list title here
  •     var oList = oWeb.get_lists().getByTitle("List Name");
  •  
  •     // Get Item using CAML Query
  •     var camlQuery = new SP.CamlQuery();
  •  
  •     // Get Item using CAML Query
  •     camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + '<Value Type=\'Text\'>b</Value></Eq></Where></Query></View>');
  •     oListItems = oList.getItems(camlQuery);
  •  
  •     // Load list items to Client Context
  •     // "Include" will get only "Title" and "Id" properties, rather than fetching all the properties(This avoids unnecessary data transfer between client and server)
  •     clientContext.load(oListItems, 'Include(Title, Id)');
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •  
  •     // Get list item count of the items to be updated
  •     var itemCount = oListItems.get_count();
  •  
  •     for (var i = 0; i <= itemCount - 1; i++) {
  •         var oListItem = oListItems.itemAt(i);
  •  
  •         // "Title" is column Name here and "New Title" is new value of the column
  •         oListItem.set_item("Title", "New Title");
  •         oListItem.update();
  •     }
  •  
  •     //Execute the batch
  •     clientContext.executeQueryAsync(FinalQuerySuccess, FinalQueryFailure);
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }
  •  
  • function FinalQuerySuccess(sender, args) {
  •     console.log("success");
  • }
  •  
  • function FinalQueryFailure(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013

We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint