Activity Forums Salesforce® Discussions Can we get the site.com site url dynamically in class or vf page in Salesforce?

  • Gourav

    Member
    June 8, 2016 at 10:44 am

    Try this code hope this will help you also:-

    String name = 'mysite';

    Site site = [
    SELECT GuestUserId
    FROM Site
    WHERE Name = :name
    ];

    String path = '/sites/servlet.SiteDebugMode';
    PageReference pr = new PageReference(path);
    pr.getParameters().put('guid', site.GuestUserId);
    pr.getParameters().put('sitedebugmode', 'x');
    String url = pr.getContent().toString().substringAfter('URL=').substringBefore(path);

    System.debug(url); //eg "http://dev-bigass.cs81.force.com/mysite"

  • Parul

    Member
    September 12, 2018 at 4:30 pm

    Hello,

    Try this once.. This has mine problem:

    var prvsURL = document.referrer;

    var getLoc = getParameterByName('loc',prvsURL);

    function getParameterByName(name, url) {

    name = name.replace(/[\[\]]/g, "\\$&");

    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),

    results = regex.exec(url);

    if (!results) return null;

    if (!results[2]) return '';

    return decodeURIComponent(results[2].replace(/\+/g, " "));

    }

    Hope this helps.

  • shariq

    Member
    September 14, 2018 at 5:08 am

    Hi,

    Try this -

    String sitename = 'testSiteName';

    Site site = [SELECT GuestUserId FROM Site WHERE Name = :sitename ];

    String sitepath = '/sites/servlet.SiteDebugMode';
    PageReference prRef = new PageReference(sitepath );
    prRef .getParameters().put('guid', site.GuestUserId);
    prRef .getParameters().put('sitedebugmode', 'x');
    String siteurl = prRef .getContent().toString().substringAfter('URL=').substringBefore(sitepath );

    System.debug(siteurl ); //eg "http://dev-bigass.cs81.force.com/testSiteName"

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos