Created
May 13, 2025 18:01
-
-
Save keyan1603/5788b6941798ae622876f2033a218762 to your computer and use it in GitHub Desktop.
Get Sitecore blob item and save it as a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $itemId = '--Your Item id--' | |
| $fieldId = '{40E50ED9-BA07-4702-992E-A912738D32DC}' #blob field id. Please change as per requirement if required. | |
| $outputFile = 'd:\custom.js' #file path to save | |
| # SQL connection | |
| $connectionString = "--your connection string--" | |
| $query = @" | |
| DECLARE @ItemId UNIQUEIDENTIFIER = '$itemId'; | |
| DECLARE @BlobFieldId UNIQUEIDENTIFIER = '$fieldId'; | |
| DECLARE @BlobId UNIQUEIDENTIFIER; | |
| SELECT | |
| @BlobId = CAST(Value AS UNIQUEIDENTIFIER) | |
| FROM | |
| [dbo].[Fields] | |
| WHERE | |
| ItemId = @ItemId AND FieldId = @BlobFieldId; | |
| SELECT Data FROM [dbo].[Blobs] WHERE BlobId = @BlobId; | |
| "@ | |
| # Run SQL query | |
| $connection = New-Object System.Data.SqlClient.SqlConnection $connectionString | |
| $command = $connection.CreateCommand() | |
| $command.CommandText = $query | |
| $connection.Open() | |
| $reader = $command.ExecuteReader() | |
| if ($reader.Read()) { | |
| $buffer = New-Object byte[] $reader.GetBytes(0, 0, $null, 0, 0) | |
| $reader.GetBytes(0, 0, $buffer, 0, $buffer.Length) | |
| [IO.File]::WriteAllBytes($outputFile, $buffer) | |
| } | |
| $connection.Close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment