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

蓝咒 提交于 2020-01-14 05:32:24

问题


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?


回答1:


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"



回答2:


Try this:

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



回答3:


Change this
headers: { "accept": "application/json", },

Try This headers: { "accept": "application/json;odata=verbose", },



来源:https://stackoverflow.com/questions/49679078/how-to-enable-json-response-for-sharepoint-2013-rest-apis-onpremise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!