4

How to get current communityId? So if there are two communties like community-domain/sales and community-domain/support so based on particular community context we should get that community Id. Is there global variable?

I found only solution to get communityId value in controller

String communityId=Network.getNetworkId();
  • There are a bunch of $Site global methods but none return a community Id AFAIK. Do all of your pages have custom controllers or extensions because you could use apex to surface the id to the page pretty easily. – greenstork Jun 16 '14 at 19:10
  • No custom controllers; most of the pages are based on js remoting and remote objects...I think to introduce GeneralController to calculate communityId – Natallia Jun 16 '14 at 19:22
  • 1
    Just to clarify, any page that uses JS Remoting would have a custom controller, although remote objects do not require a controller. You could certainly have a very basic controller with the fetching of the community id in the constructor to surface it to the page. – greenstork Jun 16 '14 at 19:25
2

Natalia, your question actually answered my question, which was how to get the Community ID to my Apex controller for a Visualforce component. I think you have answered your own question, which is to create a base controller that all of your site controllers will extend, so that every page can have access to the Community ID.

The base controller would look something like the following:

/*
 * Base class for all custom Visualforce controllers, containing common
 * utility methods such as getting and setting page parameters
 */
public virtual with sharing class CustomController {

    /*
     * @return the Community ID for the community context in which the
     *         controller is operating, based on the location at which
     *         a particular Visualforce page is requested
     */
    public Id getNetworkId() {
        return Network.getNetworkId();
    }
}

Then, just make sure all of your controllers for the site in question extend this base controller.

/*
 * Controller for the SiteProfile page
 */
public with sharing class SiteProfileController extends CustomController {

    /* ... */
}

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.