Added way to download binary from original to use in the copied component.
Source Link
Alex Klock
  • 2.9k
  • 16
  • 27

The reason you are seeing this error is because you are creating a new Multimedia Component using the Create method. When creating a new multimedia component, it looks at the BinaryContent.UploadFromFile property to upload the binary from a file path. If you look at your compData.BinaryContent.UploadFromFile property, its actually not set, so you are just copying a null value over, thus your property not set error.

Since you are copying components, have you thought of using the Copy method?

var component = _client.Copy(compData.Id, compData.LocationInfo.OrganizationalItem.IdRef, true, new ReadOptions());

You can then make any changes to the component if needed and save those changes.

Update: Since you are copying across publications and can't use the Copy method as per your comment, you could probably do something like download the binary from the original component, and then set the UploadFromFile to the path of the downloaded binary. The code will look something along the lines of:

var compData = (ComponentData)client.Read(componentID, new ReadOptions());

var component = ...... // all your component creation stuff here...

if (compData.BinaryContent != null)
{
    string filePath = @"C:\TempFiles\" + Path.GetFileName(compData.BinaryContent.FileName);
    StreamDownloadClient downloadClient = new StreamDownloadClient("streamDownload_netTcp_2011");
    
    using (Stream stream = downloadClient.DownloadBinaryContent(compData.Id))
    {
        using (FileStream fs = File.Create(filePath))
        {
            using (MemoryStream ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                byte[] binary = ms.ToArray();
                fs.Write(binary, 0, binary.Length);
            }
        }
    }
    component.BinaryContent = new BinaryContentData
    {
        Filename = compData.BinaryContent.Filename,
        MultimediaType = compData.BinaryContent.MultimediaType,
        UploadFromFile = filePath
    }
}
component = (ComponentData)client.Create(component, new ReadOptions());

Note that the above assumes your code is running on the CMS Server. If you are running the code remotely, you'll have to upload the file using StreamUploadClient after downloading it, as in Bart's example. If your code is running on the CMS Server, then there's no need to upload, as your binary file will be saved on the server already during the download.

Hope that helps!

The reason you are seeing this error is because you are creating a new Multimedia Component using the Create method. When creating a new multimedia component, it looks at the BinaryContent.UploadFromFile property to upload the binary from a file path. If you look at your compData.BinaryContent.UploadFromFile property, its actually not set, so you are just copying a null value over, thus your property not set error.

Since you are copying components, have you thought of using the Copy method?

var component = _client.Copy(compData.Id, compData.LocationInfo.OrganizationalItem.IdRef, true, new ReadOptions());

You can then make any changes to the component if needed and save those changes.

The reason you are seeing this error is because you are creating a new Multimedia Component using the Create method. When creating a new multimedia component, it looks at the BinaryContent.UploadFromFile property to upload the binary from a file path. If you look at your compData.BinaryContent.UploadFromFile property, its actually not set, so you are just copying a null value over, thus your property not set error.

Since you are copying components, have you thought of using the Copy method?

var component = _client.Copy(compData.Id, compData.LocationInfo.OrganizationalItem.IdRef, true, new ReadOptions());

You can then make any changes to the component if needed and save those changes.

Update: Since you are copying across publications and can't use the Copy method as per your comment, you could probably do something like download the binary from the original component, and then set the UploadFromFile to the path of the downloaded binary. The code will look something along the lines of:

var compData = (ComponentData)client.Read(componentID, new ReadOptions());

var component = ...... // all your component creation stuff here...

if (compData.BinaryContent != null)
{
    string filePath = @"C:\TempFiles\" + Path.GetFileName(compData.BinaryContent.FileName);
    StreamDownloadClient downloadClient = new StreamDownloadClient("streamDownload_netTcp_2011");
    
    using (Stream stream = downloadClient.DownloadBinaryContent(compData.Id))
    {
        using (FileStream fs = File.Create(filePath))
        {
            using (MemoryStream ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                byte[] binary = ms.ToArray();
                fs.Write(binary, 0, binary.Length);
            }
        }
    }
    component.BinaryContent = new BinaryContentData
    {
        Filename = compData.BinaryContent.Filename,
        MultimediaType = compData.BinaryContent.MultimediaType,
        UploadFromFile = filePath
    }
}
component = (ComponentData)client.Create(component, new ReadOptions());

Note that the above assumes your code is running on the CMS Server. If you are running the code remotely, you'll have to upload the file using StreamUploadClient after downloading it, as in Bart's example. If your code is running on the CMS Server, then there's no need to upload, as your binary file will be saved on the server already during the download.

Hope that helps!

Source Link
Alex Klock
  • 2.9k
  • 16
  • 27

The reason you are seeing this error is because you are creating a new Multimedia Component using the Create method. When creating a new multimedia component, it looks at the BinaryContent.UploadFromFile property to upload the binary from a file path. If you look at your compData.BinaryContent.UploadFromFile property, its actually not set, so you are just copying a null value over, thus your property not set error.

Since you are copying components, have you thought of using the Copy method?

var component = _client.Copy(compData.Id, compData.LocationInfo.OrganizationalItem.IdRef, true, new ReadOptions());

You can then make any changes to the component if needed and save those changes.