Code Samples

Get started with using the Data Export API. Explore our samples and discover the things you can build. This Sample Code is provided for the purpose of illustration only and is not intended to be used as-is in a production environment.

Using the Yammer API to Set-up Recurring Exports

On first run, the script will export all network data up from the beginning of the network up until present. From then on, it will automatically check for previous exports, and perform an export from the date of the previous export. This script can be used with common schedulers (Cron, etc) to easily set up recurring exports.

Shell

#!/bin/sh
 
if [! $1 -o ! $2 ]; then
  echo "Usage: export.sh <OAuth Access Token> <Directory>"
  exit 1
fi
 
 
# Your Yammer OAuth 2 Access Token. This must be a token for a verified admin account.
AT=$1
 
# Download location for the export files
DIR=$2
cd $DIR
 
# Find the last export if there is one. The start date and time of the previous export
# is encoded in the filename in UNIX time (http://en.wikipedia.org/wiki/unix_time)
LAST_EXPORT=`ls export-*.zip | sed -e 's/export-\(.*\)\.zip/\1/g' | sort -n | tail -l`

# Check to see if a previous export was done
if [ ! $LAST_EXPORT ]; then
  # No previous export was done. Start at the beginning of time (or 1970, which is close enough given Yammer's age).
  LAST_EXPORT=0
fi
 
# Convert UNIX time to ISO-8601 time, which the API endpoint accepts.
DATE=`date -j -r  $LAST_EXPORT "+%Y-%m-%dT%H:%M:%S%z"`
 
# Calculate the current date in UNIX time for the filename of the export.
NEXT_EXPORT=`date "+%s"`
 
# Perform the next export. Send the OAuth2 access token and store the UNIX time of this export in the filename.
curl -v --output export-$NEXT_EXPORT.zip \
  --header "Authorization: Bearer $AT" \
  --get --data-urlencode since=$DATE \
  "https://export.yammer.com/api/v1/export"
 
# Verify the download completed
if [ $? != 0]; then
  echo "Download failed...cleaning up."
  rm export-$NEXT_EXPORT.zip
  exit 1
fi
 
# Verify the contents of the zip file.
unzip -t export-$NEXT_EXPORT.zip >/dev/null 2>&1
if [$? != 0 ]; then
  echo "Invalid up file detected, export failed....removing downloaded ZIP"
  rm export-$NEXT_EXPORT.zip
  exit 1
fi

Sample Data Export Script for Windows

On the first run, the script will export all network data up from the beginning of the network up until the present. From then on, it will automatically check for previous exports, and perform an export from the date of the previous export. This script can be used with Windows schedulers to create automatic, recurring exports.

You may need to install wget for Windows if you do not already have it installed. Here is a link to the latest version:

http://sourceforge.net/projects/gnuwin32/files/wget/1.11.4-1/

Make sure to update your PATH environment variable to include the GNU bin path (e.g., C:Program Files (x86)GnuWin32bin).

Batch Script

@ECHO OFF

IF [%1]==[ ] (
  IF [%2]==[ ](
    ECHO "Usage: export.cdm <OAuth Access Token> <Directory>"
    EXIT /B
  )
)

:: Your Yammer Oauth 2 Access Token. This must be a token for a verified admin account. 
SET AT=%1

:: Download location for the export files.
SET DIR=%2
cd %DIR%

:: Find the last export of there is one. The start date and time of the previous export is encoced in the filename 
SET LAST_EXPORT=0
IF EXIST export-*.zip (
  FOR /f "delims=" %%a in ('dir /B /D export-*.zip') do(
    :: Use the timestamp from the most recent file
    SET LAST_EXPORT=%%a
  )
)

:: Remove the relevant parts of the filename so the timestamp can be stored 
SET LAST_EXPORT=%LAST_EXPORT:export-=%
SET LAST_EXPORT=%LAST_EXPORT:.zip=%
:: Don't foget the replace the underscore with the colons to ensure correct ISO-8601 formatting
SET LAST_EXPORT=%LAST_EXPORT:_=:%

:: Calculate the current date in UNIX time for the filename of the export.
SET TIME_STAMP=%time:~0,2%:%time:~3,2%:%time:~6,2%z
SET NEXT_EXPORT=%date:~10,4%-%date:~4,2%-%date:~7,2%T%TIME_STAMP%
:: Make sure to replace ":" with "_" so that Windows will save the file
SET NEXT_EXPORT=%NEXT_EXPORT::=_%
:: Make sure to replace any spaces with 0's. DOS doesn't always provide leading 0's when a time fragment is less than 10
SET NEXT_EXPORT=%NEXT_EXPORT: =0%

::Preform the next export. Send the OAuth 2 access token and store the time of this export in the filename.
SET FILE_NAME=export-%NEXT_EXPORT%.zip
SET AUTH_HEADER=Authorization: Bearer %AT%
SET API_URL=https://export.yammer.com/api/v1/export
IF NOT EXIST cacert.pem ( wget http://curl.haxx.se/ca/cacert.pem )

ECHO %LAST_EXPORT%
wget -o %FILE_NAME% -t 1 --header "%AUTH_HEADER%" -ca-certificate cacert.pem
%API_URL%?since=%LAST_EXPORT%

Data Export API Sample Code Copyright (c) Microsoft Corporation All rights reserved. MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.