EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB32942: How to execute a Report Services Document and loop over the elements in the Group By Section to then export to PDF each group by selection using the MicroStrategy Web API


Community Admin

• Strategy


The following code snippet will execute the Report Services Document, loop through the elements in the page by and export each available element to PDF

The following screenshot illustrates a Report Services Document with an attribute in the Grouping Section:
 

ka04W000000Og9JQAS_0EM440000002GuX.jpeg

 
The following code snippet will execute the Report Services Document, loop through the elements in the page by and export each available element to PDF
 


public static void executeDocument() throws Exception {
  //Create connection with the Intelligence Server:
  iSession = createSession("localhost", "Strategy Tutorial", "administrator", "");
  //Create a new RWInstance from this session for a given Document ID.
  //Fetch the results in PDF View
  RWInstance rwi = iSession.getFactory().getRWSource().getNewInstance("88CFDDD442725CC86040D6BC9843A1B8", EnumRWExecutionModes.RW_MODE_PDF);
  //Get the prompts collection
  WebPrompts oprompts = rwi.getPrompts();
  
  WebElementsPrompt prompt = null;
  
  //Answer all prompts with default answers
  for(int i = 0; i < oprompts.size(); i++) {
   prompt = (WebElementsPrompt)oprompts.get(i);
   prompt.setAnswer(prompt.getDefaultAnswer());
  }
  
  oprompts.validate();
  oprompts.answerPrompts();  
  
  //Make sure execution has finished in the Intelligence Server by checking the status
  while (rwi.pollStatus() != 1) Thread.sleep(2000);
  
  //rwi has been executed, find drop down control elements
  try{
   RWDefinition def = rwi.getDefinition();
   String key = null;
   rwi.getData();
   
   for (int i=0;i<def.getGroupBys().size();i++){
    RWGroupBy gb = def.getGroupBys().get(i);
    
    if(gb.getLevel() == EnumRWGroupByLevels.RWLEVEL_GROUP_START){
    
     RWGroupByUnit gbu = gb.getUnit();
     RWGroupByElements elem = gbu.getElements();
     key = gb.getKey();
     
     for (int x=0;x<elem.size();x++){
      System.out.println("Exporting: " + elem.get(x).getDisplayName());
      saveDocument(rwi,elem.get(x).getID(),key,elem.get(x).getDisplayName());
     }
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 private static void save(byte[] data, String fileName) throws Exception {
   FileOutputStream fos = new FileOutputStream("c:\\temp\\" + fileName + ".pdf");
   fos.write(data);
   fos.close();
 }
 
 private static void saveDocument(RWInstance rwi, String element, String key, String fileName){
  try{
   
   RWManipulation rwManip = rwi.getRWManipulator();
   RWInstance newInst = null;
    
   rwManip.setCurrentGroupByElement(key, element, true);
   
   newInst = rwManip.applyChanges();
   RWExportSettings expSet = newInst.getExportSettings();
   
   expSet.setMode(EnumRWExportModes.RW_EXPORT_CURRENT);
   
   while (newInst.pollStatus() != 1) Thread.sleep(2000);
   
   //Once results are ready, fetch the data.
   data = newInst.getPDFData();
   save(data,fileName);
   
  }catch (Exception e) {
   e.printStackTrace();
  }
 }

 
 
Full Code can be found here
 

ka04W000000Og9JQAS_0EM440000002Guk.png

RWDLoopPDF
 
The code will generate a PDF for each element in the page by including the all selection
 

ka04W000000Og9JQAS_0EM440000002Gul.jpeg

 
 
CUSTOMIZATION WARNING
 
This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. In the event of a code change in future builds, Strategy may not be able to provide additional code on this matter even though this customization is provided at this time for this specific build. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy consulting assistance.
 


Comment

0 comments

Details

Knowledge Article

Published:

June 22, 2017

Last Updated:

June 22, 2017