问题
I need to post two values Caption and Location, later being a GeoLocation field in Sharepoint list. I am using the following JSON:
{"__metadata": { "type": "SP.ListItem" }, "Caption": "Testing", "Location": "POINT (78.4 17.4)"}
But it's not working. Shows the following error:
Cannot deserialize data for type Microsoft.SharePoint.SPFieldGeolocationValue
I am doing this from Xcode.
回答1:
SharePoint REST service expects SP.FieldGeolocationValue to be specified in the following format:
{
"__metadata": {
"type": "SP.FieldGeolocationValue"
},
"Altitude": <val>,
"Latitude": <val>,
"Longitude": <val>,
"Measure": <val>
}
JavaScript example
The example demonstrates how to create list item in Contacts
list and set Geolocation value for Location
field:
function createContact(name,location){
var listTitle = 'Contacts';
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/items";
var payload = {
"__metadata": { "type": "SP.ListItem" },
"Title": name,
"Location": {
"__metadata": {"type": "SP.FieldGeolocationValue"},
"Latitude": location[0],
"Longitude": location[1],
}
};
return executeJson(url,'POST',null,payload);
}
createContact("Work Address", [60.2872339,24.8516785])
.done(function(data)
{
console.log('Contact has been created');
})
.fail(function(error){
console.log('An error occured while creating contact');
});
where
function executeJson(url,method,headers,payload)
{
method = method || 'GET';
headers = headers || {};
headers["Accept"] = "application/json;odata=verbose";
if(method == "POST") {
headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}
var ajaxOptions =
{
url: url,
type: method,
contentType: "application/json;odata=verbose",
headers: headers
};
if (typeof payload != 'undefined') {
ajaxOptions.data = JSON.stringify(payload);
}
return $.ajax(ajaxOptions);
}
来源:https://stackoverflow.com/questions/31745125/how-to-post-geolocation-values-to-a-sharepoint-list-field-with-rest-api