5

I want to insert the value of html forms fields in SharePoint list using ajax (jQuery) Can someone point me in the right direction.

Thanks in advance.

1 Answer 1

4

How to update List Item via SharePoint REST interface

function updateJson(endpointUri,payload, success, error) 
{
    $.ajax({       
       url: endpointUri,   
       type: "POST",   
       data: JSON.stringify(payload),
       contentType: "application/json;odata=verbose",
       headers: { 
          "Accept": "application/json;odata=verbose",
          "X-RequestDigest" : $("#__REQUESTDIGEST").val(),
          "X-HTTP-Method": "MERGE",
           "If-Match": "*"
       },   
       success: success,
       error: error
    });
}

function getItemTypeForListName(name) {
    return"SP.Data." + name.charAt(0).toUpperCase() + name.slice(1) + "ListItem";
}

function updateListItem(webUrl,listTitle,listItemId,itemProperties,success,failure)
{
     var listItemUri =  webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
     var itemPayload = {
       '__metadata': {'type': getItemTypeForListName(listTitle)}
     };
     for(var prop in itemProperties){
           itemPayload[prop] = itemProperties[prop];
     }
     updateJson(listItemUri,itemPayload,success,failure);
}

Usage

var itemProperties = {'Title':'John Doe'};
updateListItem(_spPageContextInfo.webAbsoluteUrl,'Contacts',1,itemProperties,printInfo,logError);
function printInfo()
{
    console.log('Item has been created');
}
function logError(error){
    console.log(JSON.stringify(error));
}

References

Manipulating list items in SharePoint Hosted Apps using the REST API

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.