Sample script to send a ticket with attachments within Service Manager

Can anyone share the sample script to send a ticket along with attachments from third party to Service Manager

In extaccess i didnt find the attachments related fields but in the wsdl attachment is there as boolean. if I create a new custom field for attachment what should be the Data type and how to insert the attachment?

What can be written in SM to insert attachment? Please help

 

Tags:

  • Hi,

    Yes, Service Manager has allows all typical attachment operations using web service (SOAP, see Web Services Guide)  or REST (see RESTful API).

    Regarding Service Manager consuming an external web service, please refer to https://docs.microfocus.com/SM/9.52/Codeless/Content/programming/javascript/reference/javascript_object_attachment.htm and out of the box examples. I'll not cover this part here.

     

    Part 1:  Handling attachment with REST

    When an external system sends REST request, then you can first look at the service document: http://<server>:<port>/SM/9/rest

    Here you'll find for example:

    Incident

    Resource type          Supported Methods          URI

    Resource Collection        GET/POST          http://localhost:13080/SM/9/rest/incidents

    Single Resource             GET/POST/PUT    http://localhost:13080/SM/9/rest/incidents/{number}

    Resource Attachment Collection GET/POST/DELETE http://localhost:13080/SM/9/rest/incidents/{number}/attachments

    Resource Single Attachment GET/PUT/DELETE http://localhost:13080/SM/9/rest/incidents/{number}/attachments/{id}

     

    So by using this REST URL for an incident specified by number you can access ALL attachments or single identified by the attachment id. GET request on the Resource Attachment Collection returns a JSON document with all the attachments ids.

    Tip: For testing with REST GET requests you can use your browser, when SM.ini contains restaccessviabrowser:1 parameter.

    1. So try this:   localhost:13080/.../attachments

    ==> You get a JSON docment returned like this:

    {
      "@count": 29,
      "@start": 1,
      "@totalcount": 29,
      "Messages": [
        "1: undefined",
        "0: undefined",
        "vars.$smtestmessages: [C   object Datum] - ",
        "vars.$smtestresult: [C   object Datum] - {NULL, NULL, NULL}"
      ],
      "ResourceName": "attachment",
      "ReturnCode": 0,
      "content": [
        {"attachment": {
          "href": "cid:58ac7b3200242211802b29a8",
          "len": 23146,
          "name": "error.log",
          "type": "multipart/form-data; boundary=\"----=_Part_32_596452009.1487698738148\"",
          "xmime:contentType": "multipart/form-data; boundary=\"----=_Part_32_596452009.1487698738148\""
        }},
    .. ]
    }

    You see, this incident has 29 attachments, and for each you'll find information like the lenght, the file name or the attachment id (href).

    2. In this text we're using the attachment id retrieved in test 1:

    localhost:13080/.../58ac7b3200242211802b29a8

    ==> Now your browser should show a file being downloaded.

    3. In order to test other operations with attachment, we need to send requests with other HTTP methods.

    This requires either a specific plugin being installed in your browser or to implement an HTML page to test. The one below can be used for your testing.

    Currently it is configured to delete the attachment we worked with above.

     

    <!DOCTYPE html>
    <html>
    
    <head>
      <script language="javascript">
    
    function sendrequest(method, url, contenttype, user, password, sync, request, callback)
    {
    
     // Create a XMLHttpRequest object
      var ajax = null;
      if(window.XMLHttpRequest){          //Google Chrome, Mozilla Firefox, Opera, Safari, IE 10
    	ajax = new XMLHttpRequest();
      }
      else
      {
           return;
      }
    
      // Add data to ajax object
      ajax.open( method, url, sync, user, password );
    
      if (contenttype) ajax.setRequestHeader("Content-Type", contenttype);
    
      if (!sync)
      {
          // Define how response will be processed
          ajax.onreadystatechange = function()
          {
             var result = {};
    
             switch (this.readyState)
    	 {
               case 0: result.status = "ReadyState: "   this.readyState; break;
               case 1: result.status = "ReadyState: "   this.readyState; break;
               case 2: result.status = "ReadyState: "   this.readyState; break;
               case 3: result.status = "ReadyState: "   this.readyState; break;
               case 4:
                {
                   var result = {};
                   result.status = ajax.status;
                   result.response = ajax.responseText;
    
                   if (callback) callback(result);
    
                   break;
                }
             }
    
             updatePage(result.status, result.response);
    
             return result;
          }
      }
    
      // send the request
      if (request)  ajax.send( request );
      else ajax.send();
    
      if (sync)
      {
          var result = {};
          result.status = ajax.status;
          result.response = ajax.responseText;
    
          updatePage(result.status, result.response);
          if (callback) callback(result);
      }
    }
    
    function updatePage(status, response)
    {
       document.getElementById("status").innerHTML=status;
       document.getElementById("response").value=response;
    }
    
    </script>
    </head>
    
    <body>
    <h1>Test SM RestfulAPI</h1>

      Request: <textarea id="request" width="80" height="60"></textarea>
      <p></p>
      Status: <p id="status"></p>
      Response: <textarea id="response" width="80" height="60"></textarea>

      <script language="javascript">

    // Defaults
        var method = "GET"
        var request = null;
        var contenttype = null;
        var sync = false;

    // Credentials
        var user = "falcon";
        var password = "";

    // TEST
        var method = "DELETE";
        var URL = "">localhost:13080/.../58ac7b3200242211802b29a8";

        sendrequest(method, URL, contenttype, user, password, sync, request);

      </script>
    </body>
    </html>

    Note: If you save this to a file on your local machine and open it in a browser, it depends of the browser if it allows this REST request or not. For example Chrome will block the request because of Access-Control-Origin not specify, IE will to allow dynamic content, but execute it if granted. 

  • Part 2 : Attachments with SM web services

    Regarding Web services I use a testing tool that allows me to input the requests. In this case I used SoapUI.

    1. Retrieve attachment information

    Set "attachmentInfo" attribute to true.

    <soapenv:Envelope xmlns:soapenv="">schemas.xmlsoap.org/.../" xmlns:ns="">http://schemas.hp.com/SM/7" xmlns:com="">schemas.hp.com/.../Common" xmlns:xm="">www.w3.org/.../xmlmime">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:RetrieveIncidentRequest attachmentInfo="true" attachmentData="false">
             <ns:model>
                <ns:keys><ns:IncidentID>IM10003</ns:IncidentID></ns:keys>
                <ns:instance></ns:instance>
             </ns:model>
          </ns:RetrieveIncidentRequest>
       </soapenv:Body>
    </soapenv:Envelope>

     

    == > Response will contain the complete Incident data (shortend in example below) and the attachment information:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="">schemas.xmlsoap.org/.../">
       <SOAP-ENV:Body>
          <RetrieveIncidentResponse message="Success" returnCode="0" schemaRevisionDate="2016-05-10" schemaRevisionLevel="0" status="SUCCESS" xsi:schemaLocation="servicecenter.peregrine.com/PWS ">bra03sm25.eu.hpecorp.net:13080/.../Incident.xsd" xmlns="">servicecenter.peregrine.com/PWS" xmlns:cmn="">servicecenter.peregrine.com/.../Common" xmlns:xsd="">www.w3.org/.../XMLSchema" xmlns:xsi="">www.w3.org/.../XMLSchema-instance">
             <model>
                <keys>
                   <IncidentID type="String">IM10003</IncidentID>
                </keys>
                <instance recordid="IM10003 - Not able to print" uniquequery="number=&quot;IM10003&quot;">
                   <IncidentID type="String">IM10003</IncidentID>
    [..]
                   <attachments>
                      <cmn:attachment href="cid:551bb5d00003a005805a1278" len="19779" name="cwc_labels_ru.utf8" type="application/octet-stream" upload.by="falcon" upload.date="04/01/15 03:09:36"/>
                      <cmn:attachment href="cid:551bb5d00008c006805a1278" len="12842" name="cwc_labels_en.properties" type="application/octet-stream" upload.by="falcon" upload.date="04/01/15 03:09:36"/>
                      <cmn:attachment href="cid:551bb5d0000b8007805a1278" len="14606" name="cwc_labels_de.utf8" type="application/octet-stream" upload.by="falcon" upload.date="04/01/15 03:09:36"/>
                      <cmn:attachment href="cid:5668051d000e901f808612a0" len="21579" name="cimultirel.unl" type="application/octet-stream" upload.by="falcon" upload.date="12/09/15 03:40:29"/>
                      <cmn:attachment href="cid:566808350013605b809bd6c0" len="27944" name="cimultirel_v2.1.unl" type="application/octet-stream" upload.by="falcon" upload.date="12/09/15 03:53:41"/>
                   </attachments>
                </instance>
             </model>
          </RetrieveIncidentResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

     

    In the attachment information find the href attribute for each attachment.

     

    2. Retrieve a single attachment by href from Service Manager

    You need to specify the attachment  in the instance and set the action attribute to "get".

    <soapenv:Envelope xmlns:soapenv="">schemas.xmlsoap.org/.../" xmlns:ns="">http://schemas.hp.com/SM/7" xmlns:com="">schemas.hp.com/.../Common" xmlns:xm="">www.w3.org/.../xmlmime">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:RetrieveIncidentRequest attachmentInfo="false" attachmentData="true">
             <ns:model>
                <ns:keys><ns:IncidentID>IM10003</ns:IncidentID></ns:keys>
                <ns:instance>
                   <attachments>
                      <attachment href="cid:551bb5d00003a005805a1278" action="get"/>
                   </attachments>
                </ns:instance>
             </ns:model>
          </ns:RetrieveIncidentRequest>
       </soapenv:Body>
    </soapenv:Envelope>

     

     

    Now the attachment appears below the XML response in Soap UI like this:

    SM attachment_SOAPUI1.PNG

     

     

     

     

     

     

     

     

     

     

     

    Looking at the raw message shows the file contents is put into a separate HTTP message part:

    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Set-Cookie: JSESSIONID=7A8FB01D34C0CF736842A48724DF3FB5; Path=/sc62server/; HttpOnly
    Keep-Alive: timeout=1200000, max=1000
    Connection: Keep-Alive
    Content-Encoding: gzip
    Content-Type: multipart/related; boundary="----=_Part_56_30479565.1495112821461"; type="text/xml"
    Transfer-Encoding: chunked
    Date: Thu, 18 May 2017 13:07:01 GMT
    
    ------=_Part_56_30479565.1495112821461
    Content-Type: text/xml; charset=utf-8
    
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="">schemas.xmlsoap.org/.../"><SOAP-ENV:Body><RetrieveIncidentResponse message="Success" returnCode="0"  
    [..]
    ------=_Part_56_30479565.1495112821461
    Content-Type: application/octet-stream
    Content-Location: cwc_labels_ru.utf8
    Content-ID: cid:551bb5d00003a005805a1278
    
    # <!-- (C) Copyright Hewlett-Packard Company, L.P. All Rights Reserved. -->
    AboutPage.Title=О программном обе�печении HP
    [..]

     

    3. Delete an attachment in SM:

    To remove an attachment, set the action for the attachment to "remove".

    Any manipulate of an attached file require an successful update to the target record - so the incident in our example. To make it successful, all validations must be passed - hence the request below contains some more fields than just the attachment information.

    <soapenv:Envelope xmlns:soapenv="">schemas.xmlsoap.org/.../" xmlns:ns="">http://schemas.hp.com/SM/7" xmlns:com="">schemas.hp.com/.../Common" xmlns:xm="">www.w3.org/.../xmlmime">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:UpdateIncidentRequest attachmentInfo="true" attachmentData="false">
             <ns:model>
                <ns:keys>
                   <ns:IncidentID>IM10003</ns:IncidentID>
                </ns:keys>
                <ns:instance>
    
                   <Category>incident</Category>
                   <Area>hardware</Area>
                   <Subarea>hardware failure</Subarea>
    
                   <JournalUpdates>
                      <JournalUpdates>Drop attachment</JournalUpdates>
                   </JournalUpdates>
    
                   <ns:attachments>
                      <com:attachment href="cid:551bb5d00003a005805a1278" action="remove"/>
                   </ns:attachments>
                   
                </ns:instance>
             </ns:model>
          </ns:UpdateIncidentRequest>
       </soapenv:Body>
    </soapenv:Envelope>

     

    4. Attach a new file to an incident

    Attaching a file uses the action "add".

    I found that web services allow a lot of different option to be specified - and this makes the attachment request successful or not. You may expect some trial&error testing here.

    The request looks like that:

    <soapenv:Envelope xmlns:soapenv="">schemas.xmlsoap.org/.../" xmlns:ns="">http://schemas.hp.com/SM/7" xmlns:com="">schemas.hp.com/.../Common" xmlns:xm="">www.w3.org/.../xmlmime">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:UpdateIncidentRequest attachmentInfo="true" attachmentData="false">
             <ns:model>
                <ns:keys>
                   <ns:IncidentID>IM10003</ns:IncidentID>
                </ns:keys>
                <ns:instance>
    
                   <Category>incident</Category>
                   <Area>hardware</Area>
                   <Subarea>hardware failure</Subarea>
    
                   <JournalUpdates>
                      <JournalUpdates>Attach a file</JournalUpdates>
                   </JournalUpdates>
    
                   <ns:attachments>
                      <com:attachment href="cid:soapui.log" name="soapui.log" type="application/octet-stream" action="add"/>
                   </ns:attachments>
                   
                </ns:instance>
             </ns:model>
          </ns:UpdateIncidentRequest>
       </soapenv:Body>
    </soapenv:Envelope>

     

    The href attribute here refers to an attachment in the request HTTP message. So the file has to be included here, and the content id may vary from what is used in Service Manager then and replied in the response.

    SM attachment_SOAPUI2.PNG

     

     

     

     

     

     

     

     

    This is then the attachment information as we receive in the response with the previously existing and the new file attachment:

    [..]
                   <attachments>
                      <cmn:attachment href="cid:551bb5d00008c006805a1278" len="12842" name="cwc_labels_en.properties" type="application/octet-stream" upload.by="falcon" upload.date="04/01/15 03:09:36"/>
                      <cmn:attachment href="cid:551bb5d0000b8007805a1278" len="14606" name="cwc_labels_de.utf8" type="application/octet-stream" upload.by="falcon" upload.date="04/01/15 03:09:36"/>
                      <cmn:attachment href="cid:5668051d000e901f808612a0" len="21579" name="cimultirel.unl" type="application/octet-stream" upload.by="falcon" upload.date="12/09/15 03:40:29"/>
                      <cmn:attachment href="cid:566808350013605b809bd6c0" len="27944" name="cimultirel_v2.1.unl" type="application/octet-stream" upload.by="falcon" upload.date="12/09/15 03:53:41"/>
                      <cmn:attachment href="cid:591da42c0029800280680d00" len="79002" name="soapui.log" type="application/octet-stream" upload.by="falcon" upload.date="05/18/17 07:39:56"/>
                   </attachments>
    [..]

     

  • Hi,

     

    check this out: /it_ops_mgt/itsm/sws-smgr/f/itrc-695/190583/restful-api---attachments-for-incident

    Complete guide to send an attachment via RESTful API.