如何为内部部署的 SharePoint 2013 REST API 启用 JSON 响应?

How to enable JSON response for SharePoint 2013 REST APIs onpremise?

我正在使用 SharePoint 2013 本地和在线版本进行集成。在访问在线 SharePoint 的 REST API 时,我可以毫无问题地将 application/jsonapplication/xml 用作 ACCEPT header。

但是,在访问 SharePoint 2013 本地 REST API 时,我可以将 application/xml 用作 ACCEPT header 并使用 application/json 抛出以下错误:

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

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

试试这个:

"accept": "application/json; odata=verbose"

我之前在 on-premise SharePoint 2013 中遇到过此类问题。Mike 的回答也很有价值。您需要将 Accept header 值更改为“application/json;odata=verbose”,但我认为这不是问题所在。我认为您需要修补 SharePoint 的 on-premise 实例以支持 OData 3 和 JSON Light。请仔细阅读以下 blog post 中的说明。当我们在现场部署解决方案时,当我们从 REST API 发出对 json 的请求时,我们仍然发现农场会遇到这种情况。但是,您更有可能在新启动的开发实例中找到它。解决起来相对快速和简单。祝你好运!

编辑:
Technet 文章似乎最近被删除了。这是 WCF Data Services 5.6 的下载 link。仍然遵循原始 post 中的指导,我想你会很快 运行 。此外,在此更新后,您应该能够删除接受 header 的 odata=verbose 部分。

PowerShell 完成升级(运行 安装 WCF 数据服务后) 运行 在升级 WCF 数据服务的 SharePoint 服务器上。

$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"

改变这个
headers: { "accept": "application/json", },

试试这个 headers: { "accept": "application/json;odata=verbose", },