Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos


            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\sap.com\irj\servlet_jsp\irj\root\WEB-INF\portal\”



   &nbspExtend WEBAS_HOME and click extend . Navigate to \bin\ext\com.sap.security.api.sda\com.sap.security.api.jar, and include that jar file

Extend PORTAL_HOME and include the following jar files
1) \lib\prtapi.jar
2) Portal user management API  -  \portalapps\com.sap.portal.usermanagement\lib\com.sap.security.api.e p5.jar
3) \portalapps\com.sap.netweaver.bc.rf\lib\bc.rf.framework_api.jar
4) KM Repository Framework APIs -  \portalapps\com.sap.netweaver.bc.rf.service\lib\bc.rf.global.service.urlgenerator_api.jar
5) Repository Framework Utility: URL Generator -  \portalapps\com.sap.netweaver.bc.sf\lib\bc.sf.framework_api.jar
6)Repository Framework: Repository Services \portalapps\com.sap.netweaver.bc.util\lib\bc.util.public_api.jar

 Go to webdynpro refernces>sharing references in properties of webdynpro project
Add PORTAL:sap.com/com.sap.km.application


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();
wdContext.nodeFilenode().bind(fileelement);
/*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(); com.sap.security.api.IUser 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 =
 ResourceFactory.getInstance();
 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);
 out.write(fileelement.getFiledata());
 out.close();
/*From the temporary location read the file using an input stream*/
 FileInputStream fin = new FileInputStream(fileName);
 fin.read();
/*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
 
 fin.close();
 file.delete();
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.portal.security.usermanagement.IUser;
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 .
27 Comments