Render an entity in a string and return it through an AJAX callback in a custom form I use the code snippet from:
render a drupal 8 entity in a string

$node = \Drupal::entityManager()-&gt;getStorage('node')-&gt;load($nodeid);
$view_builder = \Drupal::entityManager()-&gt;getViewBuilder('node');
$renderarray = $view_builder-&gt;view($node, 'full');
$html = \Drupal::service('renderer')-&gt;renderRoot($renderarray);


It works in a normal controller class without problems. But I did not get it to work in an Ajax Callback in a Custom Form. 

The $html = \Drupal::service('renderer')->renderPlain($build);  returns nothing.

Where ist the problem? Here is the reduced code of my test form:

&lt;?php

namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;


class SearchApiPageBlockForm extends FormBase {

  public function getFormId() {
    return 'search_api_page_block_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state, $args = array()) {

    $form['form']['actions'] = array('#type' =&gt; 'actions',);
    $form['form']['actions']['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; $this-&gt;t('TESTBUTTON!'),
      '#ajax' =&gt; array(
        'callback' =&gt; '::myAjaxCallback',
      ),
    );

    return $form;
  }

  public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  }


  public function myAjaxCallback(array &amp;$form, FormStateInterface $form_state) {

    $view_builder = \Drupal::entityTypeManager()-&gt;getViewBuilder('comment');
    $storage = \Drupal::entityTypeManager()-&gt;getStorage('comment');
    $node = $storage-&gt;load(103); //cid 103 already exists
    $build = $view_builder-&gt;view($node);

    dpm('before rendering'); // -&gt; dpm output is: 'before rendering' 

    $html = \Drupal::service('renderer')-&gt;renderPlain($build);  

    dpm($html);// -&gt; dpm has no output 
    dpm('after rendering');// -&gt; dpm has no output

    }

}


My goal is, to return the html of an entity via the 

$response-&gt;addCommand(new HtmlCommand('#testtmp_message', $output));


UPDATE: Content of build:
dpm(build)
The code in my question is OK. The problem was caused by a conflict by calling the form from hook_preprocess_field(). I changed the hook, and I am calling the form from template_preprocess_block() and the AJAX callback of the entity gets fine.

4k4 put me on the right track. Thank you.