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

如何在本地为SharePoint 2013 REST API启用JSON响应?,sharepoint,sharepoint-2013,sharepoint-online,Sharepoint,Sharepoint 2013,Sharepoint Online,我使用SharePoint 2013 onprem和在线版本进行集成。在访问在线SharePoint的REST API时,我能够使用application/json和application/xml作为ACCEPT标题,没有任何问题 但是,在访问SharePoint 2013内部REST API时,我可以使用application/xml作为ACCEPT标题,并使用application/json抛出以下错误: GET - http://xxxxxxx:8300/_api/web/ Header

我使用SharePoint 2013 onprem和在线版本进行集成。在访问在线SharePoint的REST API时,我能够使用
application/json
application/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."
        }
    }
}
您能建议如何获取列表、LISTITEM对象的JSON响应吗?

尝试以下方法:

"accept": "application/json; odata=verbose"
试试这个:

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

我以前在内部部署SharePoint 2013中遇到过此类问题。迈克的回答也有可取之处。您需要将Accept头值更改为“application/json;odata=verbose”,尽管我认为这不是问题所在。我认为您需要修补SharePoint的内部实例,以支持OData3和JSON Light。请仔细阅读以下说明。当我们在现场部署解决方案时,我们仍然会发现当我们从RESTAPI请求json时会遇到这种情况。然而,您更有可能在一个新开发的实例中找到它。这是相对快速和简单的解决。祝你好运

编辑:
看来Technet文章最近被删除了。这里是的下载链接。仍然遵循原始帖子中的指导,我认为你会很快开始运行。此外,您应该能够在此次更新后删除Accept标头的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"
$configOwnerName=“JSONLightDependentAssembly”
$spWebConfigModClass=“Microsoft.SharePoint.Administration.SPWebConfigModification”
$dependentAssemblyPath=“配置/runtime/*[local-name()='assemblyBinding'和namespace-uri()='urn:schemas-microsoft-com:asm.v1']”
$dependentAssemblyNameStart=“*[local-name()='dependentAssembly'][*/@name=”
$dependentAssemblyNameEnd=“”][*/@publicKeyToken='31bf3856ad364e35'][*/@culture='neutral']”
$dependentAssemblyValueStart=“”
$EDMSassemblyName=“Microsoft.Data.Edm”
$odataAssemblyName=“Microsoft.Data.Odata”
$dataServicesAssemblyName=“Microsoft.Data.Services”
$dataServicesClientAssemblyName=“Microsoft.Data.Services.Client”
$spatialAssemblyName=“System.Spatial”
$assemblyNamesArray=$edmAssemblyName、$odataAssemblyName、$dataServicesAssemblyName、$DataServiceClientAssemblyName、$spatialAssemblyName
在Microsoft.SharePoint.Powershell中添加PSSNaps
$webService=[Microsoft.SharePoint.Administration.SPWebService]::ContentService
################添加单个程序集####################
对于($i=0;$i-lt 5;$i++)
{
回显“正在添加程序集…”$assemblyNamesArray[$i]
$dependentAssembly=新对象$spWebConfigModClass
$dependentAssembly.Path=$dependentAssemblyPath
$dependentAssembly.Sequence=0#要插入的第一项
$dependentAssembly.Owner=$configOwnerName
$dependentAssembly.Name=$dependentAssemblyNameStart+$AssemblyNameArray[$i]+$dependentAssemblyNameEnd
$dependentAssembly.Type=0#确保子节点
$dependentAssembly.Value=$dependentAssemblyValueStart+$assemblyNamesArray[$i]+$dependentAssemblyValueEnd
$webService.WebConfigModifications.Add($dependentAssembly)
}
###############################################################
echo“保存Web配置修改”
$webService.Update()
$webService.ApplyWebConfigModifications()
回显“更新完成”

我以前在内部部署SharePoint 2013中遇到过此类问题。迈克的回答也有可取之处。您需要将Accept头值更改为“application/json;odata=verbose”,尽管我认为这不是问题所在。我认为您需要修补SharePoint的内部实例,以支持OData3和JSON Light。请仔细阅读以下说明。当我们在现场部署解决方案时,我们仍然会发现当我们从RESTAPI请求json时会遇到这种情况。然而,您更有可能在一个新开发的实例中找到它。这是相对快速和简单的解决。祝你好运

编辑:
看来Technet文章最近被删除了。这里是的下载链接。仍然遵循原始帖子中的指导,我认为你会很快开始运行。此外,您应该能够在此次更新后删除Accept标头的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"
$configOwnerName=“JSONLightDependentAssembly”
$spWebConfigModClass=“Microsoft.SharePoint.Administration.SPWebConfigModification”
$dependentAssemblyPath=“配置/runtime/*[local-name()='assemblyBinding'和namespace-uri()='urn:schemas-microsoft-com:asm.v1']”
$dependentAssemblyNameStart=“*[local-name()='dependentAssembly'][*/@name=”
$dependentAssemblyNameEnd=“”][*/@publicKeyToken='31bf3856ad364e35'][*/@culture='neutral']”
$dependentAssemblyValueStart=“”
$EDMSassemblyName=“Microsoft.Data.Edm”
$odataAssemblyName=“Microsoft.Data.Odata”
$dataServicesAssemblyName=“Microsoft.Data.Services”
$dataServicesClientAssemblyName=“Microsoft.Data.Services.Client”
$spatialAssemblyName=“System.Spatial”
$assemblyNamesArray=$edmAssemblyName、$odataAssemblyName、$dataServicesAssemblyName、$DataServiceClientAssemblyName、$spatialAssemblyName
在Microsoft.SharePoint.Powershell中添加PSSNaps
$webService=[Microsoft.SharePoint.Administration.SPWebService]::ContentService
################添加单个程序集####################
对于($i=0;$i-lt 5;$i++)
{