代码之家  ›  专栏  ›  技术社区  ›  Joe

如何在本地为SharePoint 2013 REST API启用JSON响应?

  •  1
  • Joe  · 技术社区  · 6 年前

    我正在使用SharePoint 2013 onprem和在线版本进行集成。在访问REST API for online SharePoint时,我能够使用 application/json application/xml ACCEPT 标题没有问题。

    然而,在访问SharePoint 2013内部部署REST API时,我可以使用 应用程序/xml 接受 标题和使用 应用程序/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."
            }
        }
    }
    

    您能建议我如何获得列表、LISTITEM对象的JSON响应吗?

    3 回复  |  直到 6 年前
        1
  •  4
  •   Mike Smith - MCT    6 年前

    尝试以下操作:

    "accept": "application/json; odata=verbose"
    
        2
  •  2
  •   Swerve    6 年前

    我以前在内部部署SharePoint 2013中遇到过此类问题。迈克的回答也有价值。您需要将Accept标头值更改为“ 应用程序/json;odata=详细 “,但我认为这不是问题所在。我认为您需要修补SharePoint的内部实例以支持OData 3和JSON Light。请仔细阅读以下说明 blog post 。当我们在现场部署解决方案时,我们仍然会发现当我们从REST API请求json时,农场会遇到这种情况。然而,您更有可能在一个新开发的实例中找到它。这是相对快速和简单的地址。祝你好运

    编辑:
    看来Technet文章最近被删除了。以下是的下载链接 WCF Data Services 5.6 。仍然遵循原帖子中的指导,我认为你会很快恢复正常。此外,您应该能够 odata=详细 此更新后接受标头的部分。

    PowerShell完成升级(在安装WCF Data Services后运行) 在升级的WCF Data Services所在的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"
    
        3
  •  1
  •   Ganesh Virpatil    6 年前

    更改此
    标题: { “accept”:“应用程序/json”, },则,

    试试这个 标题: { “accept”:“application/json;odata=verbose”, },则,