Google Sites‎ > ‎Sample Scripts‎ > ‎

Listing members of a Google Site


Google's sharing and permissions interface is described in detail by Steegle here:

It's not immediately obvious, but you can see the email address of any of the users listed in the share window by mousing over their user name.

(If you wish, you can get back to the old-style sharing interface which can be advantageous in some situations: just add /system/app/pages/admin/sharing to the end of your site's URL

Listed below is a script that creates a directory listing of the members of a site - example page created here > Site Directory Page

Instructions for use

As a Site Script

  1. Create a Webpage in a site for the directory listing (does not have to be the site for which the listing is being generated)
  2. Open the script editor in a site (again, does not have to be the site for which the listing is being generated)
  3. Copy and paste the script into the editor, edit the parameters as required - put the URL of the page created in step 1 into DIRECTORYURL - and save with the name of your choice
  4. Open a page in the site in which the script has been saved and create a link to the script function buildDirectory
  5. Click on the link to generate the directory listing - NB The contents of the directory page are completely replaced
E.g. This button is a link to the script in this site (which will not work unless you have collaborator status - which you dont'!)

As a Spreadsheet Script

  1. Create a Webpage in a site for the directory listing (does not have to be the site for which the listing is being generated)
  2. In a Google Spreadsheet, open the script editor
  3. Copy and paste the script into the editor, edit the parameters as required - put the URL of the page created in step 1 into DIRECTORYURL - and save with the name of your choice
  4. Run the script function buildDirectory to generate the directory listing - NB The contents of the directory page are completely replaced
    (Script contains a onOpen function to build a menu with a single option to run the buildDirectory script

// Build Site member directory listing
// Author : Phil Ridout
// Version: 1.0, 6th November 2010

function buildDirectory()
{
  /////////////////////////////////////////////////////////////////////////////////
  //  PARAMETERS TO BE SET AS APPROPRIATE
  /////////////////////////////////////////////////////////////////////////////////
  
  // SITEURL - the URL of the site to build a member directory listing of
  var SITEURL           'https://sites.google.com/a/pjrprojects.co.uk/sitesguide/';
  
  // DIRECTORYURL - the URL of the site page to put the directory listing in
  var DIRECTORYURL      'https://sites.google.com/a/pjrprojects.co.uk/sitesguide/google-sites/sample-scripts/build-a-directory-page/site-directory-page';
  
  // Flags to control what to display in the directory listing
  var SHOWOWNERS        false;
  var SHOWCOLLABORATORS true; // = can edit
  var SHOWVIEWERS       true;
  
  /////////////////////////////////////////////////////////////////////////////////   
  // END OF PARAMETERS
  /////////////////////////////////////////////////////////////////////////////////
  
  var site SitesApp.getSiteByUrl(SITEURL);
  if (!site)
  {
    Browser.msgBox("Site not found")// This line only works if running from spreadsheet
    return;
  }
  
  var directory SitesApp.getPageByUrl(DIRECTORYURL);
  if (!directory)
  {
    Browser.msgBox("Directory page not found")// This line only works if running from spreadsheet
    return;
  }
      
  var owners        getSiteMembers_(SITEURL,'owners');
  var collaborators getSiteMembers_(SITEURL,'collaborators');
  var viewers       getSiteMembers_(SITEURL,'viewers');
  
  if (collaborators.length == 0)
  {collaborators "None"};
   
  if (viewers.length == 0)
  {viewers "None"};
  
  var header1 '<b>Site Owners</b><br>';
  var header2 '<br><br><b>Site Collaborators</b><br>';
  var header3 '<br><br><b>Site Viewers</b><br>';
 
  // set the page content according to the flags set
    
  var pageContent '';
  
  if (SHOWOWNERS)
  {
    pageContent pageContent header1 owners.toString().replace(/,/g,', ');
  }
  if (SHOWCOLLABORATORS)
  {
    pageContent pageContent header2 collaborators.toString().replace(/,/g,', ');
  }
  if (SHOWVIEWERS)
  {
    pageContent pageContent header3 viewers.toString().replace(/,/g,', ');
  }
  
  // write the directory page
  var newpage directory.setHtmlContent(pageContent);
}
    
///////////////////////////////////////////
// GETSITEMEMBERS function
// returns array of email addresses
/////////////////////////////////////////// 
function getSiteMembers_(siteUrl,memberType)
{
  var mySite=SitesApp.getSiteByUrl(siteUrl);
  switch (memberType)
      {
    case 'owners':
      var siteMembers=mySite.getOwners()
      break;
    case 'collaborators':
      var siteMembers=mySite.getCollaborators()
      break;
    case 'viewers':
      var siteMembers=mySite.getViewers()
      break;
      }
  var memberEmails=new Array();
  for(var i=0;i<siteMembers.length;i++)
  {
    memberEmails[i]=siteMembers[i].getEmail();
  }
  return memberEmails;
}
  
// onOpen function for use when installed in a spreadsheet
function onOpen({
  var mySheet SpreadsheetApp.getActiveSpreadsheet();  
  var menuEntries {name"Build Directory"functionName"buildDirectory"},
                    ];
  mySheet.addMenu("More..."menuEntries);
}


Subpages (1): Site Directory Page
Comments