1

we are using community and site.com in our project. we want to embed VF pages(for allowing Video chatting between Guest user and console users is one of the examples) in our static site.com pages. so our approach was to create a rest resource class which upon 'GET' request returns the corresponding html code of the VF page using PageReference.getContent() and then from site.com, we make ajax calls to this rest resource and then embed the returned content to one of the div's in our html page.

This is working fine for the guest user but not for the community logged in users.

So, this is what we did to test the basic thing, we created a simple testVFPage:-

<apex:page>
   {!$user.username} 
</apex:page>

And then an apex rest resource

@RestResource(urlMapping='/getHTMLContent')
global class GetHTMLContentController{
    @HttpGet
    global static String doGetHTMLContent() {
             PageReference vfPage=new PageReference('https://<mysiteurl>/testVFpage');
             Blob htmlContent=vfPage.getContent();
             System.debug('--UserInfo--'+UserInfo.getusername());
             return htmlContent.toString();
    }
}

when guest user tries to access this page, as expected, it prints the "Guest Username" in debug log and returns HTML Code with "Guest" username but when a community user logged in user access this page, it prints "Community LoggedIn username" in debug log but again returns HTML Code with "Guest" Username which is the main issue.

How can i get the content of the VF Page in the context of community logged in user?

NOTE: if a community logged in user tries to access the page, we pass the session id(generated from {!GETSESSIONID()}, when user logged in) in Authorisation header for these rest calls which makes it running in the context of the logged in user.

Any Suggestions on this or the alternative way to embed VF page(except iframe which creates UI issues.)?

Thanks, Shubham

0

You are trying to access a public site page using the sites url 'https://<mysiteurl>/testVFpage so independent of from where it is getting invoked it will be always using the context of site guest user.

So the different in output when running from normal org salesforce url and site url.

Page access from salesforce url (Authentication required) Printing SF username. enter image description here

Page access from salesforce site url (Authentication not required) Printing site guest user name.

enter image description here

So if you want to run the page from the context of RestResource then try using your normal salesforce org domain url and not the site url. Make sure that you give page access to the community user from which page is getting accesses otherwise getContent call would fail.

  • i had already tried this:- vfPage=new PageReference('https://<serverurl>.salesforce.com/apex/testVFpage'); but this was redirecting users to the SFDC login page which made me think that this way lacks of Session Id and then i started looking for ways for passing Session ID to access VF page. I tried with both types of Session IDs:- {!GETSESSIONID()}: VF Page UserInfo.getSessionId(): Inside the apex rest class. I tried multiple ways to initiate PageReference by Page.PageName,partial URL, passing sid in 'Authorization' headers and as a parameter of PageReference but nothing worked. – shubham jain Sep 27 '16 at 5:28
  • And Community Users have already access to the VF page – shubham jain Sep 27 '16 at 5:38
  • Shubham, I guess here we are out of luck. I have tried several times to open the vf page in other users context or get data by initiating a context by setting session id but did not get any success. If I can think of any alternative solution I would certainly post it here. – Kiran Machhewar Sep 27 '16 at 5:45
  • Thanks Kiran, really appreciate your efforts as I am new to this forum and wasn't expecting help this soon. It took me some time to realize that PageReference is actually an http request and getContent() returns the response in html hence i tried making a jquery ajax call to "https://<siteurl>/apex/testV‌​Fpage" which worked for me as request is sent from the browser instead of any server rest resource which makes it runing in the logged-in user context without any additional session id or header and i can get the corresponding HTML code which i can easily embed in my HTML. – shubham jain Sep 27 '16 at 15:52
  • @shubham I am happy that I was of any help to you and also thank you for coming back to the forum with the solution you found. Please update that as a solution which could help others facing the same problem. – Kiran Machhewar Sep 27 '16 at 16:34
0

I am posting this solution if anybody is looking for the same approach to embed a VF page into site.com html pages(and don't want to go for iframes). So if your problem looks similar to mine then you would be having a VF page which fulfills your business requirement perfectly now you want to embed this in your site.com pages:-

  1. Give access to both of your profiles(Guest User profile & Authenticated User Profile) for this VF page.
  2. To get the content of the VF page, make an ajax call.

    $.get('https:///apex/', {if any parameter is there,pass them here as param:value},function(data){ //logic to embed in div $("#dynamicContent").html(data); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.