[SalesForce] Querying Communities

How (programatically) can I get a list of the Communities in an org to present to a System Administrator?

I've setup a community in my development org and published, and I can switch between the Internal org and the Support community using the dropdown int the top right. So far, so good.

However even though I can access and edit that community, I can't seem to find a way to get to it via code.

Running the query select id, name from community simply returns one community, the default Internal one.

Am I missing something obvious or is this just a non-starter?

Best Answer

ConnectApi.Communities.getCommunities() This works from developer console as well as from apex class, but you must use API 28.0 (or above).

11:56:52:193 USER_DEBUG [5]|DEBUG|ConnectApi.CommunityPage[buildVersion=28.0, communities=(ConnectApi.Community[buildVersion=28.0, description=funn2comunity, id=0DB90000000PBSLGA4, invitationsEnabled=false, name=Fun2community, sendWelcomeEmail=true, status=Live, url=/services/data/v28.0/connect/communities/0DB90000000PBSLGA4, urlPathPrefix=null], ConnectApi.Community[buildVersion=28.0, description=This is a funcommunity, id=0DB90000000PBQtGAO, invitationsEnabled=false, name=Funcommunity, sendWelcomeEmail=true, status=Live, url=/services/data/v28.0/connect/communities/0DB90000000PBQtGAO, urlPathPrefix=mainportal]), total=2]

Communities is called Network in the SObject API. So if you didn't want to use the ConnectApi, you could query Communities by running:

SELECT id, name FROM Network