Downloading SharePoint Attachments with PHP, a snippet

To use this code, please download the latest version of Camelot PHP Tools and the Camelot SharePoint Integration Toolkit

We have gotten a few questions how to download attachments from SharePoint List Items using Camelot PHP Tools. Originally we planned to implement a separate library doing this but recently came to the conclusion that it wasn’t really necessary. Instead we will rely on Camelot XML and the original SQL syntax.

$SharePointQuery = new SharePointQuery(array(
    'sql' => "SELECT FileName, Data FROM Testlist.Attachments WHERE ItemID = 1",
    'connection_name' => 'SharePointConnection1'
));

Snippet

The snippet below illustrates how to download the first attachment available with ItemId 1

/*
 * Camelot PHP Tools
 *
 * @version 1.5
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 */
error_reporting(E_COMPILE_ERROR);
ini_set('display_errors', '1');


// Load all required files for Camelot PHP Tools
require_once '../loader.php';

try {
    $SharePointQuery = new SharePointQuery(array(
                'sql' => "SELECT FileName, Data FROM Testlist.Attachments WHERE ItemID = 1",
                'connection_name' => 'SharePointConnection1'
            ));

    // Store the file info, change the index [0] if you have multiple attachments
    $file = $SharePointQuery->CamelotSoap->_sorted->_content[0];
    
    // Send the file to the browser
    header("Content-type: application/data");
    header('Content-Disposition: attachment; filename="' . $file["FileName"] . '"');
    header("Content-Description: PHP Generated Data");
    
    echo base64_decode($file["Data"]);
    
    $file = null;
    
} catch (Exception $exc) {
    echo $exc->getTraceAsString();
}

It doesn’t really get harder than this, enjoy.
 

 

This entry was posted in PHP. Bookmark the permalink.

One Response to Downloading SharePoint Attachments with PHP, a snippet

  1. samjhana joshi says:

    Can you tell me how can i connect Php app and sharepoint(installed in server)?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>