Gravatar for artem@brainjocks.com

Question by artem, Dec 10, 2015 7:35 AM

Modify Rest response

Preconditions: Sitecore 8 Update 3. Coveo for sitecore: Sitecore Integration 3.0 September 2015 Release. Free edition for dev purposes.

I want to modify REST response data by additing additional fields to each result entity. For that purpose I'm going to use coveoProcessParsedRestResponse pipeline. I found documentation here: https://developers.coveo.com/display/SitecoreV3/Altering+Search+Results+Before+They+Are+Displayed+in+a+Search+Interface I created class:


    public class DefineAdditionalProductLinks : IProcessor<CoveoProcessParsedRestResponseArgs>
    {
        public void Process(CoveoProcessParsedRestResponseArgs args)
        {
            foreach (var result in args.ResponseContent.Results)
            {
                result.ClickUri = "http://localhost/";
                result.Raw["imagesurl"] = "http://localhost/";
            }
        }
    }

Registered it in config:


    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <pipelines>
          <coveoProcessParsedRestResponse>
            <processor patch:after="*[last()]" type="MyProject.Custom.Pipelines.Coveos.DefineAdditionalProductLinks, MyProject.Custom" />
          </coveoProcessParsedRestResponse>
        </pipelines>
      </sitecore>
    </configuration>

In result: on the client-side I don't see my modifications:

  • clickUri - same as it was before modifications.
  • raw.imagesurl - not defined at all.

Additional information: I can debug code and can see my changes in args.ResponseContent.Results after my code finished.

Question: What am I doing wrong? Why I can't see changes on the client-side?

Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, Dec 10, 2015 8:25 AM

2 questions for you:

  1. If you do a showconfig, is your processor really the last in line (we have another processor that modifies the clickableURI).

  2. Can you see any exceptions in your logs? An error when re-serializing the .NET object to JSON could explain your problem.

Gravatar for artem@brainjocks.com

Comment by artem, Dec 10, 2015 8:48 AM

  1. yes, it does, my processor registered as the last one.
  2. I can see some stange error

8168 20:38:11 WARN  The JSON message length exceeds the default length that is accepted by JavaScriptSerializer. Increasing accepted length to 2182172 characters.
8168 20:38:11 ERROR Unable to run the pipeline 'coveoProcessRestJsonResponse', it probably have been removed from the configuration.
Exception: System.InvalidOperationException
Message: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Source: System.Web.Extensions
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
   at Coveo.SearchProvider.Rest.Serialization.JsonSerializer.SerializeToStream[T](Stream p_Stream, T p_Object)
   at Coveo.SearchProvider.Rest.Processors.CoveoProcessRestJsonResponse.ParseJsonRestSearchResponseProcessor.SerializeResponse(SearchResponse p_ParsedContent)
   at Coveo.SearchProvider.Rest.Processors.CoveoProcessRestJsonResponse.ParseJsonRestSearchResponseProcessor.Process(CoveoProcessRestJsonResponseArgs p_Args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Coveo.Framework.Pipelines.PipelineRunner.Run(String p_PipelineName, PipelineArgs p_Args)

Could it be the reason? I'm going to change it js serializer to Newtone and will see will it help or not.

Gravatar for lbergeron@coveo.com

Comment by Luc Bergeron, Dec 10, 2015 9:01 AM

The error message you mentioned talks about the coveoProcessRestJsonResponse pipeline. Based on the error message, it looks like the pipeline definition is not in the configuration file anymore.

Can you do a showconfig and validate that the pipeline is configured like this?

<coveoProcessRestJsonResponse>
  <processor type="Coveo.SearchProvider.Rest.Processors.CoveoProcessRestJsonResponse.ParseJsonRestSearchResponseProcessor, Coveo.SearchProvider.Rest" />
</coveoProcessRestJsonResponse>
Gravatar for topazahmed@gmail.com

Comment by topaz, Sep 2, 2016 2:22 PM

i am trying to modify my rest response

so i inserted a pipeline before CoveoProcessParsedRestResponseArgs

but the CoveoProcessParsedRestResponseArgs only has results in it. CurrentContextItem is null.

I need that to get the custom coveo fieldnames any thoughts?

Gravatar for sbelzile@coveo.com

Comment by Sébastien Belzile, Sep 2, 2016 2:34 PM

@topaz, I will answer your question here: https://answers.coveo.com/questions/7259/how-do-i-get-coveo-field-name-on-a-coveo-pipeline

1 Reply
Gravatar for artem@brainjocks.com

Answer by artem, Dec 10, 2015 9:26 AM

I replaced JavaScriptSerializer with Newtone with steps provided here: https://developers.coveo.com/display/SitecoreV3/Altering+Search+Results+Before+They+Are+Displayed+in+a+Search+Interface and it started to work. Looks like the problem was in Json Serializer, which raises an error in coveoProcessRestJsonResponse pipeline.

But it's totaly unclear for me, why do I receive json result even if there was an error in "coveoProcessRestJsonResponse" pipeline?

Gravatar for jflheureux@coveo.com

Comment by Jean-François L'Heureux, Dec 17, 2015 5:29 PM

The error in one of your coveoProcessRestJsonResponse pipeline processors just prevented the JSON response to be modified. The original response received from the Coveo Search API was sent to the client browser.

Ask a question