0

Using my current code I need to make a separate curl request for on each row of the smartsheet to get the attachment id, then send another request with that id to get the download url. This will occur for each row of the smartsheet and is very inefficient for sheets with lots of rows. Is there a way to get all the attachment id's / url's from a sheet with one request?

class Sheet_request{
    private $urlMain = "https://api.smartsheet.com/2.0/users/me";
    private $url_food_sheet = "https://api.smartsheet.com/2.0/sheets/3650210866XXXX/?include=attachments";
    private $url_food_main= "https://api.smartsheet.com/2.0/sheets/36502108669XXX";


    private function curl($url) {
        $ch = curl_init($url);
        $request_headers = array();
        $request_headers[] = "Authorization: Bearer XXXXXXXXXXXXXXXX";
        $request_headers[] = "Content-Type: application/json";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        if ($response === false) {
            die(curl_error($ch));
        }
        curl_close($ch);
        return $response;
    }

    function get_attachments() {
        $response = $this -> curl($this -> url_food_sheet);
        $sheetObj = json_decode($response);
        foreach ($sheetObj->rows as $row) {
            if (isset($row->attachments)){
                $rowAttachmentsURL = $this -> url_food_main . "/rows/" . number_format($row -> id, 0, "", "") . "/attachments";
                $getAttachmentsResponse = $this->curl($rowAttachmentsURL);
                $attachment = json_decode($getAttachmentsResponse);
                $attachmentURL = $this->url_food_main . "/attachments/".number_format($attachment->data[0]->id, 0, "", "");
                $attachmentInfo = $this->curl($attachmentURL);
                $attachmentInfo = json_decode($attachmentInfo);
                file_put_contents("pictures/".$attachmentInfo->name, file_get_contents($attachmentInfo->url));
            }
        }

    }



}




$foo = new Sheet_request();

$foo->get_attachments();
Brett
  • 2,502
  • 19
  • 30
StoneLight
  • 27
  • 2
  • 8

2 Answers2

1

You can use the Get All Attachments operation to retrieve all attachments (Ids) in a single request for the specified sheet. As the documentation shows, the Request URI is: https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments.

Please note that Attachments can exist in Smartsheet at 3 different levels: Sheet, Row, Comment. The Get All Attachments response will contain an all attachments in the sheet (at any/all levels: Sheet, Row, Comment) -- so if you're just interested in Row attachments, you'll want to only process items in the response where the parentType attribute has a value of "ROW".

Once you have access to the attachment Ids in the "Get All Attachments" response, you'll still need to subsequently use the Get Attachment operation for each attachment to retrieve the URLs one at a time. (There's currently no way to get these in bulk.)

Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
0

At the sheet or row level, I believe you can now add the 'include' query parameter to force inclusion of attachments at .sheet. and .row. level, to ensure .comment. level attachments, need to add discussions to the include values:

https://api.smartsheet.com/2.0/sheets/{sheetId}?include=attachments,discussions
fedesc
  • 2,554
  • 2
  • 23
  • 39
jlhfinpro
  • 1
  • 1