Skip to Content

Uploading files to KM repository using Webdynpro APIs

            Recently we came across a scenario in which we were required to upload files to Knowledge management repository based on a path generated at runtime. We used the Portal APIs available in Webdynpro for creating the interface for file upload.

          You will have to add the Portal APIs specified below to access the Portal APIs. If you are a adding the jar files from server, you should be having permissions to access folders in the server. Set the path for the classpath variables -WEBAS_HOME and PORTAL_HOME. We have to extend these two classpath variables
Go to Window >preferences>java>classpath variables
    Click on new and give WEBAS_HOME as variable name and give path as “ \\server name\usr\sap\J2E\JC00\j2ee\cluster\server0”

Click on new and add PORTAL_HOME and give path as “ WEBAS_HOME \apps\\irj\servlet_jsp\irj\root\WEB-INF\portal\”


   &nbspExtend WEBAS_HOME and click extend . Navigate to \bin\ext\\, and include that jar file

Extend PORTAL_HOME and include the following jar files
1) \lib\prtapi.jar
2) Portal user management API  –  \portalapps\\lib\ p5.jar
3) \portalapps\\lib\bc.rf.framework_api.jar
4) KM Repository Framework APIs –  \portalapps\\lib\
5) Repository Framework Utility: URL Generator –  \portalapps\\lib\bc.sf.framework_api.jar
6)Repository Framework: Repository Services \portalapps\\lib\bc.util.public_api.jar

 Go to webdynpro refernces>sharing references in properties of webdynpro project

Create a file node containing 2 attributes filename and filedata.This filename can be used to access the name of the file.The data stored in the file is being passed as a binary stream.
Therefore the filedata should be of binary type.

fileelement = wdContext.createFilenodeElement();
/*Make a simple type of that type*/
  IWDAttributeInfo attInfo = wdContext.nodeFilenode().getNodeInfo().getAttribute(“filedata”);
 ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();

Following is the code for uploading files

/*Get an object of current Portal user */ IWDClientUser wdClientUser =
 WDClientUser.getCurrentUser(); sapUser = wdClientUser.getSAPUser();
/*create an ep5 user from the retrieved user
 IUser ep5User =WPUMFactory.getUserFactory().getEP5User(sapUser);
 ResourceContext context = new ResourceContext(ep5User);
/*Give the path to KM in the variable path */
 RID rid = RID.getRID(path);
 IResourceFactory factory =
 ICollection folder = (ICollection) factory.getResource(rid,context);
 Using the upload element we can upload the files to a location in the server drive
/*temperory location for writing */
 String location = “give the location here”;
 String fileName = location+fileelement.getFilename();
 file = new File(fileName);
/*Create an output stream for writing to the temperory location*/
 FileOutputStream out = new FileOutputStream(file);
/*From the temporary location read the file using an input stream*/
 FileInputStream fin = new FileInputStream(fileName);;
/*Using this input stream we can write to the repository
 Content content = new Content(fileelement.getFiledata(),fileelement.get) */
 Content content = new Content(fin,”byte”, -1);
 IResource newResource = folder.createResource(fileelement.getFilename(),null, content);
After uploading files to KM repository delete the file from its temporary location .Close the input stream when you have uploaded
write the above code within a try-catch block. After writing the code organize imports .

These are the imports to be organised
import com.sapportals.wcm.repository.Content;
import com.sapportals.wcm.repository.ICollection;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.IResourceContext;
import com.sapportals.wcm.repository.IResourceFactory;
import com.sapportals.wcm.repository.IResourceList;
import com.sapportals.wcm.repository.IResourceListIterator;
import com.sapportals.wcm.repository.ResourceContext;
import com.sapportals.wcm.repository.ResourceFactory;
import com.sapportals.wcm.util.uri.RID;
import com.sapportals.wcm.util.usermanagement.WPUMFactory;

 Now you can deploy the application in the server.Access it through Portal by creating a webdynpro iview .

