Simplifying Asset Sharing in Sitecore Content Hub with Public URLs.

Are you struggling with managing asset sharing in Sitecore Content Hub? Sharing assets internally or externally can sometimes be a hassle, especially when dealing with multiple stakeholders and teams. However, fear not! Sitecore Content Hub provides a robust solution for creating public URLs for assets, streamlining the sharing process and enhancing collaboration.

Today, we’ll delve into the process of generating public URLs for assets in Sitecore Content Hub using PowerShell. By following a few simple steps, you can effortlessly create public links for your assets, facilitating seamless sharing across teams and beyond.

Understanding the Script

Let’s break down the PowerShell script provided for generating public URLs:

function GeneratePublicURL {
    [CmdletBinding()]
    param (        
        [Parameter(Mandatory = $true, Position = 1)]
        [String]$token,

        [Parameter(Mandatory = $true, Position = 2)]
        [String]$hostUri,

        [Parameter(Mandatory = $true, Position = 3)]
        [String]$assetId,

        [Parameter(Mandatory = $true, Position = 4)]
        [String]$assetUrl
    )

    begin {
        Import-Function Send-Request
        $renditionName = "downloadOriginal"
    }

    process {
        try {
            # Query in Content Hub to check if the asset public link already exists
            $query = "https://$hostUri/api/entities/query?query=Definition.Name=='M.PublicLink' AND Parent('AssetToPublicLink').id==$assetId AND String('Resource') == '$renditionName'"
            $assetToPublicLinkResponse = Send-Request -Uri $query -Method "GET" -Token $token

            # Handle response
            if ($assetToPublicLinkResponse.response.StatusCode -eq "OK") {
                # Check if public links exist
                if ($assetToPublicLinkResponse.content.total_items -gt 0) {
                    Write-Log "(GeneratePublicURL) - A public link has already been created. Public link count: $($assetToPublicLinkResponse.content.total_items)"
                } else {
                    Write-Log "(GeneratePublicURL) - No public links found for asset ID: $assetId"
                    # Prepare request body		
                    $requestBody = @{
                        properties = @{
                            RelativeUrl = $assetUrl
                            Resource = $renditionName                                    
                        }
                        is_root_taxonomy_item = $false
                        is_path_root = $false
                        inherits_security = $true 							
                        entitydefinition = @{href = "https://$hostUri/api/entitydefinitions/M.PublicLink"}
                        relations = @{
                            AssetToPublicLink = @{
                                parents = @(@{href="https://$hostUri/api/entities/$assetId"})
                            }
                        }
                    } | ConvertTo-Json -Depth 4

                    $content = [System.Net.Http.StringContent]::new($requestBody, [System.Text.Encoding]::UTF8, "application/json")
                    # Send POST request to create public link
                    $response = Send-Request -uri "https://$hostUri/api/entitydefinitions/M.PublicLink/entities" -method "POST" -content $content -token $token

                    # Check response
                    if ($response.response.StatusCode -eq "Created") {
                        Write-Log "(GeneratePublicURL) - Public link created successfully. M.PublicLink(Id:$($response.Content.id) - Identifier:$($response.Content.identifier))"				
                    } else {
                        Write-Log "(GeneratePublicURL) - Failed to create public link. StatusCode: $($response.StatusCode)"
                        Write-Log "(GeneratePublicURL) - Response Content: $($response.Content)"
                    }
                }
            }
        } catch {
           Write-Log "Error creating public link: $_"
        }
    }

    end {
        Write-Log "(GeneratePublicURL) - Task Completed."
    }
}

$assetURL = "XXXXXXX"
$token = "XXXXXX"
$hosturi = "XXXXXXXX"
$assetId = "XXXXXXXX"
$publicLinkRes = GeneratePublicURL -token $token -hostUri $hosturi -assetId $assetId -assetUrl $assetURL -Verbose

How to Use the Script

To use the script, follow these steps:

  1. Token: Ensure you have a valid token for authentication.
  2. Host URI: Provide the URI of your Sitecore Content Hub instance.
  3. Asset ID: Specify the ID of the asset for which you want to generate a public URL.
  4. Asset URL: Input the asset’s relative URL.

Executing the Script

Once you’ve filled in the required parameters, execute the script. It will check if a public link already exists for the asset. If not, it will create a new public link using the provided asset URL.

Conclusion

With Sitecore Content Hub’s ability to generate public URLs for assets, sharing and collaborating on digital assets becomes a breeze. By leveraging the power of Sitecore PowerShell scripting, you can automate the process and streamline your workflow. Say goodbye to tedious manual sharing processes and embrace efficient asset management with Sitecore Content Hub.

So why wait? Start simplifying asset sharing today with Sitecore Content Hub’s public URL generation with help of Sitecore PowerShell script!

One response to “Simplifying Asset Sharing in Sitecore Content Hub with Public URLs.”

  1. […] Generating Public Links from Uploaded Assets in CH DAM: Explore the functionality of our module in generating public links for assets uploaded into Content … […]

Leave a comment

I’m Tushar

a seasoned software developer with a deep-rooted love for innovation and problem-solving. From my early days as a curious coder to my current role as a technology enthusiast, every step of this journey has been filled with learning opportunities and rewarding experiences.

Let’s connect