cancel
Showing results for 
Search instead for 
Did you mean: 

Web Dynpro JAVA Application using to Upload and Download files KM of SAP Portal

vdurgarao09
Contributor
0 Kudos

Dear All

I have a requirement for client.Here 2 types of users available.User one upload the documents in KM using the WDJ application and same time that user can create the sub folder.once that folder is available in that upload the documents.This user is a admin type.An other user can see the document by using the WDJ application. this user is end user type and Read only.

->My question is by using the WDJ application to upload and download and creating the folders or sub folders in run time process.

I was checked this blow links but i am unable to get the information.

http://wiki.scn.sap.com/wiki/display/snippets/km+library+-+reusable+component+for+webdynpro+java?ori...

http://scn.sap.com/people/rohit.radhakrishnan/blog/2005/05/27/uploading-files-to-km-repository-using...

Please any Code /Document available means share me.

Thank for advance,

BR,

Durga Rao.

Accepted Solutions (1)

Accepted Solutions (1)

anil_kumar259
Active Contributor
0 Kudos

hi Durga rao,

File upload :

try {

          IPrivateCompView.ICtx_vn_FileDataElement element = wdContext.currentCtx_vn_FileDataElement();

      IWDResource resource = element.getCtx_va_FileResource();

      if(resource!=null){

      //To get the Current User

      IWDClientUser wdClientUser = WDClientUser.getCurrentUser();

      com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();

      com.sapportals.portal.security.usermanagement.IUser ep5User =WPUMFactory.getUserFactory().getEP5User(sapUser);

      ResourceContext resourseContext = new ResourceContext(ep5User);

      

      //Portal KM Path

      RID pathRID = RID.getRID("/documents/Links"); //Root Directory

      IResourceFactory resourseFactory =

      com.sapportals.wcm.repository.ResourceFactory.getInstance();

      ICollection collection = (ICollection)resourseFactory.getResource(pathRID,resourseContext);

    

      //Read the File

      InputStream stream = resource.read(true);

      String resourceName = resource.getResourceName();

      wdContext.currentContextElement().setFileName(resourceName);

      wdContext.currentZep_Sug_Submit_InputElement().setAttachment(resourceName);

      String resourceExtn = resource.getResourceType().getFileExtension();

      

      //KM Content

      Content cont = new Content(stream,"application/octet-stream",-1,"UTF-16");

      //create a file in above mentioned path in KM

      com.sapportals.wcm.repository.IResource new_resource =collection.createResource(resourceName, null, cont);

      //Get the File length

      long contentLen = new_resource.getContent().getContentLength();

      //If no data in file

      if(contentLen <= 0){

      wdComponentAPI.getMessageManager().reportException("No data in file" , true);

      }else {

      // wdComponentAPI.getMessageManager().reportMessage(IMessageUploadFile_KMComp.SF__UPLOAD,new Object[] {resourceName},true);

      

      }

    

      }else{

      element.setCtx_va_FileResource(null);

      }

     

      }catch (Exception e) {

      wdComponentAPI.getMessageManager().reportSuccess(""+e.getMessage());

      //wdComponentAPI.getMessageManager().reportWarning(e.printStackTrace());

  // TODO: handle exception

  }

File Download :

  IWDClientUser wdClientUser;

   try {

  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);

  //wdComponentAPI.getMessageManager().reportSuccess("resourseContext"+resourseContext);

  IResourceFactory resourseFactory = ResourceFactory.getInstance();

  //wdComponentAPI.getMessageManager().reportSuccess("resourseFactory"+resourseFactory);

  //path to the KM Folder ("/documents/Alice In Wonderland/alice.gif")

  RID pathRIDimg = RID.getRID(path);

  if(wdContext.currentOutputElement().getAttachment() != null){

  if((null != pathRIDimg )&&(null != resourseContext))

     {

     IResourceFactory factory = com.sapportals.wcm.repository.ResourceFactory.getInstance();

     com.sapportals.wcm.repository.IResource resource = factory.getResource(pathRIDimg, resourseContext);

     // wdComponentAPI.getMessageManager().reportSuccess("resourceimg :"+resource);

    

     String s = path;

     String result = s.substring(s.lastIndexOf('.')+1).trim();

     BufferedInputStream bufIn = new BufferedInputStream(resource.getContent().getInputStream());

      IWDCachedWebResource cachedWebResource = null;

      if(result.equalsIgnoreCase("xlsx")){

      cachedWebResource = WDWebResource.getWebResource(bufIn, WDWebResourceType.XLSX);

      String url = cachedWebResource.getUrl(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()) ;

      wdContext.currenDetailsElement().setDoc_link(url);

}

The above worked code worked perfectly for me.

vdurgarao09
Contributor
0 Kudos

Dear Anil kumar,

Thanks for the Reply.

I will be trying this one i will update later.Before can you tell me what are the jars i need to add and dynamically create folder in KM.It will possible are not tell me.means the location path should be dynamic creation.

Please help me.

BR,

Durga.

anil_kumar259
Active Contributor
0 Kudos

hi durga rao,

  what version of nwds and portal you are using. if it is 7.3 no need to add the jar files just add the following components as dependcies.

vdurgarao09
Contributor
0 Kudos

Dear Anil kumar,

Thanks for the Reply.

I am using nwds 7.31 sp12 .What you said that files not there.please tell me which file i need to configure.

BR,

Durga Rao.

anil_kumar259
Active Contributor
0 Kudos

Ask the basis team to get the KMC-CM and KMC-WPC sca and import them in the NWDS, then you can the above SC's like in below screenshot.

For reference follow the below thread

Regards,

Anil

vdurgarao09
Contributor
0 Kudos

Dear Anil,

Thanks for the Reply.

I checked file upload is working fine.But i want to create the folder in the KM in that folder i need to upload the files ,this process should be though code.once the folder is there the upload files need to save .Other wise need to  create the folder.same process to get the download files also.

Please help me.

Regards,

Durga Rao.

anil_kumar259
Active Contributor
0 Kudos

Hi Durga Rao,

for creating a folder in the KM pragmatically use the below code,

try {

IWDClientUser WDClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = WDClientUser.getSAPUser();
com.sapportals.portal.security.usermanagement.IUser epUser = WPUMFactory.getUserFactory().getEP5User(sapUser);
// wdComponentAPI.getMessageManager().reportSuccess(epUser.toString());
IResourceContext context = new ResourceContext(epUser);
RID rid = RID.getRID(“/documents”);
ICollection parent = (ICollection)ResourceFactory.getInstance().getResource(rid, context);
IResource resource = parent.createResource(“file”, null, null);
ICollection collection = parent.createCollection(“folder”, null);
wdComponentAPI.getMessageManager().reportSuccess(“uploaded”);
//ILink link = parent.createLink(″link″, ″/documents/file″, LinkType.INTERNAL, null);

} catch (ResourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WDUMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UserManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

vdurgarao09
Contributor
0 Kudos

Dear Anil,

Please tell me ,End user can just view the files in folder.It is passable to view the resource files in table format.

BR,

Durga Rao.

anil_kumar259
Active Contributor
0 Kudos

yes we can.Just create the table in wdj and dispaly all the docuements of the folder in the table.

vdurgarao09
Contributor
0 Kudos

Hi anil,

Can you tell me,How to get the RID path in dynamically and files link.please any code means share me.

thanks

Durga.

anil_kumar259
Active Contributor
0 Kudos

check this links

Answers (0)