How to pull Sharepoint list data into Flash using actionscript/XMLHTTP

I developed this as part of a project to retrieve and aggregate data from a sharepoint list and display in fields on a regional map of the US using flash for the UI.  This was in Sharepoint 2003 but will work in 2007 as well. 
 
The basic premise is to use XMLHTTP to obtain the data from your Sharepoint list, from Flash use the XML object in Actionscript to load the response XML and then parse through the XML using the XML DOM. 
 
 
//*****Begin configuration area **************
//*******General Configuration (Required for both portal and team sites**************
//change siteBaseURL to Sharepoint site URL
siteBaseURL = "http://[serveraddress]";
//valid values are portal or team
siteType = "team";
//List Id configuration
//update listid to appropriate list id in sharepoint
//to locate list id browse to sharepoint list and select modify columns/settings and
//copy listid from URL
 //change listId to image list GUID
listId = "{[listguid]}";
//*********Team Site Specific Configuration(Required for teamsites only************
//change site to site name in URL
site = "[sitename]";
 //*****End configuration area **************
//switch for whether this is a team or portal site to obtain the correct URL 
 if(siteType == "team")
 {
   GetURL = siteBaseURL + "sites" + "/" + site + "/_vti_bin/owssvr.dll?Cmd=Display&List=" + listId + "&XMLDATA=TRUE";
 }
 else
 {
   //GetURL = "";
 }
 if(siteType == "portal")
 {
   GetURL = siteBaseURL + "_vti_bin/owssvr.dll?Cmd=Display&List=" + listId + "&XMLDATA=TRUE";
 }
 else
 {
  }
myXML = new XML();
myXML.load(GetURL);
myXML.ignoreWhite = true;
myXML.onLoad=extractData;
function extractData(success){
rootNode = this.firstChild;
oNodeList = rootNode.childNodes;
wArray = new Array();
eArray = new Array();
cArray = new Array();
for(i = 0; i < oNodeList.length; i++)
 {
  rsnode = oNodeList[1];
 }
  
zNodeList = rsnode.childNodes;
 //sum attachment nodes
 
 for(i = 0; i < zNodeList.length; i++)
 {
  zNode =  zNodeList[i];
  
  if(zNode.attributes.ows_Region == "West")
  {
   attr = parseFloat(zNode.attributes.ows_Amount);
   num = new Number(attr);
   wArray[i] = num;
  }
  
  if(zNode.attributes.ows_Region == "East")
  {
   attr = parseFloat(zNode.attributes.ows_Amount);
   num = new Number(attr);
   eArray[i] = num;
  }
  
  if(zNode.attributes.ows_Region == "Central")
  {
   attr = zNode.attributes.ows_Amount;
   num = new Number(attr);
   cArray[i] = num;
  }
      
  }
_root.westText.text = FormatAsDollars(sumAttributes(wArray));
_root.centralText.text = FormatAsDollars(sumAttributes(cArray));
_root.eastText.text = FormatAsDollars(sumAttributes(eArray));
function sumAttributes(aArray)
{
 tNum = new Number();
 for(x = 0; x < aArray.length; x++)
 {
  xNum = new Number(aArray[x]);
  tNum = tNum + xNum;
 }
 return tNum;
   
}
 
function FormatAsDollars (amount, isCents) {
//
// Make sure we’re given a real number to work with: 
   if (isNan(amount)) {
     return NaN;
   }
//
// Convert everything to cents. A float is assumed to be
// dollars-and-cents, while an integer is assumed to be in
// dollars unless an "isCents" argument is added to the call. 
   if (isCents != "isCents") {
     amount = amount*100;
     amount = Math.round(amount);
   }
//
// Handle less-than-a-dollar single-digit and double-digit cases:  
   amount = String(Math.floor(amount));
   if (amount.length == 1) {
     return ("0.0"+amount);
   }
   if (amount.length == 2) {
     return ("0."+amount);
   }
  
//
// First grab the last two characters as cents, and remove them
// from the amount string:
   cents = amount.slice(-2);
   amount = amount.substring(0, amount.length-2);
//
// Now stuff the last three digits at a time into an array, removing
// them from the string. (We have to work backwards to figure where
// commas should go.)
   dollars = [];
   do {
     dollars.push(amount.slice(-3));
     amount = amount.substring(0, amount.length-3);
   } while (amount.length>3);
//
// If there are 1 or 2 numbers remaining, they’ll need to be at
// the front of the number, with their own comma. (We need to test
// to make sure we don’t end up with $,123.00 or such.)  
   if (amount.length) {
     dollars.push(amount);
   }
//
// Now reverse the array, so the last elements appear first:  
   dollars.reverse();
//
// Finally construct the return string. The "join()" function turns
// an array to a comma-delimited string.  
   //return ("$"+ dollars.join() + "." + cents);
   //modify to format return format output
   return (dollars.join());
}
}
//author Rod Stagg January 2005 
About these ads

One thought on “How to pull Sharepoint list data into Flash using actionscript/XMLHTTP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s