SharePoint 2010 ListData.svc Saving Data

A couple of things I need to note here for anyone working with the SharePoint 2010 Javascript Object Model. Most of the examples you find out there will show you how to save basic column types like Single Line of Text, but I had a REALLY hard time finding an example on how to save a HyperLink column, because the URL and Description fields need to be set differently.

The offical MS documentation on this field:

The URL field uniquely consists of two strings that are separated by a comma and space. One string contains the URL path and the other contains the description that is used as hyperlinked text.

So your REST call should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var url = 'http://deadmines:88/_vti_bin/listdata.svc/MySites';
var mySites = {};
 
// Insert a new Part location.
mySites.URL = _spPageContextInfo.webServerRelativeUrl + ', ' + this.web.get_title();
 
var body = Sys.Serialization.JavaScriptSerializer.serialize(mySites);
 
$.ajax({
    type: 'POST',
        url: url,
        contentType: 'application/json',
        processData: false,
        data: body,
        success: function () {
            // change message back
            $("#add-link").html('Link successfully added!');
        },
        error: function () {
            alert('Error occurred. Please contact your administrator.');
        }
    });

REPEAT: There is a space between the comma and the description.

If you’re interested in learning how to save other field types, leave me a comment and I’ll write something up.

Trackbacks/Pingbacks

  1. SharePoint 2010 ListData.svc Saving Data | SharePoint … | ARB Security Solutions - SharePoint Security Integrators - January 29, 2013

    [...] SharePoint Blog Post From SharePoint Development – Google Blog Search: This is a quick post to show how you can save a HyperLink column to a SharePoint List using the [...]

Leave a Reply