[SalesForce] How to get current communityId on visualforce page

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();

Best Answer

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 {

    /* ... */
}
Related Topic