Media field

The sfMediaField directive enables you to select and display a document or a video from the existing libraries in your website. In addition, you can use this directive to upload a document or a video to a library.

After you select a document or a video, the default template lists information about its base properties such as type, size and upload date. You can select another item or edit the properties of the current item. For more information, see Document selector and Video selector.

You can use the directive in the frontend, as well as in the backend. For example, on a page, as well as in a widget designer.

The sfMediaField is a directive with an isolated that is defined in a module with the same name: sfMediaField.

The following image is an example of a media field used for documents:

Document field

Media field attributes

The sfMediaField directive exposes the following attributes:

Attribute Description
Accepts a scope variable that holds the id of the selected item.
Accepts a scope variable that holds the selected item.
Specifies the media type. The value can be documents or videos.
Accepts a provider name to use when retrieving the items to display.
Allows you to override the template of the media field.
Specifies the assembly where the template of the media field is located.
Specifies if the document/video selector will open initially if no item is bound.

Specifies the permissions that allow all users to access this selector. The options are:

  • sfDocumentsSettings
  • sfVideosSettings
  • sfImagesSettings
sf-master Defines whether the field displays media items and their statues, such as DraftLocked, and so on, or only published items. To configure the Media field to display master items, that is, all media items, independently of their status, set the sf-master="true". By default, the field displays only live items and the value of the sf-master is set to false.

NOTE: When you set the sf-master="true", the Media field lists the items but does not display their status.

Add the media field directive

The following example demonstrates how to add media field directive in a widget designer's view.

To enable AngularJs to link the sfMediaField directive in your custom designer view, you must load the script of the directive and add a dependency to the module:

  1. Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own DesignerView.YourView.json file. If you have a JSON file that matches the convention (even if empty), this automatic scripts registration will not occur. In your DesignerView.YourView.json file, add a components array. If you want to use the media field for documents, add "sf-document-field" to the components array. If you want to use it for videos, add "sf-video-field". As a result, the file content should be similar to the following:
  2. Sitefinity CMS automatically finds all AngularJS modules you rely on and references the widget designer to them. In case you rely on custom AngularJS modules or have logic that needs an AngularJS controller, you can create your own designerview-<yourview>.js file. If you have a .js file that matches the convention (even if empty), this automatic modules referencing will not occur. In your designerview-yourview.js file, place the following code snippet:

    Since there is no selected item when the controller is initialized for the first time, the sf-auto-open-selector attribute in the code above opens a document or a video selector depending on sf-media-type value. After you select an item, the item and its ID are stored in their respective scope variables: selectedMedia and selectedMediaId. The mediaProvider variable holds the name of the provider from which to load the document libraries.

    NOTE: The document and the video selector can load documents or videos from different providers.

  3. In your DesignerView.YourView.cshtml file, place the following tag where you want to render the sfMediaField directive:

Additional resources

External links



Was this article helpful?

Next article

File URL field