如何使用Rest API将Geolocation值发布到Sharepoint列表字段? [英] How to POST Geolocation values to a Sharepoint List Field with Rest API?

查看:90
本文介绍了如何使用Rest API将Geolocation值发布到Sharepoint列表字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发布两个值Caption和Location,稍后是Sharepoint列表中的GeoLocation字段.我正在使用以下JSON:

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)"}

{"__metadata": { "type": "SP.ListItem" }, "Caption": "Testing", "Location": "POINT (78.4 17.4)"}

但是它不起作用.显示以下错误:

But it's not working. Shows the following error:

无法反序列化Microsoft.SharePoint.SPFieldGeolocationValue类型的数据

Cannot deserialize data for type Microsoft.SharePoint.SPFieldGeolocationValue

我正在通过Xcode进行此操作.

I am doing this from Xcode.

推荐答案

SharePoint REST服务期望

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示例

该示例演示如何在Contacts列表中创建列表项以及如何为Location字段设置Geolocation值:

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');
});

其中

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);
}

这篇关于如何使用Rest API将Geolocation值发布到Sharepoint列表字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