Monday, June 20, 2022

How to cancel/delete pending jobs programmatically in Sitecore Content Hub ?

 

You may consider using the Web Client SDK to delete the pending job. Here is a sample code you may use to query the pending job entity ids in the system and delete the jobs. You may include your custom code to filter the jobs you want to delete.


var query = Query.CreateQuery(entities => from e in entities
                                          where e.DefinitionName == "M.Job"
                                          select e);

var scroller = MClient.Querying.CreateEntityScroller(query, TimeSpan.FromSeconds(200),
 EntityLoadConfiguration.DefaultCultureFull);

if (!scroller.CanMoveNext())
{
    return;
}
await scroller.MoveNextAsync().ConfigureAwait(false);

do
{
    foreach(var entity in scroller.Current.Items)
    {
        try
        {
            //filter the entity to delete (eg. condition=pending, logical unit=xx etc.)
            //await client.Entities.DeleteAsync((long)entity.Id);
        }
        catch (Exception ex)
        {
            Logger.Error(ex, $ "Failed to process job entity: {entity.Id}.
 Error: {ex.Message}");
        }
    }
    if (!scroller.CanMoveNext())
    {
        Logger.Info($ "Scroller cannot move next, stopping update.");
        break;
    }
}
while (await scroller.MoveNextAsync().ConfigureAwait(false));


For more details on Web Client SDK:
https://docs.stylelabs.com/contenthub/4.1.x/content/integrations/web-sdk/index.html
https://docs.stylelabs.com/contenthub/4.1.x/content/integrations/sdk-common-documentation/clients/entities-client.html
https://docs.stylelabs.com/contenthub/4.1.x/content/integrations/sdk-common-documentation/clients/querying-client.html

No comments:

Post a Comment

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...