【问题标题】:How to enable JSON response for SharePoint 2013 REST APIs onpremise?如何在本地为 SharePoint 2013 REST API 启用 JSON 响应?
【发布时间】:2018-09-15 16:08:26
【问题描述】:

我正在使用 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/
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."
        }
    }
}

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

【问题讨论】:

    标签: sharepoint sharepoint-2013 sharepoint-online


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 是的,添加 odata=verbose 有效。但我只想使用 application/json 。看起来我需要按照上面的评论运行一些 powershell 脚本。
    【解决方案2】:

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

    编辑:
    看来 Technet 文章最近已被删除。这是WCF Data Services 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
    
    
    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"
    

    【讨论】:

    • 您好转向,感谢您的回复。是的,我只想使用 application/json。我浏览了您共享的链接“解决方案现已发布到 Technet”,Technet 链接将我重定向到 Microsoft 论坛页面,标题为“SharePoint Server 管理”。请指导。
    • 很抱歉。我已经用你需要的直接链接编辑了这篇文章。
    • 您好 Swerve,我下载并安装了 WCF 数据服务 5.6。但我发现 API 行为没有变化。 JSON Minimal 和 nometadata 仍然不适合我。我按照建议阅读了博客。我想我还没有运行步骤“JSON.ps1 来更新 web.config”。您能否建议我可以从哪里下载并按照说明安装此 ps1?
    • 您好 Swerve,我通过 social.msdn.microsoft.com/Forums/office/en-US/… 运行了 powershell 脚本,但没有运气。
    • 对不起乔。我现在共享了 PowerShell,我们用于在将 WCF 数据服务升级到 5.6 后允许在 Accept 标头中使用 application/json。
    【解决方案3】:

    改变这个
    标题: { “接受”:“应用程序/json”, },

    试试这个 标题: { “接受”:“应用程序/json;odata=verbose”, },

    【讨论】:

    • 嗨 Ganesh,我不想使用 odata=verbose。我只想使用“应用程序/json”。我们需要运行一些 powershell 脚本来启用它。
    猜你喜欢
    • 2018-09-15
    • 2015-07-22
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多