Skip to Content

I needed to develop an iView which shows role content in SAP NetWeaver Portal. I just wanted to share my approach with the community. Here is what you need to do:

  1. In Development Infrastructure Perspective, create a Development Component with type Portal Application Standalone
  2. In your development component add references to
    • ENGFACADE tc/je/usermanagement/api
    • EP-BASIS-API tc/epbc/pcd/gl/api
  3. Switch to Enterprise Portal Perspective and create a new Portal Application Object, with DynPage type and an arbitrary name (e.g. in RoleContent)
  4. Upload images for role hierarchy Folder.gif, iView.gif, Page.gif, Role.gif, and Role.gif. You may download them from below respectively:
    • Folder.gif iView.gif Page.gif Role.gif Workset.gif
  5. Adjust dist/PORTAL-INF/portalapp.xml and add parameter TargetRoleName. This will appear in iView properties, thus you can enter role unique name to be displayed. Your xml content will look like:
    • <?xml version="1.0" encoding="utf-8"?>
      <application>
        <application-config>
          <property name="PrivateSharingReference" value="com.sap.portal.htmlb"/>
        </application-config>
        <components>
          <component name="RoleContent">
            <component-config>
              <property name="ClassName" value="com.saptr.portal.RoleContent"/>
            </component-config>
            <component-profile>
                 <property name="TargetRoleName" value="pcd:portal_content/administrator/super_admin/super_admin_role"/>
            </component-profile>
          </component>
        </components>
        <services/>
      </application>
  6. You will have a java class with name like RoleContentDynPage. Copy and paste codes in corresponding methods and adjust imports.
package com.saptr.portal;
 
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import com.sap.portal.directory.Constants;
import com.sap.security.api.IRole;
import com.sap.security.api.UMFactory;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.pcd.gl.IPcdAttribute;
import com.sapportals.portal.pcd.gl.IPcdContext;
import com.sapportals.portal.prt.component.IPortalComponentProfile;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentResponse;
public class RoleContent extends PageProcessorComponent {
 public DynPage getPage() {
  return new RoleContentDynPage();
 }
 public static class RoleContentDynPage extends DynPage {
  private InitialContext ctx;
  private IPortalComponentRequest request;
  private IPortalComponentResponse response;
     private String roleUniqueName;
     private String imgPath;
 
  /**
   * Initialization code executed once per user.
   */
  public void doInitialization() {
      Hashtable env = new Hashtable();
      try {
       request = (IPortalComponentRequest)this.getRequest();
       response = (IPortalComponentResponse)this.getResponse();
       env.put(Context.INITIAL_CONTEXT_FACTORY, IPcdContext.PCD_INITIAL_CONTEXT_FACTORY);
       env.put(Constants.REQUESTED_ASPECT, IPcdAttribute.PERSISTENCY_ASPECT);
       env.put(Context.SECURITY_PRINCIPAL, request.getUser());
       ctx = new InitialContext(env);
      } catch (Exception e) {
       e.printStackTrace();
      }
  }
  /**
   * Input handling code. In general called the first time with the second page request from the user.
   */
  public void doProcessAfterInput() throws PageException {
  }
  /**
   * Create output. Called once per request.
   */
  public void doProcessBeforeOutput() throws PageException {
   try {
    imgPath = request.getWebResourcePath() + "/images/";
    IPortalComponentProfile profile = request.getComponentContext().getProfile();
    roleUniqueName = profile.getProperty("TargetRoleName");
       IRole role = UMFactory.getRoleFactory().getRoleByUniqueName(roleUniqueName);      
       String roleRoot = role.getUniqueName();      
       response.write("<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\">");
       response.write("<tr>");
       response.write("<td><img src=\"" + imgPath + "Role.gif\"> Role</td>");
       response.write("<td><img src=\"" + imgPath + "Workset.gif\"> Workset</td>");
       response.write("<td><img src=\"" + imgPath + "Folder.gif\"> Folder</td>");
       response.write("<td><img src=\"" + imgPath + "Page.gif\"> Page</td>");
       response.write("<td><img src=\"" + imgPath + "iView.gif\"> iView</td>");
       response.write("</tr>");
       response.write("<tr>");
       response.write("<td colspan=\"5\">");
    try {
     IPcdContext rootRole = (IPcdContext)ctx.lookup(roleRoot);
     Object titleObj = rootRole.getAttributes("").get("com.sap.portal.pcm.Title");
     if (titleObj instanceof IPcdAttribute) {
      IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
      String title = titleAtt.get(request.getLocale());
      response.write("<strong>'" + title + "' (" + rootRole.getAtomicName() + ") Role Content</strong>");
     }
    } catch (Exception e) {
     response.write("<strong>'" + roleRoot + "' Role Content</strong>");
    }
       response.write("<ul>");
       this.getChildren(request, response, roleRoot);
       response.write("</ul>");
       response.write("</ul>");
       response.write("</td>");
       response.write("</tr>");
       response.write("</table>");
      } catch (Exception e) {
       response.write(e.getLocalizedMessage());
       e.printStackTrace();
      }
  }
 
