Dec 19, 2012

Reading Visualforce Page Code In Apex

Recently faced a strange requirement to change the Visualforce page code dynamically (apologies can't disclose the complete scenario)

So would like to share the small POC with code. Code will take Visualforce Page name as parameter and will return you the code written for that VFP as text. For example, this is my Visualforce Page saved with name "VisualforceCode"


<apex:page id="PG" controller="VisualforceCodeController">
<apex:form id="FRM">
    <apex:pageMessages id="PM"/>
    <apex:pageBlock id="PB">
        
        <apex:commandLink value="See Magic" action="{!DisplayCode}" >
            <apex:param assignTo="{!CurrentPageName}" value="{!$CurrentPage.Name}"/>
        </apex:commandLink>
        
        <br/> <br/>

        <apex:outputPanel id="OPT" rendered="{!showCodeSection}">
            <apex:outputLabel value="{!PageText}"/>
        </apex:outputPanel>
    
    </apex:pageBlock>
</apex:form>
</apex:page>


Now I want to read the code written in this page, so here is the working code which send page name as parameter in "UtilClass" to get the code as text:


public class VisualforceCodeController
{
    //To display section which hold code as text of VF page
    public boolean showCodeSection {get; set;}
    
    //To hold VF Code as text
    public String PageText {get; set;}
    
    //Hold current page name
    public String CurrentPageName {get; set;}
    
    public VisualforceCodeController()
    {
        CurrentPageName = '' ;
        showCodeSection = false ;
    }
    
    public PageReference DisplayCode()
    {
        if(CurrentPageName != '')
        {
            //Fetching VF code
            PageText = UtilClass.VFPageCode(CurrentPageName) ;
            showCodeSection = true ;
        }
        return null ;
    }
}


To get the visualforce page name I've used "{!$CurrentPage.Name}" which is a global variable

Here Util Class code:


//General Class with static methods
public class UtilClass
{
    //Method will take visualforce page name as parameter and returns the code written in it
    public static String VFPageCode(String PageName)
    {
        ApexPage testPage = new ApexPage();
        String PageText = '' ;
        
        if(PageName != '')
        {
            //Fetching visualforce page code
            testPage = [Select Id, Markup from ApexPage where name =: PageName];
            PageText = testPage.markup ;
        }
        return PageText ;
    }
}

There are many other methods you can use with "ApexPage" mentioned here. So if you want to update the VFP code from apex dynamically you can use "ApexPage"

3 comments:

  1. This is great post. It is useful for me and I knew that your upcoming post would be that. superLike

    ReplyDelete
  2. can we create vf page dynamically ,,i mean in apex we are reading the markupof vf page,with the obtained markup,can i create replica of previous vf page
    vf page

    ReplyDelete
  3. Your code is so beneficial for developer. Just they will pick up and put in header file. Likely, you're providing a service for kids, while I'm offering Mold Inspection Services in San Diego to those who are concerned. They can count on us, and please allow me the opportunity to clean your house.

    ReplyDelete