Get Started!

AddAsset Method

To simplify the attaching of assets to a dataItem, an "AddAsset" method is exposed via the ControlBase class.

General

As can be seen in the Attaching Assets Post there's a number of options available to the developer to configure how the assets (uploaded files) and the Data Base Items are associated. The shear number of options means that the logic can be quite verbose.

To simplify this process of attachment the AddAsset method is exposed via the ControlBase ASCX class, using the standard defaults for attaching assets which are :

  1. The source / temp file location is "__tmp__/".

  2. The asset should be stored persistently in "__assets__/".

The AddAsset Method

The signature of the method is :

public Asset AddAsset(
  DataItem dataItem,  // The Associated DataItem
  string name,        // The File's Name
);

The dataItem parameter is the peice of data the asset / file should be associated with.

The name parameter is the name of the file, such as "file-name.txt".

The AddAsset method returns an Asset object which contains all of the information about the file, suchas it's FilePath, URL, MimeType etc.

Example

Here's an example of attaching an already uploaded file located in "__tmp__/" to a WebPage, from an ASCX control.

public WebPage oWebPage { get; set; }

public void Example(){

  // Associate "file-name.txt" with the current 
  // oWebPage dataItem 
  var asset = AddAsset(
    oWebPage, 
    "file-name.txt"
  );


  // Get & use the public URL of the Asset
  var assetPath = asset.cPath;

}

The example above locates a file found in "__tmp__/" named "file-name.txt", copies it into the "__assets__/" path for that WebPage.

If the WebPage had an Id of 123 the Assets public URL is :

/__assets__/webpages/00123/file-name.txt

This can be obtained via the cPath property of the Asset Object, returned from the AddAsset method.

Overload Source Path Method

The Method has an optional overload with the signature:

public Asset AddAsset(
  DataItem dataItem,  // The Associated DataItem
  string name,        // The File's Name
  string srcPath,     // The Source / Temp file location
);

If the file that is to be associated with the DataItem is located in a folder that is not the "__tmp__/" path, this srcPath parameter allow you to override where it sources the asset from.

This means you can use the same method to associate any file within the scope of the application with a dataItem & have it included in the Asset Backup mechs.