cancel
Showing results for 
Search instead for 
Did you mean: 

query webscript from share dashlet

kay_be_
Champ in-the-making
Champ in-the-making
I need some clarification on how to get a second result from a webscript when the dashlet is already created. Let me explain. I created a webschript in alfresco that queries the companies of the users. Then I made a dashlet that lists these companies. Each company is a link, when the user clicks on a company, a list appears of the members with their emailadresses.
How can I make that second call to a different webscript and return the data in the dashlet?
I tried to solve it with javascript using this dashlet template:
<script type="text/javascript">//<![CDATA[
   new Alfresco.widget.DashletResizer("${args.htmlid}", "${instance.object.id}");
   function getjson(e){
      var name = e.id;
      var scriptURL = "/RedTree/emaillist?company=" + name + "&format=json";
      alert(scriptURL);
      var connector = remote.connect("alfresco");
      alert ("connector ok");
      var json = connector.call(scriptURL);
      if (json.status == 200) {
         var obj = eval('(' + json + ')');
         alert('done');
      }else {
      alert('failed');
      }
      
   }

//]]></script>
<div class="dashlet">
   <div class="title">${msg("header")}</div>
   <p style="color:green;font-size:10px;font-weight:bold;">Click a company to view the memberlist with contact details.</p>
   <div class="body scrollableList" <#if args.height??>style="height: ${args.height}px;"</#if>>
     <div class="detail-list-item first-item last-item">
      <#list result.companylist as company>
         <a id="${company.companyname}" href = "#" onclick="getjson(this);">${company.companyname}</a>
      </#list>
     </div>
   </div>
</div>

But this doesn't work out well. I even don't know if this is the way to go.
I don't get to the check that alerts that connector is made.
The alfresco script /RedTree/emaillist?company=" + name + "&format=json works fine and results in json data.
Where did I went wrong?
4 REPLIES 4

jpotts
World-Class Innovator
World-Class Innovator
You tried to use the "remote" object, which only works in a server-side controller, in client-side JavaScript. You've got the right idea–you essentially want to do an AJAX call to request some data and then replace the content of the dashlet with content from the second web script call. You can do that, but it will take a bit more work than what you've done so far.

You might want to have a look at the My Documents dashlet and how it works. It has a little dropdown that consists of "My Favorites", "I'm Editing", and "I've Recently Modified". When you switch between these, the dashlet does a GET to grab the appropriate set of documents and rebuilds the list. You'll do something similar, except that instead of invoking the GET on a dropdown change, you'll do it when one of the links is clicked.

Hope that helps,

Jeff

kay_be_
Champ in-the-making
Champ in-the-making
Ok thanks. I tried to figure out how the ajax call was made in my-documents dashlet but it seems quite complicated  Smiley Surprisedops:  I used YUI ajax call to the webscript but I receive the following error as a response:
javax.servlet.ServletException: Could not resolve view with name 'user/admin/alfresco/services/RedTree/emaillist' in servlet with name 'Spring Surf Dispatcher Servlet'

My JS with ajax call (companyemaillist.get.html.ftl) = dashlet:
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/ajax/yahoo/connection/connection-min.js"> </script>
<script type="text/javascript" src="<%=request.getContextPath()%>/scripts/ajax/common.js"> </script>
<script type="text/javascript">//<![CDATA[
   new Alfresco.widget.DashletResizer("${args.htmlid}", "${instance.object.id}");
//]]></script>

<script type="text/javascript">
   function getjson(e){
      alert('start');
      var name = e.id;
      var scriptURL = "alfresco/services/RedTree/emaillist?company=" + name + "&format=json";
      alert(scriptURL);
      YAHOO.util.Connect.asyncRequest("GET", scriptURL,
         {
            success: handleInfo,
            failure: handleErrorYahoo
         },
         null);
         
   }
   function handleInfo(response) { alert('succes' + response.responseText);}
   function handleErrorYahoo(response) { alert('failed…' + response.responseText);}
</script>
<div class="dashlet">
   <div class="title">${msg("header")}</div>
   <p style="color:green;font-size:10px;font-weight:bold;">Click a company to view the memberlist with contact details.</p>
   <div class="body scrollableList" <#if args.height??>style="height: ${args.height}px;"</#if>>
     <div class="detail-list-item first-item last-item">
      <#list result.companylist as company>
         <a id="${company.companyname}" href = "#" onclick="getjson(this);">${company.companyname}</a>
      </#list>
     </div>
   </div>
</div>

The webscript JS that searches the mailadresses for a certain company (get-emaillist.get.js)
function main() {
var companyname = args.company;
var query = "TYPE:\"{http://www.alfresco.org/model/content/1.0}person\" AND @cm\\:organization:\"" + companyname + "\"";
var userList = search.luceneSearch(query);
var length = userList.length;
eval("model.userList = userList");
eval("model.company = companyname");
}

main();

the response in json (get-emaillist.get.json.ftl)

<#escape x as jsonUtils.encodeJSONString(x)>

{ "${company}":
   [
   <#list userList as child>
      {
      "firstname": "${child.properties.firstName}",
      "lastname": "${child.properties.lastName}",
      "email": "${child.properties.email}" 
      }
      <#if !(child == userList?last)>,</#if>
   </#list>
   
   ]
   
}   
</#escape>

I hoped this webscript get-emaillist would return the json object but it doesn't. 

Also I 've found this: http://www.unorganizedmachines.com/site/software-and-technology/34-software-development/97-calling-w... Maybe something I can give it a try?

Did I made a mistake in assigning my scriptURL?

erikwinlof
Confirmed Champ
Confirmed Champ
When constructing the url you must make sure its going "through" Share's proxy to reach the repository webscript.

Try changing your url from:
var scriptURL = "alfresco/services/RedTree/emaillist?company=" + name + "&format=json";
to:
var scriptURL = Alfresco.constants.PROXY_URI + "RedTree/emaillist?company=" + name + "&format=json";

Cheers, Erik

kay_be_
Champ in-the-making
Champ in-the-making
Ok, now it works! I'll have to read more about proxy in alfresco but at first sight it makes sense. I got my JSON now and I can integrate the response with javascript. Thanks both!
Regards