allBlogsList

Programmatically Creating Public Links in Sitecore Content Hub

Intro

Public links allow users to share assets with external people who do not have permission to download the assets. This functionality provides an unsigned public link to any pre-generated file rendition of an asset. This post will explain how to add public links to existing Renditions of an Asset programmatically.

Content Hub documentation has explains public links and describes how to create them from the Content Hub interface. This post describes two ways of creating public links programmatically:

  • From within Content Hub: an action to create a public link, automatically triggered and executed inside Content Hub instance when Asset is created or updated.
  • From an external client: sample code using the Content Hub client SDK.

Steps to create an action to auto-generate public links in Content Hub

  • Create Code Script

    • Log into Content Hub and navigate to Scrips… public

    • Click on "+ Script" button, fill out Name and optional Description fields, choose "Action" as its type public

    • Once in Script Editor, click on "Edit", paste the below code (feel free to update as needed), then click "Save Changes", "Build" and finally "Publish"

var assetId = Context.TargetId;
CreateRendition("preview", assetId.Value);
MClient.Logger.Info("Created public link 'preview' for asset with id '" + assetId + "'");
CreateRendition("downloadOriginal", assetId.Value);
MClient.Logger.Info("Created public link 'downloadOriginal' for asset with id '" + assetId + "'");

async void CreateRendition(string rendition, long assetId)
{
		var publicLink = await MClient.EntityFactory.CreateAsync("M.PublicLink");
		if (publicLink.CanDoLazyLoading())
		{
			await publicLink.LoadMembersAsync(new PropertyLoadOption("Resource"), new RelationLoadOption("AssetToPublicLink"));
		}
		publicLink.SetPropertyValue("Resource", rendition);
		var relation = publicLink.GetRelation<IChildToManyParentsRelation>("AssetToPublicLink");
		if (relation == null)
		{
			MClient.Logger.Error("Unable to create public link: no AssetToPublicLink relation found.");
			return;
		}
		relation.Parents.Add(assetId);
		await MClient.Entities.SaveAsync(publicLink);
}
  • Make sure this newly created script is enabled like so: public

  • Create Action

    • From Content Hub management page navigate to Actions and click on "New Action public
  • Fill out Action Name and optional Label. Set Action type to "Action Script" and choose the above-created script like so: public

  • Create Trigger

    • Go back to Content Hub management page, navigate to Triggers, click on "NEW TRIGGER" link, and populate its properties as follows:

      • The "General" tab specifies when and how the action will be triggered public
    • The "Conditions" tab allows specifying which schema entities can trigger the action. For this example click on "Add Definition" and select "M.Action" public

    • The "Actions" tab is where one or more actions can be selected to executed - choose previously created action. public

    • Save trigger, then choose to activate it

Now, with the above-created script, action, and trigger in place, public links will be created on each newly created asset.

Creating Public Links from client code

Below is the sample code to create a public link from within an external client (a client app, communicating with Content Hub via its APIs)

public async Task<long> CreatePublicLinkAsync(long assetId)
{
	IEntity publicLink = null;
	
  try
	{
		publicLink = await _mClient.EntityFactory.CreateAsync("M.PublicLink").ConfigureAwait(false);
	}
	catch (Exception ex) when (ex.Message == "Could not find any cultures.")
	{
			publicLink = await _mClient.EntityFactory.CreateAsync("M.PublicLink", new CultureLoadOption(new string[] { "en-US" })).ConfigureAwait(false);
	}

	// Set the rendition type/resource
	publicLink.SetPropertyValue("resource", "downloadoriginal");
	// Link the public link to the asset
	var assetTopublicLinkRelation = publicLink.GetRelation("AssetToPublicLink", RelationRole.Child);
  assetTopublicLinkRelation.SetIds(new long[] { assetId });
  
	// Save the public link
	long publicLinkId = await _mClient.Entities.SaveAsync(publicLink).ConfigureAwait(false);
	return publicLinkId;
}