I need to place my text file as a static resource in salesforce org. Then I need to read that text file and display the contents in a VF page.

In this way whenever we want to update the text contents in a VF page, we can easily change the file contents without touching the code.

3 Answers 3


The StaticResource object is readable via a normal SOQL query.

Add a property to your controller that queries the static resource and returns its body assuming the body is text:

public String textFromSomeUniqueName {
    get {
        StaticResource sr = [
                select Body
                from StaticResource
                where Name = 'SomeUniqueName'
        return sr.Body.toString();

The page can then output the text using:



If you want to output HTML content use:

<apex:outputText value="{!textFromSomeUniqueName}" escape="false"/>
  • one word ....AWESOME !!!!...it works smoothly....Thanks Keith C
    – CodedoC
    Commented Jun 24, 2014 at 11:00
  • Is it possible to maintain the line breaks in static resource ?
    – Samir
    Commented May 21, 2018 at 11:05
  • @samir Line breaks can be included in static SOQL in Apex as shown above. If you mean in the static resource content then the answer is yes there too.
    – Keith C
    Commented May 21, 2018 at 11:18
  • I imported a body but its shown only in single line on vf page. I had to use iframe to show the content as it is in static resource
    – Samir
    Commented May 21, 2018 at 11:21
  • @Samir HTML does not respect normal text line breaks. You may have to replace those on the fly or when the content is written with e.g. <br/>.
    – Keith C
    Commented May 21, 2018 at 12:22

Have a look at this question and answers (Editing a Static Resource via Apex with Metadata API)

You'll need to use the metadata api to achieve this.

  • I think the requirement is to only read from Visualforce; the updating (probably) is to be be done through e.g. Setup.
    – Keith C
    Commented Jun 24, 2014 at 10:29
  • I have checked there....but its for creating updating and deleting usign metadata API...!! I want to read text file saved as a static resource.
    – CodedoC
    Commented Jun 24, 2014 at 10:51

If you just want to read the static resource then you can use simple SOQL

List<StaticResource> sr = [Select body, name from StaticResource where Name = 'xx']; 
  • public class Myclass { public list<StaticResource> getGetText() { return [Select body, name from StaticResource where Name = 'pranTXT']; } } ----------------------------------------------------------- <apex:page controller="Myclass"> <apex:outputText value="{0}"> <apex:param value="{!getGetText}"/> </apex:outputText> </apex:page> ------------------------------------------ Tried this ...but its throwing error...
    – CodedoC
    Commented Jun 24, 2014 at 10:48
  • Error: Myclass Compile Error: The property String getText is referenced by Visualforce Page (TestPage) in salesforce.com. Remove the usage and try again. at line 8 column 19
    – CodedoC
    Commented Jun 24, 2014 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .