How to show or hide a portal or portlet page in WebSphere Portal

One of the common requirements in WebSphere Portal Server is how do i show/hide particular portlet or portal page. You might want to show particular page say every Monday or only to those users for whom value of particular email attribute say city is San Francisco. Or you might want to implement some custom logic ex. you might have a table in database which has information such as role and you might want to say if role is say Manager in database then only show a portlet or page.

WebSphere Portal has concept of Visibility rule, so you can create a visibility rule and assign it to particular portlet on a page and when portal is displaying that page to user it will execute the visibility rule to decide if a portlet should be displayed to the user or not. While creating the visibility rule you can use one of the built in criteria for portal or you can create your own Java class that will get executed as a part of visibility rule and your Java class will decide if the particular portlet will be displayed to the user or not. I wanted to try this feature so i followed these steps


  1. First create a SampleApplicationObject java project and then create a SampleApplicationObject.java first which looks like this in it.


    package com.webspherenotes.pzn;

    import com.ibm.websphere.personalization.RequestContext;
    import com.ibm.websphere.personalization.applicationObjects.SelfInitializingApplicationObject;


    public class SampleApplicationObject implements SelfInitializingApplicationObject{

    private RequestContext requestContext;

    @Override
    public void init(RequestContext requestContext) {
    this.requestContext = requestContext;
    requestContext.setSessionAttribute("sampleobject", this);
    }

    public String getUserName() {
    System.out.println("Value returned from User Name " + requestContext.getRemoteUser());
    return requestContext.getRemoteUser();
    }

    }


    The SampleApplicationObject is extended from the SelfInitializationApplicationObject and it has init() method which gets called to give this object a chance to initialize itself. In the init() method it will get access to object of RequestContext class which has some methods that you can use to get access to the environment.

    I also have a getUserName() method in this class which returns name of the user for whom this rule is getting executed. Similarly you can add more methods to this class that follow JavaBean specification to return more values.


  2. Add pznresources.jar and pznruntime.jar which are located in WebSphere\PortalServer\pzn\prereq.pzn\lib as part of your project build path


  3. Export this java class and content of the project as .jar file and copy the exported jar in \WebSphere\PortalServer\pzn\prereq.pzn\collections directory


  4. Restart the Portal Server


  5. Once the portal server is started login and go to the portal page which has Personalization Navigator and Personalization Editor portlet on it. In the Personalization Navigator portlet click on New -< Application Object and it will open a New Application Object screen like this.


    Enter fully qualified class name as well as session key name that you used for storing the object in session here.

  6. Once the Sample Application Object is created go the Editor screen of the page and you should see the userName property that your java class is exposing like this


  7. Once the Sample Application Object is ready you can use it for creating sample visibility rule. For that go to the Personalization Navigator Portlet click on New -< Rule. It will open a Rules Editor page like this. In that change type of the rule to Visibility rule


  8. The Rules editor is active editor in that if you click on the Attributes link it will open all the different attributes that are available for creating rules. We can also see our custom Select Application Object, If you select that it will show userName property so select that and set "wasadmin" as value. I am setting wasadmin as user because thats the userName that i want to use for testing, you can set some other value here




With this your visibility rule is in place now the next step would be to assign it to either page or portlet depending on what you want to show or hide.

6 comments:

Anonymous said...

this.context does not work

Neeraj Sidhaye said...

Good post.

I have a requirement where I want to show/hide certain links within portlet content based on user group.

Please suggest, what would be best approach to implement this?
It like, field level access control.

Unknown said...

Hi,
I want my custom application application object to return and arrays of values (just like the portalUser.Groups). How can i create a visibility rule to handle the array objects?
should the custom application object return and arraylist or string array?

Travel with Khalid Habib said...

Hi
Can u guide me how to create a portlet in which show list of event by clicking calendar date

Regards

Unknown said...

Hi,

We can control show/hide via permission model also in Portal, then why do we need to set rules.

Just wanted to understand benefits of using while using rules.

Abhi said...

Thanks for info
Web Design Company in Bangalore
Website development in Bangalore