Post

2 followers Follow
1
Avatar

File Upload via Web Service API v2.0 (.NET)

I've followed the example code for uploading a document to Clarizen, as outlined in the Web Service API Guide Version 2; however the FileInformation object appears to no longer support the Content property. See the example code, and my code below:

======= Example Code =========
//Step 3: Upload the file
UploadMessage upload = new UploadMessage();
upload.DocumentId = document.Id;
upload.FileInformation = new FileInformation();
//Sorage.Server - File is physically stored on the server.
//Other options are Sorage.Url and Sorage.Link where the file is not actually uploaded but
//a reference to the file path is stored in Clarizen
upload.FileInformation.Storage = Storage.Server;
upload.FileInformation.FileName = fileName;
//Read all file content as an array of bytes
upload.FileInformation.Content = File.ReadAllBytes(fileName);
======= End Example Code =========

======= My Code =========
// upload the file
ClarizenService.UploadMessage upload = new ClarizenService.UploadMessage();
upload.DocumentId = document.Id;
upload.FileInformation = new ClarizenService.FileInformation();
upload.FileInformation.Storage = ClarizenService.Storage.Server;
upload.FileInformation.FileName = fileName;
upload.FileInformation.Content = File.ReadAllBytes(fileName);
======= End My Code =========

I am getting a compile error stating:
'FileInformation' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'FileInformation' could be found (are you missing a using directive or an assembly reference?)"

Has the Content property been moved to a different object?
How do I actually upload the content of a file into Clarizen?

Jon Park Answered

Official comment

Avatar

Hi Jon,

To upload a file from your file system please follow the instructions below:

  1. Use "CreateMessage" to upload a Document to Clarizen 
  2. Use "CreateMesage" to link between the new document to a Work Item

Code example:

private void UploadAndLinkFiletoWorkItem(Clarizen clarizenService)
{
   var documentId = UploadFile(clarizenService);
   LinkFileToWorkItem(documentId, clarizenService);
}

private static EntityId UploadFile(Clarizen clarizenService)
{
   var content = File.ReadAllBytes(@"C:\Temp\photo.jpg");
   var createMessage = new CreateMessage
   {
      Entity = new Entity
      {
          Id = new EntityId { TypeName = "Document" },
          Values = new[]
          {
             new FieldValue {FieldName = "Content", Value = content},
             new FieldValue {FieldName = "Name", Value = "photo.jpg"}
          }
     }
  };

  var results = clarizenService.Execute(new[] { createMessage });
  var createResult = ((CreateResult)results[0]);

  return createResult.Id;
}

private static void LinkFileToWorkItem(EntityId documentId, Clarizen clarizenService)
{
   var createMessage = new CreateMessage
   {  
      Entity = new Entity
      {
         Id = new EntityId { TypeName = "AttachmentLink" },
         Values = new[]
         {
             new FieldValue {FieldName = "Document", Value = documentId},
             new FieldValue{FieldName = "Entity", Value = new EntityId{TypeName = "Project", Value = "5ra3pfvvxr45m3gyyrr0v97ke5792"}}
         }
      }
   };
   var results = clarizenService.Execute(new[] { createMessage });
}

*************************

I hope this helps,

Elad

 

Elad Franklin
Comment actions Permalink

Please sign in to leave a comment.

1 comment