如何在本地为 SharePoint 2013 REST API 启用 JSON 响应? [英] How to enable JSON response for SharePoint 2013 REST APIs onpremise?

查看:38
本文介绍了如何在本地为 SharePoint 2013 REST API 启用 JSON 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SharePoint 2013 onprem 和在线版本进行集成.在访问在线 SharePoint 的 REST API 时,我能够使用 application/jsonapplication/xml 作为 ACCEPT 标头,没有任何问题.>

但是,在访问 SharePoint 2013 内部部署 REST API 时,我可以使用 application/xml 作为 ACCEPT 标头并使用 application/json 抛出以下错误:

GET - http://xxxxxxx:8300/_api/web/标题 -接受:应用程序/json回复:{错误": {"code": "-1, Microsoft.SharePoint.Client.ClientServiceException",信息": {"lang": "en-US","value": "HTTP 标头 ACCEPT 丢失或其值无效."}}}

您能否建议我如何获得 LIST、LISTITEM 对象的 JSON 响应?

解决方案

我之前在本地 SharePoint 2013 中遇到过此类问题.Mike 的回答也有优点.您需要将 Accept 标头值更改为application/json;odata=verbose",但我认为这不是问题所在.我认为您需要修补 SharePoint 的内部部署实例以支持 OData 3 和 JSON Light.仔细阅读以下博客文章.当我们在现场部署解决方案时,我们仍然会发现当我们从 REST API 请求 json 时遇到这种情况.但是,您更有可能在新启动的开发实例中找到它.它相对快速和简单地解决.祝你好运!


看来 Technet 文章最近已被删除.这是
的下载链接WCF 数据服务 5.6.仍然遵循原始帖子中的指导,我认为您会很快启动并运行.此外,您应该能够在此更新后删除 Accept 标头的 odata=verbose 部分.

PowerShell 完成升级(在 WCF 数据服务安装后运行)在升级 WCF 数据服务的 SharePoint Server 上运行此程序.

$configOwnerName = "JSONLightDependentAssembly"$spWebConfigModClass ="Microsoft.SharePoint.Administration.SPWebConfigModification"$dependentAssemblyPath ="configuration/runtime/*[local-name()='assemblyBinding' and namespace-uri()='urn:schemas-microsoft-com:asm.v1']"$dependentAssemblyNameStart ="*[local-name()='dependentAssembly'][*/@name='"$dependentAssemblyNameEnd = "'][*/@publicKeyToken='31bf3856ad364e35'][*/@culture='neutral']"$dependentAssemblyValueStart = "<dependentAssembly><assemblyIdentity name='"$dependentAssemblyValueEnd ="'publicKeyToken='31bf3856ad364e35'culture='neutral'/><bindingRedirect oldVersion='5.0.0.0' newVersion='5.6.0.0'/></dependentAssembly>"$edmAssemblyName ="Microsoft.Data.Edm"$odataAssemblyName ="Microsoft.Data.Odata"$dataServicesAssemblyName ="Microsoft.Data.Services"$dataServicesClientAssemblyName ="Microsoft.Data.Services.Client"$spatialAssemblyName ="System.Spatial"$assemblyNamesArray = $edmAssemblyName,$odataAssemblyName,$dataServicesAssemblyName,$dataServicesClientAssemblyName, $spatialAssemblyName添加 PSSnapin Microsoft.SharePoint.Powershell$webService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService################ 添加单个程序集####################对于 ($i=0; $i -lt 5; $i++){echo "正在添加程序集..."$assemblyNamesArray[$i]$dependentAssembly = 新对象 $spWebConfigModClass$dependentAssembly.Path=$dependentAssemblyPath$dependentAssembly.Sequence =0 # 要插入的第一项$dependentAssembly.Owner = $configOwnerName$dependentAssembly.Name =$dependentAssemblyNameStart + $assemblyNamesArray[$i] + $dependentAssemblyNameEnd$dependentAssembly.Type = 0 #确保子节点$dependentAssembly.Value = $dependentAssemblyValueStart + $assemblyNamesArray[$i] + $dependentAssemblyValueEnd$webService.WebConfigModifications.Add($dependentAssembly)}#############################################################echo "保存 Web 配置修改"$webService.Update()$webService.ApplyWebConfigModifications()echo "更新完成"

