This blog details connecting to the Content Hub sandbox from a .NET Core application and getting an Excel report with all assets to filter by file extension.
The GitHub source code for this application is here. This has been created on Visual Studio 2022 and .NET 6.0. That uses two references in Nuget: Stylelabs.M.Sdk.WebClient version 4.2.6 and ClosedXML version 0.101.0
The prerequisites for this example are OAuth credentials, a username, and a password to connect to the Content Hub Sandbox. To create the OAuth credentials go to the option indicated below on the Content Hub Manage screen.
Click on the “+OAuth Client” button to create a new entry providing the following required information.
Configure on Visual Studio the following package source feed with this URL https://slpartners.myget.org/F/m-public/api/v3/index.json
On the ContentHub_WebClientSDK solution from GitHub, there is the MClient class to configure the credentials obtained previously.
Below is the explanation of some parts of the ContentHubController class. The following code indicates the data to be loaded in each entity/asset when the query is executed.
var assetLoadConfiguration = new EntityLoadConfiguration()
{
PropertyLoadOption = new PropertyLoadOption("Title", "FileName", "FileProperties"),
RelationLoadOption = RelationLoadOption.None,
CultureLoadOption = CultureLoadOption.Default,
};
The query to get all assets from Content Hub.
var query = Query.CreateQuery(entities =>
from e in entities
where e.DefinitionName == "M.Asset"
select e);
The following one creates an IEntityIterator that iterates over all results matched by the query.
IEntityIterator iterator = MClient.Client.Querying.CreateEntityIterator(query, assetLoadConfiguration);
while (await iterator.MoveNextAsync())
{
//Code to process the entity
}
Get the title, file name, and file properties from the asset that is iterated.
currentTitle = entity.GetPropertyValue<string>("Title");
currentFileName = entity.GetPropertyValue<string>("FileName");
properties = entity.GetPropertyValue<JToken>("FileProperties");
Get the extension data from the asset file properties which is a JObject.
JObject inner = properties["properties"].Value<JObject>();
var token = inner.SelectToken("extension");
//Verify if extension exists
if (token != null)
{
//Get the extension value from FileProperties
var extension = inner["extension"].ToString();
// ...
}
If you run ContentHub_WebClientSDK_Assets project, the following screen is displayed.
Once it is executed successfully the next screen is displayed with the option to download the file.
The file name to download is AssetsContentHub.xls. Bellow an example.
I hope you have learned with this example a little bit more about Content Hub and the Web client SDK usage. The official documentation for that SDK is here.