Skip to Content
Recently in my project I came across a scenario where my Web Dynpro Project had to pick the image from KM. The images to be displayed will be placed in KM. This will avoid loading the images into the Web DynPro project. More over when you have KM installed on your EP server, one can use it for storing backend data and resources. The KM Admin will be undertaking this task of uploading the images to predefined folder structures. Through the application path to the image will be provided dynamically giving you the flexibility to decide which image to be displayed according to the business logic. Advantage: If the image is in KM repository, admin can change the image at any point of time without redeploying the application. Content Managers can have much more flexibility. Let’s begin from the KM configuration. 1) Create a folder in KM with name Alice In Wonderland. 2) Upload the image alice.gif. 3) Create a Web Dynpro project. The first thing one need to configure is the Web Dynpro Sharing Reference. This can be done by selecting the project->Properties->Web Dynpro Refernces-> Sharing References. Add the following entry here PORTAL:sap.com/com.sap.km.application image 4) Jar files to be added. bc.rf.framework_api.jar bc.rf.global.service.urlgenerator_api.jar bc.sf.framework_api.jar bc.util.public_api.jar com.sap.security.api.ep5.jar com.sap.security.api.jar prtapi.jar image Now the project you create can access KM. 5) Add a UI Element for Image in the view. Create a context attribute-String type with name “image”. Map the image ui element(Property->Source) to the context attribute “image” just created. In the normal scenario we used to provide the image name here and image will be included with the project. But here we will take the image dynamically from KM. 6) Detailed coding. I have written a java class ImageInfo, where a method “IWDWebResource getImage(String path)” which get called from the webdynpro view. The method will “return IWDWebResource” Method IWDWebResource getImage(String path) Imports to be added java.io.BufferedInputStream; java.io.IOException; com.sap.security.api.IUser; com.sap.tc.webdynpro.services.sal.um.api.IWDClientUser; com.sap.tc.webdynpro.services.sal.um.api.WDClientUser; com.sap.tc.webdynpro.services.sal.um.api.WDUMException; com.sap.tc.webdynpro.services.sal.url.api.IWDWebResource; com.sap.tc.webdynpro.services.sal.url.api.WDURLException; com.sap.tc.webdynpro.services.sal.url.api.WDWebResource; com.sap.tc.webdynpro.services.sal.url.api.WDWebResourceType; com.sapportals.portal.security.usermanagement.UserManagementException; com.sapportals.wcm.repository.AccessDeniedException; com.sapportals.wcm.repository.IResource; com.sapportals.wcm.repository.IResourceContext; com.sapportals.wcm.repository.IResourceFactory; com.sapportals.wcm.repository.ResourceContext; com.sapportals.wcm.repository.ResourceFactory; com.sapportals.wcm.util.content.ContentException; com.sapportals.wcm.util.uri.RID; com.sapportals.wcm.util.usermanagement.WPUMFactory; //Getting the user…… IWDClientUser wdClientUser = WDClientUser.getCurrentUser(); IUser sapUser = wdClientUser.getSAPUser(); com.sapportals.portal.security.usermanagement.IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser); //Getting the Resource……… IResourceContext resourseContext = new ResourceContext(ep5User); IResourceFactory resourseFactory = ResourceFactory.getInstance(); //path to the KM Folder (“/documents/Alice In Wonderland/alice.gif”) RID pathRIDimg = RID.getRID(path); com.sapportals.wcm.repository.IResource resourceimg = resourseFactory.getResource(pathRIDimg, resourseContext); //Reading the image file…… BufferedInputStream bufIn = new BufferedInputStream(resourceimg.getContent().getInputStream()); byte[] imagebyte = new byte[bufIn.available()]; bufIn.read(imagebyte); //Mapping the image as a WebResource…. IWDWebResource webResource = WDWebResource.getWebResource( imagebyte, WDWebResourceType.JPG_IMAGE); 7)In the view we use following code to call the above method of the class-ImageInfo ImageInfo imageInfo = new ImageInfo (); IWDWebResource webResource = imageInfo.getimage(“/documents/Alice In Wonderland/alice.gif “); 8) Now finally webResource is being mapped to context attribute image wdContext.currentContextElement().setImage(webResource.getURL()); 9)The image will be displayed.
To report this post you need to login first.

9 Comments

You must be Logged on to comment or reply to a post.

  1. Vijith Kumar
    Hi Bobu,

    Great Blog .. Why dont you post the Blog in Web Dynpro Category as well … I have a doubt in the way you have approached ….. Do you think that hard coding the path is a good practice also.. Will there be any issues when we migrate the content ? ……

    Vijith

    (0) 
    1. Bobu George Putheeckal Post author
      Hi Vijith,

      The first point “hard coding the path is a good practice also..”,…. will it be a good practice to include all the images into the WDP project???and more over how convenient it will be to change those image after a deployment???

      The second point, if the portal is getting migrated from ep6 to ep7, the folders will also be migrated also, the WDP project’s ear file.
      as far as my knowledge if these two conditions succeeds successfully, there wont be any problem.

      Regards

      BP

      (0) 
  2. George Jacob
    Hi Bobu,

    Nice Blog, but I have a point that requires your attention.

    KM Images are not the alternative if you want to change the images after deploying. Actually there is no need for an alternative, WD provides this feature.

    Once an application is deployed, a folder will get created in the server.

    For example I created a project “KMTest1”,( Package: com.km, Application: Test). Uploaded an image file “Alice.jpg” in the mimes part of it.

    After uploading this I will suggest you to go and check for \usr\sap\J2E\JC01\j2ee\cluster\server0\temp\webdynpro\web\local\KMTest1\Components\com.km.Test
    folder in the hard disk.

    You can see the image uploaded in there. Also you will be able allowed to replace the image with different one after deploying also (not allowed to change its name).

    Cheers
    gEorgE

    (0) 
    1. Ranjith Vijayan
      Hi Bobu,

      Good work. Expecting more from your end in the future.

      Hi George,

      I have to disagree with you, when you say there is no need for a similar alternative. The feature that WD provides, as you have explained it, has one little problem.

      It requires the Content Admin to have access to the server at the file system level. But this kind of access is not required when you use the proposed solution of storing images in KM.

      So I believe the proposed solution is a better alternative than the WD provided feature.

      Regards
      Ranjith

      (0) 
  3. Fahad Hamsa
    Hi,

    It was very helpful for me.
    I am facing a problem in this.
    The example u had given is working fine for me.
    But when I tried the same for jpeg,PDF,DOC, etc, it is not working. What could be the problem? Can u please guide me regarding this.

    Thanks

    Fahad hamsa

    (0) 

Leave a Reply