How to programmatically attach an asset to a DataItem.
DataItem can have any assets attached. You can use the built-in method
CanvasCoreApp.ASSETS.AddAsset to record the relationship.
In addition to relating the
DataItem and the
Asset the method examines the file to extract
It requires that the file is already uploaded to NetCanvas server See the asset upload module (somewhere else) for Uploading details or use
CanvasCoreApp.ASSETS.AddAsset method received an
AddAsset object, that contains the details about the DataItem and Asset.
Here's a verbose & annotated example. There's a lot of repetition, some normalization would be good, but this works :
// Reference some DataItem
var dataItem = MyApp.DB.DataItem.GetById(1);
// name, the file name
var name = "my-image.png";
// srcFile, the full path to the asset
var srcFile = Net4orceApp.cApplicationPath + "__tmp__/" + name
// Execute the Method
var result = CanvasCoreApp.ASSETS.AddAsset(
// Passing AddAsset instance
// Source File (absolute/full)
// Destination/Container Path (relative)
// Destination File Path (relative)
@"__assets__/" + name,
// Who's uploaded the Asset
// Associate with DateItem Type
// And Associate with the specific DataItem
// The Destination file name
// Some Unique ref (used for uploader)
Check out the AddAsset method for simplified method of associating Assets & DataItems.