Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member193577
Active Contributor

Hi,


If you develop portal applications, you probably find yourself sometimes searching for best way to pass parameters (configuration or others) from java code(server) into Javascript (client side)  for easy to use later.


In this blog post you can find a code sample of how to pass parameters/properties from Portal Component and then easily read these in Javascript.


This works if you are using the ajax framework page or your custom framework page using the LSAPI mechanism


Java code for the portal component:



import com.sap.portal.navigation.afp.IAFPHelperService;
import com.sapportals.portal.prt.component.*;
import com.sapportals.portal.prt.resource.IResource;
import com.sapportals.portal.prt.runtime.PortalRuntime;
public class Test extends AbstractPortalComponent
{
private static String COMP_NAME = "MyTestComponent";
public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)
{
IPortalComponentContext componentContext = request.getComponentContext();
IPortalComponentProfile profile = componentContext.getProfile();
String myProfileProperty = profile.getProperty("myProperty");
IAFPHelperService afpHelperService = (IAFPHelperService) PortalRuntime.getRuntimeResources().getService(IAFPHelperService.KEY);
afpHelperService.addClientSideAttribute(COMP_NAME, "myProperty", myProfileProperty);

IResource myJS = request.getResource(IResource.SCRIPT,"scripts/myAppJS.js");
response.include(request, myJS);
}
}


 

In this sample I assume the portal component has (in its PortalApp.xml) a property named "myProperty".  (see related blog on how to read/set properties)

  • First we need to retrieve the AFPHelperService (line 19)

  • Then, call the addClientSideAttribute method with your component name or any namespace you would like to use , the property and the property value. (line 20).

  • In next lines I'm just including a javascript resource that I have.


Server side code is done!

 

For using the AFPHelperService , you would need reference to it in PortalApp.xml. add sharing reference as you see bellow:
  <application-config>
<property name="PrivateSharingReference" value="com.sap.portal.navigation.afp.helperservice"/>
</application-config>

 

Reading the attributes from client side - Javascript code:
var SERVER_COMP = "MyTestComponent";
var topFrame = EPCM.getSAPTop();
var propFromServer = topFrame.LSAPI.AFPPlugin.configuration.getClientSideAttributeValue(SERVER_COMP, "myProperty");
alert("myProperty = " + propFromServer);



  • First we need to get the top frame of the framework page where LSAPI is located. EPCM.getSAPTop().

  • Actual work is being done in line 3, retrieve the parameter saved on server side by calling the method LSAPI.AFPPlugin.configuration.getClientSideAttributeValue with the namespace and name of the property

  • Result of this would be an alert with property set from server side - "This is my value!":


Once the attributes were set in server side, you will be able to retrieve them in any javascript (in the same framework page session of course)

Last step is of course creating an iView from this portal component and test it!



Related Links:

Ajax Framework Page

L-Shape API

Enjoy!

Tal

You want to influence the future product roadmap of Enterprise Portal? You plan to implement your portal in the Cloud? Then you should participate in the following survey https://blogs.sap.com/2016/10/11/2016-elections-vote-now/.