user3809322
user3809322

Reputation: 11

Create sheet from Drupal module

I'm working on a Drupal module to create a Smartsheet when a Drupal node, of a specific type, is published.

The sheet is being created fine but is not using the template I'm specifying with the formId variable.

What am I doing wrong?

Thanks

Code is:

<?php

function smartsheet_node_insert($node) {
  if ($node->type == 'oa_space') {

    // Initialize URL Variables
    $baseURL = "https://api.smartsheet.com/1.1";
    $sheetsURL = $baseURL ."/sheets/";
    $columnURL = $baseURL. "/sheet/" . $node->nid . "/columns";

    // Insert your Smartsheet API Token here
    $accessToken = "[my api token]";

    // Create Headers Array for Curl
    $headers = array(
        "Authorization: Bearer ". $accessToken,
        "Content-Type: application/json"
    );


    // Create new sheet
    $theSheet = array(
      'name' => $node->title,
      'fromId' => '[template id]',
    );

    $postfields = json_encode($theSheet);

    // Connect to Smartsheet API
    $curlSession = curl_init($sheetsURL);
    curl_setopt($curlSession, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlSession, CURLOPT_POST, 1);
    curl_setopt($curlSession, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, TRUE);

    $createResponse = curl_exec($curlSession);

    // Assign response to PHP object
    $createObj = json_decode($createResponse);

    if (curl_getinfo($curlSession, CURLINFO_HTTP_CODE) != 200) {

    //if (curl_errno($curlSession)) {
        drupal_set_message("Oh No! Could not create sheet " . $node->title . "because " . $createObj->message);
    } else {
        // Tell the user!
        drupal_set_message("A Smartsheet has been created for the " . $node->title . " project");

        // close curlSession
        curl_close($curlSession);
    }
  }
}

Upvotes: 1

Views: 275

Answers (2)

user3809322
user3809322

Reputation: 11

@stmcallister. Exactly. The sheet structure is copied but not the content.

Template ID = 2774566423553924, newest sheet ID = 8533387423049604.

Thanks,

Glenn

Upvotes: 0

stmcallister
stmcallister

Reputation: 1719

Your code looks good. If the sheet is being created, then the structure of your call is good. It's likely that you just have an incorrect templateId. Where did you get the template Id?

Through the Smartsheet interface, you can find the template Id in the Sheet Properties. This article on the Smartsheet Help section describes where to find the Sheet Properties. In the properties window you'll take the value for Sheet ID, and use it for the fromId value in your Create Sheet call.

Upvotes: 2

Related Questions