Answers.Body: <p>Do you have created the custom field on event?</p>
  
  <p>I would suggest you to add manually (via the UI) an attachement and check how it looks like (using the api explorer and get the file(s)), it will be easier to understand what civi expects by looking at what it does</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-11T10:11:43.493Z
Answers.Text: Thanks for your suggestion.
  I did it and it showed me that Attachment API is not compliant with civievent. Attachment needs a reference to the object to whose attach the file and event table is not understood as a compliant table. It is restricted to Activity and Mailing only. This seems to explain the permission error.
  Actually, I have to use the File API to create an image attached to an event.
Answers.UserDisplayName: philippe
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-09T15:13:28.260Z
Answers.LastActivityDate: 2016-02-09T15:13:28.260Z
Answers.UserDisplayName: Xavier
Body: <p>I am new to CiviCRM and did not find any answer to this.</p>
  
  <p>I have a form on a Drupal page, and want to give ability to upload an image file in both Drupal and CiviCRM from within this form.</p>
  
  <p>I use the "Attachment" api function. When saving the Drupal node, I call civicrm_api3('Attachment', 'create', params) and pass file params as well as entity_id and custom field_name as explained in the api explorer. The CiviCRM field is a file custom field and the entity is an event.</p>
  
  <p>Here is the code I use : </p>
  
  <p><code>$result = civicrm_api3('Attachment', 'create', array(
      'debug' => 1,
      'sequential' => 1,
      'name' => $file->filename,
      'mime_type' => $file->filemime,
      'entity_id' => $event_id,
      'field_name' => $field_name,//custom_14
      'url' => file_create_url($file->uri),
      'path' => drupal_realpath($file->uri),
  ));
  </code></p>
  
  <p>I finally catch this error (look at {main} at the end):</p>
  
  <p><code>
  20 {main}
              [is_error] => 1
              [error_message] => Failed to run permission check: Unrecognized target entity table (civicrm_event)
          )
  </code></p>
  
  <p>Guess I missed something, but what ? Any suggestion ? </p>
  
  <p>Thanks for your help</p>
  
  <p>Philippe</p>
  
Tags: <civievent><api><attachments>
Title: Unable to add file to event with Attachment api
ThreadText: # Unable to add file to event with Attachment api
  
  (Asked by: username_0 on 2016-02-09)
  
  I am new to CiviCRM and did not find any answer to this.
  
  I have a form on a Drupal page, and want to give ability to upload an image file in both Drupal and CiviCRM from within this form.
  
  I use the "Attachment" api function. When saving the Drupal node, I call civicrm\_api3('Attachment', 'create', params) and pass file params as well as entity\_id and custom field\_name as explained in the api explorer. The CiviCRM field is a file custom field and the entity is an event.
  
  Here is the code I use :
  
  `$result = civicrm_api3('Attachment', 'create', array(
  'debug' => 1,
  'sequential' => 1,
  'name' => $file->filename,
  'mime_type' => $file->filemime,
  'entity_id' => $event_id,
  'field_name' => $field_name,//custom_14
  'url' => file_create_url($file->uri),
  'path' => drupal_realpath($file->uri),
  ));`
  
  I finally catch this error (look at {main} at the end):
  
  `20 {main}
  [is_error] => 1
  [error_message] => Failed to run permission check: Unrecognized target entity table (civicrm_event)
  )`
  
  Guess I missed something, but what ? Any suggestion ?
  
  Thanks for your help
  
  username_0
  
  **Tags:** civievent, api, attachments
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-02-09. Score: 1
  
  Do you have created the custom field on event?
  
  I would suggest you to add manually (via the UI) an attachement and check how it looks like (using the api explorer and get the file(s)), it will be easier to understand what civi expects by looking at what it does
  
  > **Comments:**
  > **username_0**: Thanks for your suggestion. I did it and it showed me that Attachment API is not compliant with civievent. Attachment needs a reference to the object to whose attach the file and event table is not understood as a compliant table. It is restricted to Activity and Mailing only. This seems to explain the permission error. Actually, I have to use the File API to create an image attached to an event.  
  
  ---
  
  