+ Start a Discussion

How to get the site url from Apex Code

Hi Guys,

Is thera any way I can got the site url from Apex Code?

I tried Site.getPrefix() , Site.getDomain() and getCurrentSiteUrl. but all of them return null.

Do I need setup some information after the site is ready.

Thanks in advance.


Please give a try using URL methods. More details for all URL methods can be found here-> http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_url.htm 




Tejpal KumawatTejpal Kumawat

Hi try to this it will be definately works...


public String getSiteUrl(){
return Url.getSalesforceBaseUrl().toExternalForm();


Thanks guys,


from Url.getSalesforceBaseUrl().toExternalForm(), the result like https://na12-api.salesforce.com. which is salesforce internal domain.


Is it possible to get the salesforce.com site domain name something like http://XXX.force.com ?




What I am doing is to make the attachments public via force.com site,  the url pattern for attachment is {!$Site.Prefix}/servlet/servlet.FileDownload?file={!List.Image},


Unfortunately, I have to expose those the attachment image via web service. so I have to get the site domain from Apex Code.


Or Is there any way to expose images directly?




Hey tony, if you use Site.getCurrentSiteUrl it will be provide the url of site...If it is not provide then change the public settings in the site settings... then also check


Thanks Dhurav,


I still got null using  Site.getCurrentSiteUrl.


I couldn't find any settings for this url from public settings.


Can you point our what inforamtion I need to setup in public settings ?


Thanks a lot