Updating SharePoint List Items with JavaScript

This is a simple example of updating a SharePoint list item via the Javascript CSOM API (I never quite know what to call it – Microsoft vary in their own naming of things).

The basic idea might be that this code would be called from a page within SharePoint, so is able to pick up the client context, and run the code. It will obviously fail if the user has no permissions to update the item in question.

function update_list_item(list_name, id, internal_field_name, value)
    // get connection to SharePoint
    var context = new SP.ClientContext.get_current();

    // get the current sharepoint site
    var web = context.get_web();

    // get a list from the site
    var list = web.get_lists().getByTitle(list_name);

    // get an item from the list
    var list_item = list.getItemById(id);

    // populate a property of the list item
    list_item.set_item(internal_field_name, value);

    // force sharepoint to save the change

    // tell SharePoint to do everything we just talked about
    context.executeQueryAsync(update_list_item_success, update_list_item_failure);
    return false;

function update_list_item_success(sender, args)
    alert("UpdateListItem Succeeded");

function update_list_item_failure(sender, args)
    alert("UpdateListItem Failed. \n" + args.get_message() + "\n" + args.get_stackTrace());

It’s probably worth making a few comments about updating different field types. The two standout ones that always cause trouble are URLs and Dates.

When updating a URL field, you can set the URL and label at the same time – by seperating them with – e.g. “, Google”.

When updating a date field, SharePoint will expect the date in a specific format if you’re sending it as text. The easiest way around this without reading up on the ISO 8061 date format that SharePoint expects is just to pass it a Javascript Date object instead of text, and it will be quite happy.

Posted by Jonathan Beckett in Notes, 0 comments

Creating Custom Lists in SharePoint with PowerShell

Shall we start this blog with some simple PowerShell commands? The following snippet creates a Custom List in SharePoint on-prem. Notice that the code checks for the existence of the list before creating it – because typically you will be using this in a larger provisioning script, and it makes sense to let your code run more than once – so as you build a solution, you can just add new elements and run it again.

# Open a connection to a SharePoint site
$web = Get-SPWeb "https://server/sites/sitecollection/subsite"

# set the internal and display names of the list
$list_internal_name = "audittrail"
$list_display_name = "Audit Trail"

if ($web.Lists[$list_display_name]) {

   # if the list exists, get it
   $list = $web.Lists[$list_display_name]

} else {

   # if the list does not exist, create it
   $web.Lists.Add($list_internal_name, "", "GenericList") > $null
   $list = $web.Lists[$list_internal_name]
   $list.Title = $list_display_name
   $list.ContentTypesEnabled = $true
   $list.EnableVersioning = $true
   $list.NavigateForFormsPages = $false
   $list.Update() > $null


# Release resources

Notice that I’m enabling versioning and content types on the list – because typically you would be setting up fields on the list next, and you should always really do that via site content types, and add them to the list.

Posted by Jonathan Beckett in Notes, 0 comments