How to update an image url field in SharePoint lis

2019-09-01 11:39发布

问题:

I have a SharePoint list (let's call it 'Employee') and I am using jQuery and REST to read and write properties on that list.

If I want to update e.g. an int property (let's call it 'EmployeeAge'), it's quite straightforward:

jQuery.ajax({
        url: myUrl,
        type: "POST",
        data:  JSON.stringify({ '__metadata': { 'type': 'SP.Data.EmployeeListItem' }, 'EmployeeAge': age }),
        headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "content-length": 0,
            "X-HTTP-Method": "MERGE",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": "*"

        },

        success: successHandlerUpdateEmployeeAge,
        error: errorHandlerUpdateEmployeeAge
        });

Now I want to use the same principle to update another column of the list. Let's call it 'EmployeeImage'. The column is of type Hyperlink/Picture and sub type Picture. I want to set the image url attribute of the column for a particular list item.

How do I format the data attribute in my ajax call to support this type of update?

I can find a thousand samples on how to update a title field, but none regarding update of complex types like this.

回答1:

After googling the error message I got after correcting the column name issue, I found the answer. It actually turned out to be quite simple:

In order to update a complex type like this, you need to provide it as a javascript object, instead of:

'EmployeeImage': 'http://myurl'

Use this notation to provide the object structure:

'EmployeeImage': {Url: 'http://myurl', Description: 'This is the description'}

Take a look at this article that pointed me in the right direction: [http://social.msdn.microsoft.com/Forums/sharepoint/en-US/a4b4bfcf-1758-4131-9af2-bd1ecf0c839b/not-able-to-insert-a-record-in-a-list-using-rest?forum=appsforsharepoint]