Hello all,
Can anyone provide me with an example how to upload a document to CS using PowerShell?
Simple document uploader in Powershell? Posted bydouwe.dijkstra@capgemini.com (Dijkstra, Douwe)On 01/18/2018 09:37 AM Hello all,Can anyone provide me with an example how to upload a document to CS using PowerShell?[To post a comment, use the normal reply function]Forum:Content Web Services ForumContent Server:My Support
Hi
As Apu said, I am a strong proponent of using PowerShell for Content Server administration and, in fact, have written a Command Line Interface to Content Server in Powershell. PS has some quirks but work more or less the same as C#. The following Forum topic covers the setup using the typical "GetAllFavorites()" example:
https://knowledge.opentext.com/knowledge/cs.dll?func=ll&objId=38136291&objAction=viewincontainer
I was looking to that CLI to see if I could just cut-and-paste an example of document upload, but it is too integrated with the CLI to be useful as a straight copy. Here is what I do know/remember from creating that code, some of this might have changed with CWS 16 and/or PS v4+, I haven't checked.
A small document can easily be uploaded using the docman.createDocument() web service, you first create the attachment:
if ($fileinfo.getType().Name -eq 'FileInfo') { updateauthentication try { $tmp = $sotdocman.getType().namespace $attachment = New-Object($tmp + ".Attachment") } catch [system.exception] { $message = "setDocument: Could not create attachment object because " + $($Error[0].Execption) LogMessage $message $ERRMSG_E_SYSTEM_ERROR $LOG_ERR throw {"setDocument: Could not create attachment object" } } $attachment.CreatedDate = $fileInfo.CreationTime; $attachment.FileName = $fileInfo.Name; $attachment.FileSize = $fileinfo.Length; $attachment.ModifiedDate = $fileInfo.LastWriteTime; }
Then, assuming you know where you are going to put it in CS and you are not creating any categories ($metadata):
try { $node = $sotdocman.createDocument($LLFolder.ID, $name, 'Inserted by Desktop Library', $false, $metadata, $attachment) }catch { $message = "setDocument: Could not create " + $name + " in " + $($LLFolder.ID) + " because " + $($Error[0].Exception.Message) LogMessage $message $ERRMSG_E_LOAD_APPLICATION $LOG_ERR throw {"setDocument: Could not create object"}}
If the file size is too large for createdocument() then you need to use the HTTP method as at least I could never get MTOM to work with Powershell:
try {$nodeContext = $sotdocman.createDocumentContext($LLFolder.ID, $name, 'Inserted by Desktop Library', $false, $metadata)catch [system.exception] { $message = "setDocument: Could not create context for " + $name + " in " + $($LLFolder.ID) + " because " + $($Error[0].Exception.Message) LogMessage $message $ERRMSG_E_LOAD_APPLICATION $SCRIPT:LOG_ERR throw {"setDocument: Could not create context for new document" }}updateauthenticationtry { $nodeID = HTTP-Upload $sotdocman.OTAuthenticationValue.AuthenticationToken $nodeContext $fileinfo }catch [system.exception] { $message = "setDocument: Could not upload content for " + $name + " because " + $($Error[0].Exception.Message) LogMessage $message $ERRMSG_E_LOAD_APPLICATION $SCRIPT:LOG_ERR throw {"setDocument: Could not upload content for new document" }}
where ConsutructURI and HTTP-Upload are:
Function ConstructURI( [string]$token, [string]$contextID, [string] $cufileName, [long] $cufileSize ){<# This creates a URI that we'll use to HTTP-Upload a file using the contextID as an authentication token Affected by: N/A#> [string] $url = $HTTP_CONTENT_HANDLER; $url += "?token=" + $([System.Web.HttpUtility]::UrlEncode($token)) + "&contextID=" + $contextID; if ( $cufileName -ne $null) {$url += "&fileName=" + $([System.Web.HttpUtility]::UrlEncode($cufileName)) + "&fileSize=" + $($cufileSize.ToString())}return $url;}
Function HTTP-Upload([string]$auth, [string]$context, [System.IO.FileSystemInfo]$hufileinfo){<# This function will upload a file using HTTP rather than a web service because the web service only works with small files and Powershell doesn't understand MTOM Affected by: N/A#>#construct the URL that represents the complete request $message = "HTTP-Upload(" + $auth + "," + $context + "," + $hufileinfo.Name LogMessage $message $LOG_DEBUG $LOG_DEBUG $url = ConstructURI $auth $context $hufileInfo.Name $hufileInfo.Length ; [System.Net.HttpWebRequest] $request = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($url) ## Add the method $request.Method = "POST" $request.Accept = "text/xml" $request.UserAgent = "Desktop Library" ## Create the request body if the verb accepts it (NOTE: utf-8 is assumed here) $bytes = [System.IO.File]::ReadAllBytes($LOCAL:hufileinfo.FullName) $request.ContentType = "text/xml" $request.ContentLength = $bytes.Length [System.IO.Stream] $outputStream = [System.IO.Stream]$request.GetRequestStream() $outputStream.Write($bytes,0,$bytes.Length) $outputStream.close() ## This is where we actually make the call. try { [System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $request.GetResponse() $sr = New-Object System.IO.StreamReader($response.GetResponseStream()) $txt = $sr.ReadToEnd() } ## This catches errors from the server (404, 500, 501, etc.) catch [Net.WebException] { [System.Net.HttpWebResponse] $resp = [System.Net.HttpWebResponse] $_.Exception.Response $messaage = $($resp.StatusCode) + ": " + $($resp.StatusDeSCRIPTion) LogMessage $message $ERRMSG_E_LOAD_APPLICATION $LOG_ERR throw {"http-upload: web exception"} }}
I hope I haven't removed too much and you can still follow the logic (my CLI provides support for reserving, preserving file names, caching, test runs etc that make it difficult to include the actual code and still have any semblance of understanding what is going oing :-))
Thanks Dave,
I forgot about this topic, but I'm picking this project up again.
Thank you for writing all of this down, I'm going to experiment a bit with it.