Content Hub: Search specific Assets via API

When we startet our Content Hub story, the first thing we had to do is migrating the assets from the old CMS to the Content Hub. To identify the assets we added a textfield “Old CMS ID” to the M.Asset, because later we have to do some changes for just the migrated assets, such like get public link for a specific one or create the public link.

First you need to configure the user in the Content Hub. Caution: It must be a superuser, otherwise you won’t get the right search results! We tried it first with ContentAdministrator, but this didn’t work out. Configure your user as API user and set clientId and clientSecret.

Now you can write the code. First we do the base part and define the endpoint and the oauth part.

// Your Sitecore Content Hub endpoint to connect to
Uri endpoint = new Uri("https://yourcontenthuburl");

// Enter your credentials here         
OAuthPasswordGrant oauth = new OAuthPasswordGrant         
{             
  ClientId = "yourid",             
  ClientSecret = "yoursecret",             
  UserName = "yoursuperuser",             
  Password = "youruserpassword"         
};         

Now we can setup the Web SDK Client.

// Create the Web SDK client         
IWebMClient client = MClientFactory.CreateMClient(endpoint, oauth);         client.TestConnectionAsync().Wait();         

Next we define the query for the asset with the specific OldCMSId.

Query query = Query.CreateQuery(
entities =>             
  from e in entities             
  where e.Property("OldCMSId") == 
  "0b990aa1-c2c0-4d71-bfef-3b17fa0dccfe" &&         
  e.DefinitionName == Asset.DefinitionName             
  select e
);         

Now we need to define the entity configuration for defining the result we want to get. (In our case we want to get the asset entity with the title.

EntityLoadConfiguration config = new EntityLoadConfiguration(             new CultureLoadOption(CultureInfo.GetCultureInfo("en-US")),             new PropertyLoadOption("Title"),             RelationLoadOption.None);         

And last we can execute the the query and get our specific asset-entity.

IEntity specificResult = client.Querying.SingleAsync(query, config).Result;

One thought on “Content Hub: Search specific Assets via API

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.