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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.