--- myst: substitutions: method: |- ```{image} /img/code_reference/net_library/method.ico ``` prop: |- ```{image} /img/code_reference/net_library/prop.ico ``` --- # IMediaResources Interface Provides the interface for working with MediaResources: Adding, Getting and/or Updating -ErpMedias. **Namespace:** powerGate.Erp.Client\\ **Assembly:** powerGate.Erp.Client.dll ## Syntax ```{code-block} CSharp :linenos: true public interface IMediaResources ``` ## Properties | Type | Name | Description | |------------------------------------------|-----------|--------------------------------------------| | {{prop}} [IErpEntitySet]() | EntitySet | Gets the EntitySet of this MediaResources. | ## Methods | Type | Name | Description | |--------------------------|------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | {{method}} void | GetErpMedia(Dictionary\ keys, [Stream][] stream) | Downloads the Media Resource of an existing [Media Link Entry (MLE)][] and writes it to the passed Stream. | | {{method}} [ErpObject][] | AddErpMedia([MediaCreateOptions][] options) | Creates a new [Media Link Entry (MLE)][] with the request body containing the Media Resource (MR) and the Content-Type header indicating its media type. In other words it will create a streamable entity (Media Link Entry) and upload it together with the specified file (Media Resource). | | {{method}} void | UpdateErpMedia([MediaUpdateOptions options][]) | Updates the Media Resource of an existing [Media Link Entry (MLE)][]. | [Stream]: https://msdn.microsoft.com/en-us/library/system.io.stream%28v=vs.110%29.aspx [MediaCreateOptions]: mediacreateoptions [MediaUpdateOptions options]: mediaupdateoptions ## Extension Methods | Type | Name | Description | |--------------------------|------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------| | {{method}} [ErpObject][] | AddErpMedia(Stream data, Dictionary\ properties=null, string contentType = “application/octet-stream”) | Overloaded. Creates a new [Media Link Entry (MLE)][] with the request body containing the Media Resource (MR) using the specified parameter values. | | {{method}} void | UpdateErpMedia(Dictionary\ keys, Stream data, string contentType = “application/octet-stream”) | Overloaded. Updates the Media Resource of an existing [Media Link Entry (MLE)][] using the specified parameter values. | [ErpObject]: erpobject [Media Link Entry (MLE)]: http://www.odata.org/documentation/odata-version-2-0/operations ## Exceptions In case of an invalid request the above methods will throw a [WebRequestException](). ## Remarks The **GetErpMedia(...)** function copies the downloaded binary data into the passed Stream object, and therefore the Stream needs to be [writable]().\ For Streams that support [seeking](), the position is automatically set to starting position in order to directly allow reading it's content. The **CreateErpMedia(...)** and **UpdateErpMedia(...)** functions reading and uploading the binary data from the passed Stream object, and therefore the Stream has to be [readable](). ## Examples In the following examples we are using public OData Services () for demonstration purposes: **Download ErpMedia** ```{code-block} CSharp :linenos: true using System; using System.Collections.Generic; using System.IO; using powerGate.Erp.Client; namespace MediaResourcesSample { class Program { static void Main(string[] args) { using (var erpclient = new ErpClient()) { using (var service = erpclient.ConnectErp(new Uri("http://services.odata.org/V3/OData/OData.svc"))) { var advertisments = service.EntitySets["Advertisements"]; //Check if the EntitySet supports MediaResources if (advertisments.SupportsMediaResources) using(var downloadData = new FileStream(@"C:\Temp\DownloadedFile.txt", FileMode.Create,FileAccess.Write)) { //Download MediaResource advertisments.MediaResources.GetErpMedia(new Dictionary { { "ID", Guid.Parse("db2d2186-1c29-4d1e-88ef-a127f521b9c6") } }, downloadData); } } } } } } ``` **Add a new ErpMedia** ```{code-block} CSharp :linenos: true using System; using System.Collections.Generic; using System.IO; using powerGate.Erp.Client; namespace MediaResourcesSample { class Program { static void Main(string[] args) { using (var erpclient = new ErpClient()) { using (var service = erpclient.ConnectErp(new Uri("http://services.odata.org/V3/OData/OData.svc"))) { var advertisments = service.EntitySets["Advertisements"]; //Check if the EntitySet supports MediaResources if (advertisments.SupportsMediaResources) using(var uploadData = File.OpenRead(@"C:\Temp\TestMedia.txt")) { var mediaCreateOptions = new MediaCreateOptions { Data = uploadData , ContentType = "text/plain", Properties = new Dictionary { { "Name", "My new Advertisment, Yeaahh!" } } }; //Create a new Media Link Entry with a MediaResource var newAdvertisment = advertisments.MediaResources.AddErpMedia(mediaCreateOptions); Console.Write("Advertisment ID: {0}", newAdvertisment["ID"]); } } } } } } ``` **Update existing ErpMedia** ```{code-block} CSharp :linenos: true using System; using System.Collections.Generic; using System.IO; using powerGate.Erp.Client; namespace MediaResourcesSample { class Program { static void Main(string[] args) { using (var erpclient = new ErpClient()) { using (var service = erpclient.ConnectErp(new Uri("http://services.odata.org/V3/OData/OData.svc"))) { var advertisments = service.EntitySets["Advertisements"]; //Check if the EntitySet supports MediaResources if (advertisments.SupportsMediaResources) using(var uploadData = File.OpenRead(@"C:\Temp\TestMedia.txt")) { //Update MediaResource of Media Link Entry advertisments.MediaResources.UpdateErpMedia( keys: new Dictionary { { "ID", Guid.Parse("f89dee73-af9f-4cd4-b330-db93c25ff3c7") } }, data: uploadData ); } } } } } } ``` ## See also **Reference** - [powerGate.Erp.Client namespace]() - [Add-ERPMedia cmdlet]() - [Get-ERPMedia cmdlet]() - [Update-ERPMedia cmdlet]()