  private void getChildren(IPortalComponentRequest request,IPortalComponentResponse response, String nodeName) {
      try{
       NamingEnumeration names = ctx.list(nodeName);
       if (names.hasMoreElements()) {
        NameClassPair nameClass;
        while (names.hasMore()) {
         nameClass = (NameClassPair) names.next();
         String newRoot = nodeName + "/" + nameClass.getName();
         IPcdContext pcdObj = (IPcdContext)ctx.lookup(newRoot);
         if(nameClass.getClassName().contains("Role")){         
          response.write("<ul>");         
          try {
           IPcdContext role = (IPcdContext)ctx.lookup(newRoot);
           Object titleObj = role.getAttributes("").get("com.sap.portal.pcm.Title");
           if (titleObj instanceof IPcdAttribute) {
            IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
            String title = titleAtt.get(request.getLocale());
            response.write("<li style=\"list-style-image: url(" + imgPath + "Role.gif);\">" + title + "</li>");
           }
          } catch (Exception e) {
           response.write("<li style=\"list-style-image: url(" + imgPath + "Role.gif);\">" + pcdObj.getAtomicName() + "</li>");
          }
          response.write("<ul>");
          this.getChildren(request, response, newRoot);
          response.write("</ul>");
          response.write("</ul>");
         } else if(nameClass.getClassName().contains("Workset")){         
          response.write("<ul>");
          try {
           IPcdContext workset = (IPcdContext)ctx.lookup(newRoot);
           Object titleObj = workset.getAttributes("").get("com.sap.portal.pcm.Title");
           if (titleObj instanceof IPcdAttribute) {
            IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
            String title = titleAtt.get(request.getLocale());
            response.write("<li style=\"list-style-image: url(" + imgPath + "Workset.gif);\">" + title + "</li>");
           }
          } catch (Exception e) {
           response.write("<li style=\"list-style-image: url(" + imgPath + "Workset.gif);\">" + pcdObj.getAtomicName() + "</li>");
          }
             response.write("<ul>");
             this.getChildren(request, response, newRoot);
             response.write("</ul>");
             response.write("</ul>");
         }else if(nameClass.getClassName().contains("Folder")){
             response.write("<ul>");
          try {
           IPcdContext folder = (IPcdContext)ctx.lookup(newRoot);
           Object titleObj = folder.getAttributes("").get("com.sap.portal.pcm.Title");
           if (titleObj instanceof IPcdAttribute) {
            IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
            String title = titleAtt.get(request.getLocale());
            response.write("<li style=\"list-style-image: url(" + imgPath + "Folder.gif);\">" + title + "</li>");
           }
          } catch (Exception e) {
           response.write("<li style=\"list-style-image: url(" + imgPath + "Folder.gif);\">" + pcdObj.getAtomicName() + "</li>");
          }            
             response.write("<ul>");
             this.getChildren(request, response, newRoot);
             response.write("</ul>");
             response.write("</ul>");
         }else if(nameClass.getClassName().contains("Page")){
          response.write("<ul>");         
          try {
           IPcdContext page = (IPcdContext)ctx.lookup(newRoot);
           Object titleObj = page.getAttributes("").get("com.sap.portal.pcm.Title");
           if (titleObj instanceof IPcdAttribute) {
            IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
            String title = titleAtt.get(request.getLocale());
            response.write("<li style=\"list-style-image: url(" + imgPath + "Page.gif);\">" + title + "</li>");
           }
          } catch (Exception e) {
           response.write("<li style=\"list-style-image: url(" + imgPath + "Page.gif);\">" + pcdObj.getAtomicName() + "</li>");
          }         
          response.write("<ul>");
          this.getChildren(request, response, newRoot);
          response.write("</ul>");
          response.write("</ul>");
         }else if(nameClass.getClassName().contains("iView")){         
          try {
           IPcdContext iview = (IPcdContext)ctx.lookup(newRoot);
           Object titleObj = iview.getAttributes("").get("com.sap.portal.pcm.Title");
           if (titleObj instanceof IPcdAttribute) {
            IPcdAttribute titleAtt = (IPcdAttribute) titleObj;
            String title = titleAtt.get(request.getLocale());
            response.write("<li style=\"list-style-image: url(" + imgPath + "iView.gif);\">" + title + "</li>");
           }
          } catch (Exception e) {
           response.write(e.getMessage() + "<br>");
          }
         }
        }
       }
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
 }
}
To report this post you need to login first.

1 Comment

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

Leave a Reply