2017-10-09 270 views
1

CRM 2016公開了odata/web api,並具有functionsactions開箱即用。如何對odata端點執行搜索操作

我試圖執行以下請求:

https://mycrmorg.com/api/data/v8.1/Search([email protected])[email protected]={"ServiceId":"0640dd7b-984f-e711-9425-00155dc0d345","AnchorOffset":1,"UserTimeZoneCode":1,"RecurrenceDuration":0,"RecurrenceTimeZoneCode":1,"RequiredResources":{"ResourceId":"2031105f-ec0d-e711-9417-00155d835de9"},"SearchWindowStart":"1754-01-01T00:00:00Z","SearchWindowEnd":"9999-12-30T23:59:59Z","SearchRecurrenceStart":null,"Duration":1,"Constraints":[],"Objectives":[],"NumberOfResults":1,"Sites":[]} 

的JSON的漂亮版本:

{ 
    "ServiceId":"0640dd7b-984f-e711-9425-00155dc0d345", 
    "AnchorOffset":1, 
    "UserTimeZoneCode":1, 
    "RecurrenceDuration":0, 
    "RecurrenceTimeZoneCode":1, 
    "RequiredResources":{ 
     "ResourceId":"2031105f-ec0d-e711-9417-00155d835de9" 
    }, 
    "SearchWindowStart":"1754-01-01T00:00:00Z", 
    "SearchWindowEnd":"9999-12-30T23:59:59Z", 
    "SearchRecurrenceStart":null, 
    "Duration":1, 
    "Constraints":[ 

    ], 
    "Objectives":[ 

    ], 
    "NumberOfResults":1, 
    "Sites":[ 

    ] 
} 

我得到的例外是:

{ 
    "error":{ 
    "code":"","message":"An unexpected 'StartObject' node was found for property named 'RequiredResources' when reading from the JSON reader. A 'StartArray' node was expected.","innererror":{ 
     "message":"An unexpected 'StartObject' node was found for property named 'RequiredResources' when reading from the JSON reader. A 'StartArray' node was expected.","type":"Microsoft.OData.Core.ODataException","stacktrace":" at Microsoft.OData.Core.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadNonEntityValueImplementation(String payloadTypeName, IEdmTypeReference expectedTypeReference, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, CollectionWithoutExpectedTypeValidator collectionValidator, Boolean validateNullValue, Boolean isTopLevelPropertyValue, Boolean insideComplexValue, String propertyName, Nullable`1 isDynamicProperty)\r\n at Microsoft.OData.Core.JsonLight.ODataJsonLightPropertyAndValueDeserializer.<>c__DisplayClass14.<ReadComplexValue>b__12(PropertyParsingResult propertyParsingResult, String propertyName)\r\n at Microsoft.OData.Core.JsonLight.ODataJsonLightDeserializer.ProcessProperty(DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, Func`2 readPropertyAnnotationValue, Action`2 handleProperty)\r\n at Microsoft.OData.Core.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadComplexValue(IEdmComplexTypeReference complexValueTypeReference, String payloadTypeName, SerializationTypeNameAnnotation serializationTypeNameAnnotation, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker)\r\n at Microsoft.OData.Core.JsonLight.ODataJsonLightPropertyAndValueDeserializer.ReadNonEntityValueImplementation(String payloadTypeName, IEdmTypeReference expectedTypeReference, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, CollectionWithoutExpectedTypeValidator collectionValidator, Boolean validateNullValue, Boolean isTopLevelPropertyValue, Boolean insideComplexValue, String propertyName, Nullable`1 isDynamicProperty)\r\n at Microsoft.OData.Core.UriParser.ODataUriConversionUtils.ConvertFromComplexOrCollectionValue(String value, IEdmModel model, IEdmTypeReference typeReference)\r\n at Microsoft.OData.Core.UriParser.ODataUriUtils.ConvertFromUriLiteral(String value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)\r\n at Microsoft.OData.Core.UriParser.Parsers.ParameterAliasBinder.ParseComplexOrCollectionAlias(QueryToken queryToken, IEdmTypeReference parameterType, IEdmModel model)\r\n at Microsoft.OData.Core.UriParser.Parsers.ParameterAliasBinder.ParseAndBindParameterAliasValueExpression(BindingState bindingState, String aliasValueExpression, IEdmTypeReference parameterType)\r\n at Microsoft.OData.Core.UriParser.Parsers.ParameterAliasBinder.BindParameterAlias(BindingState bindingState, FunctionParameterAliasToken aliasToken)\r\n at Microsoft.OData.Core.UriParser.Parsers.MetadataBinder.Bind(QueryToken token)\r\n at Microsoft.OData.Core.UriParser.Parsers.FunctionCallBinder.BindSegmentParameters(ODataUriParserConfiguration configuration, IEdmOperation functionOrOpertion, ICollection`1 segmentParameterTokens)\r\n at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.TryBindingParametersAndMatchingOperationImport(String identifier, String parenthesisExpression, ODataUriParserConfiguration configuration, ICollection`1& boundParameters, IEdmOperationImport& matchingFunctionImport)\r\n at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.TryCreateSegmentForOperationImport(String identifier, String parenthesisExpression)\r\n at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.CreateFirstSegment(String segmentText)\r\n at Microsoft.OData.Core.UriParser.Parsers.ODataPathParser.ParsePath(ICollection`1 segments)\r\n at Microsoft.OData.Core.UriParser.Parsers.ODataPathFactory.BindPath(ICollection`1 segments, ODataUriParserConfiguration configuration)\r\n at Microsoft.OData.Core.UriParser.ODataUriParser.Initialize()\r\n at System.Web.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath, ODataUriResolverSetttings resolverSettings, Boolean enableUriTemplateParsing)\r\n at System.Web.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath)\r\n at Microsoft.Crm.Extensibility.OData.CrmODataPathHandler.Parse(IEdmModel model, String serviceRoot, String odataPath)" 
    } 
    } 
} 

我相信我編碼RequiredResources的方式可能存在問題。 我在做什麼錯?我如何將此請求發送到CRM?

回答

2

我認爲這個問題可能是你使用RequiredResources作爲對象,而不是一個集合,試圖將其更改爲

"RequiredResources": [{ 
    "ResourceId":"2031105f-ec0d-e711-9417-00155d835de9" 
}], 

完全JSON

{ 
    "ServiceId":"0640dd7b-984f-e711-9425-00155dc0d345", 
    "AnchorOffset":1, 
    "UserTimeZoneCode":1, 
    "RecurrenceDuration":0, 
    "RecurrenceTimeZoneCode":1, 
    "RequiredResources": [{ 
     "ResourceId":"2031105f-ec0d-e711-9417-00155d835de9" 
    }], 
    "SearchWindowStart":"1754-01-01T00:00:00Z", 
    "SearchWindowEnd":"9999-12-30T23:59:59Z", 
    "SearchRecurrenceStart":null, 
    "Duration":1, 
    "Constraints":[ 

    ], 
    "Objectives":[ 

    ], 
    "NumberOfResults":1, 
    "Sites":[ 

    ] 
} 

RequiredResources 集(RequiredResource ComplexType)

https://msdn.microsoft.com/en-us/library/mt592984.aspx

錯誤消息實際上告訴了你解析器預期[摘自代替{符號/記號。