Friday, June 15, 2012

Extract PCD Objects from Portal

If their is any requirement where portal Administrator wants to see all the contents of Portal Content Directory and along with it wants to see Code Link (type of iView/System). They need to implement following code.


  1. Create a Par File.

    • Go to File -> New -> Other
    • Select Portal Application -> Create a Portal Application Project. -> Next
    • Give Project Name and pecify root folder path -> Finish
       
    • Again Right click on you project -> New -> Other
       
    • Select Portal Application -> Create a New Portal Application Object -> Next -> Next
       
    • Select AbstractPortalComponent  -> Next
       
    • Give Name, Location where you want to create this java file and package name. -> Finish
  2. Code:
    • Decalre following objects inside class .

    IiView iView = null;
    InitialContext ctx = null;
    DirContext dirCtx;
    IPcdContext context;
    List iViewList = null;
    String location = null;

    In DoContent Method write following code .

    InitialContext ctx = null;
    DirContext dirCtx;
    IPcdContext context;
    List iViewList = null;

    try
    {
     Hashtable env = new Hashtable();
     env.put(IPcdContext.SECURITY_PRINCIPAL, request.getUser());
     env.put(Context.INITIAL_CONTEXT_FACTORY, IPcdContext.PCD_INITIAL_CONTEXT_FACTORY);
     env.put(com.sap.portal.directory.Constants.REQUESTED_ASPECT, PcmConstants.ASPECT_SEMANTICS);
     ctx = new InitialContext(env);

     dirCtx = (DirContext) ctx.lookup("pcd:portal_content/");
     PcdSearchControls pcdSearchControls = new PcdSearchControls();
     pcdSearchControls.setReturningObjFlag(false);
     pcdSearchControls.setSearchScope(pcdSearchControls.SUBTREE_WITH_UNIT_ROOTS_SCOPE);

     dirCtx.addToEnvironment(com.sap.portal.directory.Constants.APPLY_ASPECT_TO_CONTEXTS, com.sap.portal.directory.Constants.APPLY_ASPECT_TO_CONTEXTS);
    /*
                       
     NamingEnumeration ne_role = dirCtx.search("", "(com.sap.portal.pcd.gl.ObjectClass=com.sapportals.portal.role)",  pcdSearchControls); // for Role

     NamingEnumeration ne_page = dirCtx.search("", "(com.sap.portal.pcd.gl.ObjectClass=com.sapportals.portal.page)", pcdSearchControls); // for Page

         NamingEnumeration ne_system = dirCtx.search("", "(com.sap.portal.pcd.gl.ObjectClass=com.sapportals.portal.system)", pcdSearchControls); // forSystem
     
    */
     NamingEnumeration ne_iView = dirCtx.search("", "(com.sap.portal.pcd.gl.ObjectClass=com.sapportals.portal.iview)", pcdSearchControls);
     response.write("<table border='1'>\n");
     response.write("<tr><th>Location</th>\n");
     response.write("<th>Code Link</th></tr>\n");
     response.write("</br>");
     while (ne_role.hasMoreElements())
     {
     IPcdSearchResult searchResult = (IPcdSearchResult) ne_iView.nextElement();
     String location = "pcd:portal_content/" + searchResult.getName();
     ctx = new InitialContext(env);
     IiView result =  (IiView)ctx.lookup(location);
     String codelink =  result.getAttribute("CodeLink");
     response.write("<tr><td>"+location+ "</td>\n");
     response.write("<td>"+codelink+"</td>\n");
     }
    }

    catch(Exception e)
    {
    response.write("Exception Caught:"+e);
    }

  3. In portalapp.xml file under Application tab add following sharing references.

    com.sap.portal.pcm.admin.apiservice
    com.sap.portal.ivs.api_iview
    com.sap.portal.pcm.admin.apiservice

     
  4. Save and deploy on your portal. Run the application you will get the desired list in output.




No comments:

Post a Comment