You must be Logged on to comment or reply to a post.
  • Hi, rehit:

      I have same task. I can have written portal components to upload thousand of files if those files sit in EP6 portal. I have diffilcut to update load multiple file from local file. I try to use Mappying drive in EP 6 poral server, but portal component cannot access mapping drive.

       your code sounds a good solution for me. but I did not see anything in your code to demostrate how to upload multiple files from local to KM.  can you shed more light on that issue?


    Tony qiu

    • Hi Tony,

      Make an object of file and set the path to the required folder
      File fileEle = new File("path");
      Make an array of files having the same size as the number of files in the folder
      File[] files = new File[fileEle.size]
      Then if you move all the files in the folder to an array
      files = fileEle.listFiles();
      Loop the array and access these file elements
      while ()

      Then loop the files andset the path accordingly
      You can upload multiple files this way


      • Hi,rohit:

            thanks for your prompted response. after i read your code, it requires that uploading files must be sit on Server. if I want to upload files from client side, it won't work. because "File fileEle = new File("path")" cannot access files outside of Server.

            maybe i am wrong, can you correct my interpretaton?


           Tony qiu

        • Hi Tony,
            You are right .It can only access files within the server. I dont think it is possible to upload multiple files to km from client side.Any way i will work on that .You better put a query in Webdynpro forum . see if anybody can help you.
          • hi, Rohit:
                thanks very much. Actually, I did same as you did. i read filename,metadata from Excel spreadsheet and upload thousands of files to KM and set metadata. it works fine.  however my customers will upload files from local computer, I am struggling on that.


  • You can upload multiple files to the KM repository by first creating a Zip file (using Jar if file names contain non-english chars such as ñ) using a simple fileUpload component.

    Afterwards, using the* API instantiate/create each of the files contained in the zipFile.

    Implement the suggested code above for each file.

    That's how I did it 😉

  • Hello,
    Very Good Weblog
    I want to create a new upload for my KM repository. I want to use Web Dynpro to add new functionalities to the standard upload. The thing is, I have created some new properties and metadata to add it to my documents and actually I can see them in the Properties tab in the Upload Function. My question is: How can I use this properties component or from where I can read all the properties to show them and customize them in my new program?

    Thanks a lot

  • When I try to upload files to the km using this method, I can only upload text files. But when I try files with differnet mime types (pictures for example) the link in the KM shows me meaningless text instead of the picture (although the new file is uploaded).
    If I try:
    Content content = new Content(fin,binaryType
    .getMimeType().getHtmlMime(), -1L);

    instead of:
    Content content = new Content(fin,"byte", -1);

    The picture is uploaded with the right mime type (At least I think) but when I click the file in the KM I get the a small red cross which says that the picture is not available instead of the real picture)

  • Hi
    I m new to webdynpro. I have similar requirement of uploading folders into km. Can u please tell me as to where put this code in implementation area?Also what are the prerequsites(in the sense before using this code what all i need)
    • hi ana,

      u need to import the jar files mentioned in the blog into the project.

      u can write the code in the action which triggers the upload.

      for eg if the files r to be uploaded when a button is pressed, then write in the event handler of function.

      if there is anything else mail me @



  • Hi Rohit ,

    Thats a really nice blog , The problem we are facing is The method Iuser is depricated now in EP6 onwards so we cant use that .any pointers on that.

  • During the migration process problems have been reported for this blog. The blog content may look corrupt due to not supported HTML code on this platform. Please adjust the blog content manually before moving it to an official community.
  • even though it is deprecated it is still used

    see these links

    /thread/235656 [original link is broken]

    /thread/233710 [original link is broken]


  • Hi Rohit,
    When try to build WebDynproCO component for CAF GP, i get errors like:
    "package com.sapportals.wcm.repository does not exist
    [javac] import com.sapportals.wcm.repository.ICollection;"
    I guess it needs to link portal API through "Used DC". Please help resolve this problem.
  • Hi Rohit,

      WEBAS_HOME \apps\\irj\servlet_jsp\irj\root\WEB-INF\portal\”

    \\server name\usr\sap\J2E\JC00\j2ee\cluster\server0” to give above path.. where to give...
    2. where to write code in implementaion or any where...
    3.what is the part KM in portal...


    •                IPortalComponentRequest request =
                          (IPortalComponentRequest) this.getRequest();
                     IPortalComponentResponse response =
                          (IPortalComponentResponse) this.getResponse();
                 sapUser =,res);
                 ep5User =
                           ResourceContext context = new ResourceContext(ep5User);
                          /*Give the path to KM in the variable path */
                           RID rid = RID.getRID("/documents/DENEME");
                           IResourceFactory factory = ResourceFactory.getInstance();
                           ICollection folder = (ICollection) factory.getResource(rid,context);
                           String location = "E:\\DynproTempFolder\\";
                           String fileName = location+"a.pdf";
                           File file = new File(fileName);
      //                     IResource res_forMap = ResourceFactory.getInstance().getResource(rid, context);
      //                     IPropertyMap pm = res_forMap.getProperties();

                           String mime = new MimetypesFileTypeMap().getContentType(file);
                          /*Create an output stream for writing to the temperory location*/     
                          /*From the temporary location read the file using an input stream*/
                           FileInputStream fin = new FileInputStream(fileName);
                          /*Using this input stream we can write to the repository
                           Content content = new Content(fileelement.getFiledata(),fileelement.get) */
                           Content content = new Content(fin,"byte", -1L);
                           IResource newResource = folder.createResource("a.pdf",null,content);

  • Hi Rohit,

    Im trying to build a WebDynpro DC for CAF GP, but I get the following errors like:
    "package com.sapportals.wcm.repository does not exist"

    The Classpath is on, in the NWDS I got no Error Message before I would like Build the application.

    Hope you can help me...

    Have a nice day

  • Hi Rohit:

    I'm working with a remote server KM, and when I'm using fileElement.getName() and fileElement.getData() I have a conflict because are null and only fileElement.getResource() stores the file info for uploading (Maybe is a version problem).

    How can I write the data getting a file of my computer in a File of the server using FileOutputStream out.write() in another way?

  • Hi Rohit,

    The blog is really good. The file is also uploaded to KM . But when I goto portal KM and try to view the application ,
    It is not displaying.

    If I upload a word doc , when i try to view in KM , it is opening a dialog box, convert to , when i select word , it is display it is not right type...

    Similary for other type of files ,it is giving some other errors..

    Please help

    • String fileName = wdContext.currentFileElement().getFileResource().getResourceName();
      InputStream inputStream = wdContext.currentFileElement().getFileResource().read(false);

      File destfile = new File(fileName);

      FileOutputStream out = new FileOutputStream(destfile);

      if (inputStream != null)

      BufferedInputStream bufInStream = new BufferedInputStream(new FileInputStream(destfile));
      Content con = new Content(bufInStream, "byte", -1);
      IResource newResource = folder.createResource(fileName, null, con);

  • I am wondering if I am able to set the location as an URL for example


    So far I am getting the following error:

    http:/ (No such file or directory (errno:2))

    Any Ideas?


    Orlando Covault