How do I hide widget when executing a vm file as ServiceUser?

Hi, I've run into a weird issue while trying to execute a vm file that does a search on content not publicly available. 

Content file:

$core_v2_widget.ExecuteFile("search.vm", "%{ RunAsServiceUser = 'True'}")

search.vm:

#set ($searchQuery = "%{}")
$searchQuery.Add("Query", "test")
$searchQuery.Add("ContainerId", "containter_id_here")
#set ($response = $core_v2_searchResult.List($searchQuery))

#if ($response.Count > 0)
    #foreach($item in $response)
    #beforeall
    <div>
    #each
        $item.ViewHtml()
    #afterall
    </div>
    #end
#else
    $core_v2_widget.Hide()
#end

When running this, when there are no results, it just displays "$core_v2_widget.ExecuteFile("search.vm", "%{ RunAsServiceUser = 'True'}")" as the widget content. Works fine when there *are* results. It seems to be erroring out on having that widget.Hide() code called in the foreach loop. I've tried moving things around but it really doesn't like having the widget.Hide() code in search.vm no matter how I set it up.

I've read that running a file as ServiceUser results in only the data/content in that file being run being accessible in itself (can't access variables in search.vm from Content), so I'm not sure how to work this out so that I can call that widget.Hide function when there are not results if I can't do it in search.vm without getting an error and can't move hideWidget to Content b/c Content can't see anything that's happening in search.vm.

Is there another way to get something like this to work?

Thanks,

Wendy

  • Two options for you:

    • You could change the call to $!core_v2_widget.ExecuteFile... and it will render empty when null is returned.
    • You can use the following snippet to read the result of the Execute and hide the widget accordingly (ExecuteFile will not set a value if null is returned):
      • #set($result = false)
        #set($result = $core_v2_widget.ExecuteFile....)
        #if (!$result)
          $core_v2_widget.Hide()
        #end

    Also a note, that in v12 this behavior has been altered and calling Hide() in an executed file does work to hide the widget.