I am using SharePoint 2013 onprem and online version for integration purpose. While accessing REST APIs for online SharePoint, I was able to use application/json and application/xml as ACCEPT header with no issues.

However, while accessing SharePoint 2013 onpremise REST APIs I could use application/xml as ACCEPT header and using application/json throwing below error:

GET - http://xxxxxxx:8300/_api/web/
Header -
Accept:application/json
Response:
{
    "error": {
        "code": "-1, Microsoft.SharePoint.Client.ClientServiceException",
        "message": {
            "lang": "en-US",
            "value": "The HTTP header ACCEPT is missing or its value is invalid."
        }
    }
}

Could you please suggest How can I get JSON response for LIST, LISTITEM objects?

解决方案

I've encountered this type of issue before in on-premise SharePoint 2013. Mike's answer also has merit. You will need to change your Accept header value to "application/json;odata=verbose", though I don't think that is the issue. I think you need to patch the on-premise instance of SharePoint to support OData 3 and JSON Light. Read the instructions carefully from the following blog post. As we deploy solutions in the field, we still find farms that encounter this when we make requests for json from the REST API. However, you are far more likely to find it in a freshly spun up development instance. It's relatively quick and simple to address. Good Luck!

Edit:
It appears the Technet article was recently removed. Here is the download link for WCF Data Services 5.6. Still follow the guidance in original post and I think you'll be up and running pretty quickly. In addition, you should be able to drop off the odata=verbose portion of the Accept header after this update.

PowerShell To Complete the Upgrade (Run after the WCF Data Services Install) Run this on the SharePoint Server that your upgraded WCF Data Services on.

$configOwnerName = "JSONLightDependentAssembly"

$spWebConfigModClass ="Microsoft.SharePoint.Administration.SPWebConfigModification"

$dependentAssemblyPath ="configuration/runtime/*[local-name()='assemblyBinding' and namespace-uri()='urn:schemas-microsoft-com:asm.v1']"

$dependentAssemblyNameStart ="*[local-name()='dependentAssembly'][*/@name='"
$dependentAssemblyNameEnd = "'][*/@publicKeyToken='31bf3856ad364e35'][*/@culture='neutral']"

$dependentAssemblyValueStart = "<dependentAssembly><assemblyIdentity name='"
$dependentAssemblyValueEnd ="' publicKeyToken='31bf3856ad364e35' culture='neutral' /><bindingRedirect oldVersion='5.0.0.0' newVersion='5.6.0.0' /></dependentAssembly>"

$edmAssemblyName ="Microsoft.Data.Edm"
$odataAssemblyName ="Microsoft.Data.Odata"
$dataServicesAssemblyName ="Microsoft.Data.Services"
$dataServicesClientAssemblyName ="Microsoft.Data.Services.Client"
$spatialAssemblyName ="System.Spatial"


$assemblyNamesArray = $edmAssemblyName,$odataAssemblyName,$dataServicesAssemblyName,$dataServicesClientAssemblyName, $spatialAssemblyName


Add-PSSnapin Microsoft.SharePoint.Powershell
$webService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService


################ Adds individual assemblies ####################

For ($i=0; $i -lt 5; $i++)  
{
    echo "Adding Assembly..."$assemblyNamesArray[$i]

$dependentAssembly = New-Object $spWebConfigModClass
$dependentAssembly.Path=$dependentAssemblyPath
$dependentAssembly.Sequence =0 # First item to be inserted
$dependentAssembly.Owner = $configOwnerName
$dependentAssembly.Name =$dependentAssemblyNameStart + $assemblyNamesArray[$i] + $dependentAssemblyNameEnd
$dependentAssembly.Type = 0 #Ensure Child Node
$dependentAssembly.Value = $dependentAssemblyValueStart + $assemblyNamesArray[$i] + $dependentAssemblyValueEnd

    $webService.WebConfigModifications.Add($dependentAssembly)
}

###############################################################

echo "Saving Web Config Modification"

$webService.Update()
$webService.ApplyWebConfigModifications()
echo "Update Complete"

这篇关于如何在本地为 SharePoint 2013 REST API 启用 JSON 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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