How to POST Geolocation values to a Sharepoint List Field with Rest API?

老子叫甜甜 提交于 2019-12-12 10:17:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!