Skip to Content

Most of the times the standard iViews provided with portal don’t suffice. We need to make some minor/major changes in functionalities. So by using WebDynpro and KM APIs, we can develop new iViews for all KM functionalities.
Many documents talk about it but here is one consolidated material for all of you.
Here I have considered only creation, deletion and Search operations but same procedure can be applied for other KM operations also.

Before coding complete following tasks:
1. Create the project of type WebDynpro.
2. Add all required jar files in the path.
3. Add sharing reference in WebDynpro references as :
PORTAL:sap.com/com.sap.km.application

image

4. Add the following UI elements:
Input Field inp1 for resource name, Text View tv1 Input Field inp2 for search query
5. Add the following Context Variables:
Rname of type string, Msg of type string, Query of type string
6. Context Mapping:
inp1 with Rname, tv1 with Msg, inp2 with Query
Now let us get started.

A. Create the collection in /documents repository
The function for creating the collection is as below:

public void onActionCreate(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent) {
try{
// Get the portal user
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
// Get the resource context
ResourceContext context = new ResourceContext(ep5User);
// Get RID
RID parentRID = RID.getRID(“/documents”);
ICollection parent = (ICollection) ResourceFactory.getInstance().getResource(parented,context) ;
// Create the collection
ICollection col = parent.createCollection(wdContext.currentContextElement().getRname().toString(),null);
wdContext.currentContextElement().setMsg(“Collection Created”);
} catch(Exception e){
wdContext.currentContextElement().setMsg(e.getMessage());
}
}

B. Delete the resource or collection in KM repository
The function for deleting the collection is as below:

public void onActionDelete
(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent) {
try{
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
// Get the resource context
ResourceContext context = new ResourceContext(ep5User);
// Get RID
RID rid = RID.getRID(wdContext.currentContextElement().getRname().toString());
IResource res= ResourceFactory.getInstance().getResource(rid,context);
if(res !=null){
res.delete();
wdContext.currentContextElement().setMsg(“Its Deleted !”);
}
else
wdContext.currentContextElement().setMsg(“No resource or collection found !”);
} catch(Exception e){
wdContext.currentContextElement().setMsg(e.getMessage());
}
}

C. View content of the resource

onActionViewContent(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent){
try{
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
// Get the resource context
ResourceContext context = new ResourceContext(ep5User);
// Get RID
RID rid = RID.getRID(wdContext.currentContextElement().getRname().toString());
IResource res= ResourceFactory.getInstance().getResource(rid,context);
if(res !=null){
// Now get the content
IContent content = res.getContent();
wdContext.currentContextElement.setMsg(content.toString());
}
else
wdContext.currentContextElement().setMsg(“No resource or collection found !”);
} catch(Exception e){
wdContext.currentContextElement().setMsg(e.getMessage());
}
}

D. Search for the resources under all indices.
Write the following code in onActionQueryEntered method

public void onActionQueryEntered(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent) {
String query = wdContext.currentContextElement().getQuery();
If(query==null || query.length==0){
wdContext.currentContextElement().setMsg(“No Query Parameter Specified”);
}
try{
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
// Get the resource context
ResourceContext context = new ResourceContext(ep5User);
// Get an object of index management service
IIndexService indexService = (IIndexService) ResourceFactory.getInstance().getServiceFactory().getService(IServiceTypesConst.INDEX_SERVICE);
// Build Query Object
SearchQueryListBuilder sqb = new SearchQueryListBuilder();
Sqb.setSearchTerm(query);
IQueryEntryList qel = sqb.buildSearchQueryList();
// Get instance of federated search
IFederatedSearch federatedSearch = (IFederatedSearch) indexService.getObjectInstance(IWcmIndexConst.FEDERATED_SEARCH_INSTANCE);
List indexList = indexService.getActiveIndexes();
// For Searching executions
ISearchSession session = null;
if(session == null)
session = federatedSearch.searchWithSession(qel,indexList,context);
ISearchResultList results = session.getSearchResults(1,session.getTotalNumberResultKeys());
ISearchResultListIterator iter = results.listIterator();
String r= “”;
while(iter.hasNext()){
ISearchResult result = iter.next();
IResource nresource = result.getResource();
r = r + nresource.toString() + “\n” ;
}
wdContext.currentContextElement().setMsg(r);
}catch(Exception e){
wdContext.currentContextElement().setMsg(e.getMessage());
}
}
Hope you enjoyed it !!

To report this post you need to login first.

4 Comments

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

  1. Raji Nair
    Hi,

    Thanks for the blog. Could you tell me which are the required jar files ? I’m getting class not found for few classes like WPUMFactory, ResouceContext, RID, ICollection I have rest of the common jars in classpath.

    Thanks,

    (0) 
    1. Raji Nair
      hi Vinod, really appreciate your help.I think there is a small typo in the blog

      onActionCreate()

      ICollection parent = (ICollection) ResourceFactory.getInstance().getResource(parented,context) ;

      in the above statement, “parented” should be “parentRID” i guess. Thanks again for your help !!

      Thanks,

      (0) 

Leave a Reply