Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import external files into SDL Tridion 2011 using core service?

Tags:

tridion

I want to push PDF, Word and Excel files into SDL Tridion 2011 by using core service.

I tried below code but get this error:

Invalid value for property 'BinaryContent'. Unable to open uploaded file:

using (ChannelFactory<ISessionAwareCoreService> factory =
    new ChannelFactory<ISessionAwareCoreService>("wsHttp_2011"))
{
  ISessionAwareCoreService client = factory.CreateChannel();
  ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(
                                       ItemType.Component, "tcm:19-483-2");
  multimediaComponent.Title = "MultimediaFile";

  multimediaComponent.ComponentType = ComponentType.Multimedia;
  multimediaComponent.Schema.IdRef = "tcm:19-2327-8";

  using (StreamUploadClient streamClient = new StreamUploadClient())
  {
    FileStream objfilestream = new FileStream(@"\My Documents\My Poc\images.jpg",
                                              FileMode.Open, FileAccess.Read);
    string tempLocation = streamClient.UploadBinaryContent("images.jpg",
                                                           objfilestream);
  }
  BinaryContentData binaryContent = new BinaryContentData();
  binaryContent.UploadFromFile = @"C:\Documents and Settings\My Poc\images.jpg";
  binaryContent.Filename = "images.jpg";
  binaryContent.MultimediaType = new LinkToMultimediaTypeData()
  {
    IdRef ="tcm:0-2-65544"
  };
  multimediaComponent.BinaryContent = binaryContent;

  IdentifiableObjectData savedComponent = client.Save(multimediaComponent,
                                                      new ReadOptions());

  client.CheckIn(savedComponent.Id, null);
  Response.Write(savedComponent.Id);
}    
like image 978
Aquarius24 Avatar asked May 04 '12 10:05

Aquarius24


2 Answers

Have a read of Ryan's excellent article here http://blog.building-blocks.com/uploading-images-using-the-core-service-in-sdl-tridion-2011

All binary files are handled the same way - so his technique for images will be equally as valid for documents, just make sure you use a Schema with the appropriate mime types.

like image 99
Chris Summers Avatar answered Nov 11 '22 19:11

Chris Summers


The process for uploading binaries into Tridion using the Core Service is:

  1. Upload the binary data to the Tridion server using a StreamUploadClient. This returns you the path of the file on the Tridion server.
  2. Create a BinaryContentData that points to the file on the Tridion server (so with the path you got back from step 1)
  3. Create a ComponentData that refers to the the BinaryContentData from step 2
  4. Save the ComponentData

You are setting the local path for your file in step 2.

binaryContent.UploadFromFile = @"C:\Documents and Settings\My Poc\images.jpg";

But Tridion will never be able to find that file there. You instead should set the path that you got back from UploadBinaryContent:

string tempLocation;
using (StreamUploadClient streamClient = new StreamUploadClient())
{
  FileStream objfilestream = new FileStream(@"\My Documents\My Poc\images.jpg",
                                            FileMode.Open, FileAccess.Read);
  tempLocation = streamClient.UploadBinaryContent("images.jpg", objfilestream);
}
BinaryContentData binaryContent = new BinaryContentData();
binaryContent.UploadFromFile = tempLocation;

Note that the Ryan's original code does exactly that.

like image 39
Frank van Puffelen Avatar answered Nov 11 '22 20:11

Frank van Puffelen