• REST API - OpenKM/services/rest/mail/create

  • We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #42597  by ofstab
 
I am working in vb.net, and have a fairly good understanding of the sdk4csharp API. One of the things that that does not do is allow for the creation of mail items in the repository (using community 6.3.1).

I have been experimenting with developing my own interface with the REST API. So far, I am able to extract information from the repository using folder/getChildren to obtain a list of folders, folder/getProperties to obtain information about a folder, and mail/getProperties to obtain information about an email.

Interestingly, I was not able to deserialize the XML that resulted into a bean.folder or bean.mail object without problems. What I had to do was the classes mail, folder and document that I obtained from extracting from the SOAP interface as suggested here: https://wiki.openkm.com/index.php/C_Sha ... OpenKM_6.2.

Anyway, the extraction of information FROM openkm through the REST API is working fine. What I cannot even get started with is inserting information INTO openkm.

I presume from the following that I have to feed the 'create' method an xml file, and that it will return an xml file. What form does the xml file have to take? Is it the same xml file (mail) that is returned from a mail/getProperties query? Does it have to be encoded in any particular format?
Code: Select all
<resource path="create">
<method name="POST">
  <request>
    <representation mediaType="application/xml"/>
    <representation mediaType="application/json"/>
  </request>
  <response>
    <representation mediaType="application/xml"/>
    <representation mediaType="application/json"/>
  </response>
</method>
</resource>
I am using System.Net.HttpWebResponse to query the REST API. If anyone has a useful guide on how to use that to upload an xml file to OpenKM, I would really appreciate it.
 #42602  by jllort
 
About creating a mail, should be like created a document, but the uploaded document might be an eml or msg. When you uploading a file with these extensions automatically is identified by OpenKM as a mail and it parses it internally ( check it ).

Unfortunatelly we have not supported Mail in sd4 for .net community version, we have only supported in professional version ( in future we will add it ).

Yo might implement something like this:
Code: Select all
public Mail getMailProperties(String mailId) {
    try {
        Mail mail = new Mail();
        IRestRequest request = new RestRequest(uriHelper.MAIL_GET_PROPERTIES, Method.GET);
        request.RequestFormat = DataFormat.Xml;
        request.AddParameter("mailId", mailId, ParameterType.QueryString);
        IRestClient client = getClient(url, user, password);
        IRestResponse<Mail> response = client.Execute<Mail>(request);
        
        if (response.StatusCode == HttpStatusCode.OK) {
            //mail = response.Data;
            mail = xmlToObj.xmlToMail(response.Content.ToString());
        } else {
            //error ocured
            String error = response.Content;
            String className = error.Substring(0, error.IndexOf(":"));
            String errorMSG = "MailImpl (getMailProperties):" + error.Substring(error.IndexOf(":") + 1);
            
            switch (className) {
                case "AccessDeniedException": { throw new AccessDeniedException(errorMSG); }
                case "PathNotFoundException": { throw new PathNotFoundException(errorMSG); }
                case "RepositoryException": { throw new RepositoryException(errorMSG); }
                case "DatabaseException": { throw new DatabaseException(errorMSG); }
                case "UnknowException": { throw new UnknowException(errorMSG); }
                case "WebserviceException": { throw new WebserviceException(errorMSG); }
                default: { throw new UnknowException(errorMSG); }
            }
        }

        return mail;
    } catch (Exception e) {
        String errorMSG = "MailImpl (getMailProperties)\n" + e.Message + "\n\n" + e.ToString();
        throw new OKMRestException(errorMSG);
    }
}
 #42615  by ofstab
 
Thanks for your reply. Actually I am interested in information flowing the other way.

I seem to have tracked this down to the way that I am encoding the XML to attach to the POST body with the object I am trying to load into OpenKM through the API.

In your code below were does xmlToObj come from? How can I use that object and the xmlToMail method in my code?
Code: Select all
mail = xmlToObj.xmlToMail(response.Content.ToString());
Also I cannot seem to find the getClient method anywhere in RestSharp for this line:
Code: Select all
IRestClient client = getClient(url, user, password);
Otherwise I think that I have a Mail object that I want to load back into OpenKM. i just cannot figure out how to use RestSharp to load it in. My main connection setup is:
Code: Select all
Dim restsharp_client As New RestSharp.RestClient("http://[ip address of server]:8080/")
restsharp_client.Authenticator = New RestSharp.HttpBasicAuthenticator("[username]", "[password]")
Dim request = New RestSharp.RestRequest("OpenKM/services/rest/mail/create", HttpVerb.POST)
Dim temp_object As New com.openkm.sdk4csharp.bean.Mail
'...
'temp_object = mail object to load into openkm
'...
After that do I do this:
Code: Select all
Dim instance_of_XML_Serializer As New XmlSerializer(GetType(com.openkm.sdk4csharp.bean.Mail), _
	New XmlRootAttribute("mail"))
Dim stringWriter As New StringWriter()
instance_of_XML_Serializer.Serialize(stringWriter, temp_object)
Dim serializedXML As String = stringWriter.ToString()
request.AddParameter("application/xml; charset=utf-16", serializedXML, RestSharp.ParameterType.RequestBody)
request.RequestFormat = RestSharp.DataFormat.Xml
Or do I do this with mail as a bean.mail object:
Code: Select all
request.RequestFormat = RestSharp.DataFormat.Xml
request.AddBody(temp_object)
I either get this error:
Code: Select all
WARN  org.apache.cxf.jaxrs.utils.JAXRSUtils- No operation matching request path "/OpenKM/services/rest/mail/create" is found, Relative Path: /create, HTTP Method: POST, ContentType: text/xml, Accept: application/json,application/xml,text/json,text/x-json,text/javascript,text/xml,. Please enable FINE/TRACE log level for more details.
Or this one:
Code: Select all
[http-nio-0.0.0.0-8080-exec-3] WARN  org.apache.cxf.jaxrs.provider.AbstractJAXBProvider- java.lang.RuntimeException: Couldn't parse stream.
[...]
Code: Select all
Caused by: com.ctc.wstx.exc.WstxParsingException: Declared encoding 'UTF-16' uses 2 bytes per character; but physical encoding appeared to use 1; cannot decode
 at [row,col,system-id]: [1,39,"N/A"]
Thanks for any tips you might have.
 #42634  by jllort
 
You can not find these methods, because are methods made by us into the sdk for .net

For example the:
Code: Select all
public Mail xmlToMail(string xmlText) {
    //Create the XmlDocument.
    XmlDocument xmlDoc = new XmlDocument();
    XmlTextReader reader = new XmlTextReader(new StringReader(xmlText));
    xmlDoc.Load(reader);
    
    //Mail mail = new Mail();
    object obj = identifyXMLObjectType(xmlDoc);
    
    //Carry an array with all the properties of a mail
    PropertyInfo[] props = obj.GetType().GetProperties();
    
    foreach (PropertyInfo p in props) {
        string name = p.Name;
        string type = p.PropertyType.ToString();
        
        //Compare the data type of each property
        switch (type) {
            case "System.String[]": {
                var value = getArrayValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.String": {
                var value = getStringValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.Int32": {
                var value = getIntValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.Int64": {
                var value = getIntValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.DateTime": {
                var value = getDateTimeValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.Boolean": {
                var value = getBooleanValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.Collections.Generic.List`1[System.String]": {
                var value = getListStringValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.Collections.Generic.List`1[com.openkm.sdk4csharp.bean.Folder]": {
                var value = getListFolderValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            case "System.Collections.Generic.List`1[com.openkm.sdk4csharp.bean.Note]": {
                var value = getListNoteValue(xmlDoc, name);
                p.SetValue(obj, value, null);
                break;
            }
            
            default: break;
        }
    }
    
    return (Mail)obj;
}
Is not easy unmarshall xml from RESTFul and .net. You must invest some time investigating the rescsharp library etc..
 #43006  by ofstab
 
I have now spent the time looking into this and have got it working!

For anyone else that may be following being, here is how to use the rest api from within vb.net.

I am going to assume that you have the restsharp library, make sure to get the right version - see the openkm dev docs for details on this. If you are using the sdk4csharp library successfully then you will already have the right restsharp library.

Anyway, you will need an client interface for the restsharp library in vb.net. A really simple one looks like this:
Code: Select all
Imports System.IO
Imports System.Net
Imports System.Text

Public Enum HttpVerb
    [GET]
    POST
    PUT
    DELETE
End Enum

Public Class RestClient
        Public Property EndPoint() As String
            Get
                Return m_EndPoint
            End Get
            Set
                m_EndPoint = Value
            End Set
        End Property
        Private m_EndPoint As String
        Public Property Method() As HttpVerb
            Get
                Return m_Method
            End Get
            Set
                m_Method = Value
            End Set
        End Property
        Private m_Method As HttpVerb
        Public Property ContentType() As String
            Get
                Return m_ContentType
            End Get
            Set
                m_ContentType = Value
            End Set
        End Property
        Private m_ContentType As String
        Public Property PostData() As String
            Get
                Return m_PostData
            End Get
            Set
                m_PostData = Value
            End Set
        End Property
    Private m_PostData As String
    Public Property Accept() As String
        Get
            Return m_Accept
        End Get
        Set
            m_Accept = Value
        End Set
    End Property
    Private m_Accept As String

    Public Sub New()
            EndPoint = ""
            Method = HttpVerb.[GET]
            ContentType = "text/xml"
        PostData = ""
        Accept = ""

    End Sub
        Public Sub New(endpoint__1 As String)
            EndPoint = endpoint__1
            Method = HttpVerb.[GET]
            ContentType = "text/xml"
        PostData = ""
        Accept = ""
    End Sub
    Public Sub New(endpoint__1 As String, method__2 As HttpVerb)
        EndPoint = endpoint__1
        Method = method__2
        ContentType = "text/xml"
        PostData = ""
        Accept = ""
    End Sub

    Public Sub New(endpoint__1 As String, method__2 As HttpVerb, contenttype__3 As String)
        EndPoint = endpoint__1
        Method = method__2
        ContentType = contenttype__3
        PostData = ""
        Accept = ""
    End Sub

    Public Sub New(endpoint__1 As String, method__2 As HttpVerb, contenttype__3 As String, postData__4 As String)
        EndPoint = endpoint__1
        Method = method__2
        ContentType = contenttype__3
        PostData = postData__4
        Accept = ""
    End Sub


    Public Function MakeRequest() As String
        Return MakeRequest("")
    End Function


    Public Function MakeRequest(parameters As String) As String
        Dim request = DirectCast(WebRequest.Create(EndPoint & parameters), HttpWebRequest)

        request.Method = Method.ToString()
        request.ContentLength = 0
        request.ContentType = ContentType
        request.Accept = Accept

        If Not String.IsNullOrEmpty(PostData) AndAlso Method = HttpVerb.POST Then
            Dim encoding__1 = New UTF8Encoding()
            Dim bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData)
            request.ContentLength = bytes.Length

            Using writeStream = request.GetRequestStream()
                writeStream.Write(bytes, 0, bytes.Length)
            End Using
        End If


        Using response = DirectCast(request.GetResponse(), HttpWebResponse)
                Dim responseValue = String.Empty

                If response.StatusCode <> HttpStatusCode.OK Then
                    Dim message = [String].Format("Request failed. Received HTTP {0}", response.StatusCode)
                    Throw New ApplicationException(message)
                End If

                ' grab the response
                Using responseStream = response.GetResponseStream()
                    If responseStream IsNot Nothing Then
                        Using reader = New StreamReader(responseStream)
                            responseValue = reader.ReadToEnd()
                        End Using
                    End If
                End Using

                Return responseValue
            End Using
        End Function

    Public Function MakeRequest(parameters As String, credentials As ICredentials) As String
        Dim request = DirectCast(WebRequest.Create(EndPoint & parameters), HttpWebRequest)

        request.Method = Method.ToString()
        request.Credentials = credentials
        request.ContentLength = 0
        request.ContentType = ContentType
        request.Accept = Accept


        If Not String.IsNullOrEmpty(PostData) AndAlso Method = HttpVerb.POST Then
            Dim encoding__1 = New UTF8Encoding()
            Dim bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData)
            request.ContentLength = bytes.Length

            Using writeStream = request.GetRequestStream()
                writeStream.Write(bytes, 0, bytes.Length)
            End Using
        End If

        Using response = DirectCast(request.GetResponse(), HttpWebResponse)
            Dim responseValue = String.Empty

            If response.StatusCode <> HttpStatusCode.OK Then
                Dim message = [String].Format("Request failed. Received HTTP {0}", response.StatusCode)
                Throw New ApplicationException(message)
            End If

            ' grab the response
            Using responseStream = response.GetResponseStream()
                If responseStream IsNot Nothing Then
                    Using reader = New StreamReader(responseStream)
                        responseValue = reader.ReadToEnd()
                    End Using
                End If
            End Using

            Return responseValue
        End Using
    End Function

End Class
Now - onto the exciting OpenKM stuff. First create your client:
Code: Select all
        Dim client = New RestClient()
        client.EndPoint = "http://[ip address of your server]:8080/OpenKM/services/rest/"
        client.Method = [This is the HttpVerb Enum from above, so either HttpVerb.POST or HttpVerb.GET etc]
        client.Accept = "application/xml"
        client.ContentType = "text/xml"
The .Accept can also be application/json, but I am still working with 6.3.1, and it is broken there.

Right - now to build the command. To pull an email FROM OpenKM you would set client.Method to HttpVerb.GET, and the command would be:
Code: Select all
dim query_result as string = client.MakeRequest("mail/getProperties" & "?mailId=" & [uuid of the email to be pulled], New System.Net.NetworkCredential([username], [password]))
The query_result string should now be an xml file. You need to deserialize that from xml to a vb.net object. The Object has to match an OpenKM mail object. This was one of my mistakes in the thread above. I deserialized to a com.openkm.sdk4csharp.bean.Mail obejct when the xml file was a serialized com.openkm.bean.Mail object. This did not work. So you need some code for the latter in vb.net. it is as follows:
Code: Select all
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ws.openkm.com")>
Partial Public Class mail
    Inherits node

    Private attachmentsField() As document

    Private bccField() As String

    Private ccField() As String

    Private contentField As String

    Private fromField As String

    Private mimeTypeField As String

    Private receivedDateField As Date

    Private receivedDateFieldSpecified As Boolean

    Private replyField() As String

    Private sentDateField As Date

    Private sentDateFieldSpecified As Boolean

    Private sizeField As Long

    Private subjectField As String

    Private toField() As String

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("attachments", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property attachments() As document()
        Get
            Return Me.attachmentsField
        End Get
        Set
            Me.attachmentsField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("bcc", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property bcc() As String()
        Get
            Return Me.bccField
        End Get
        Set
            Me.bccField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("cc", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property cc() As String()
        Get
            Return Me.ccField
        End Get
        Set
            Me.ccField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property content() As String
        Get
            Return Me.contentField
        End Get
        Set
            Me.contentField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property from() As String
        Get
            Return Me.fromField
        End Get
        Set
            Me.fromField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property mimeType() As String
        Get
            Return Me.mimeTypeField
        End Get
        Set
            Me.mimeTypeField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property receivedDate() As Date
        Get
            Return Me.receivedDateField
        End Get
        Set
            Me.receivedDateField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlIgnoreAttribute()>
    Public Property receivedDateSpecified() As Boolean
        Get
            Return Me.receivedDateFieldSpecified
        End Get
        Set
            Me.receivedDateFieldSpecified = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("reply", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property reply() As String()
        Get
            Return Me.replyField
        End Get
        Set
            Me.replyField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property sentDate() As Date
        Get
            Return Me.sentDateField
        End Get
        Set
            Me.sentDateField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlIgnoreAttribute()>
    Public Property sentDateSpecified() As Boolean
        Get
            Return Me.sentDateFieldSpecified
        End Get
        Set
            Me.sentDateFieldSpecified = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property size() As Long
        Get
            Return Me.sizeField
        End Get
        Set
            Me.sizeField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property subject() As String
        Get
            Return Me.subjectField
        End Get
        Set
            Me.subjectField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("to", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property [to]() As String()
        Get
            Return Me.toField
        End Get
        Set
            Me.toField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ws.openkm.com")>
Partial Public Class document
    Inherits node

    Private actualVersionField As Version

    Private checkedOutField As Boolean

    Private convertibleToPdfField As Boolean

    Private convertibleToSwfField As Boolean

    Private descriptionField As String

    Private languageField As String

    Private lastModifiedField As Date

    Private lastModifiedFieldSpecified As Boolean

    Private lockInfoField As lockInfo

    Private lockedField As Boolean

    Private mimeTypeField As String

    Private titleField As String

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property actualVersion() As Version
        Get
            Return Me.actualVersionField
        End Get
        Set
            Me.actualVersionField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property checkedOut() As Boolean
        Get
            Return Me.checkedOutField
        End Get
        Set
            Me.checkedOutField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property convertibleToPdf() As Boolean
        Get
            Return Me.convertibleToPdfField
        End Get
        Set
            Me.convertibleToPdfField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property convertibleToSwf() As Boolean
        Get
            Return Me.convertibleToSwfField
        End Get
        Set
            Me.convertibleToSwfField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property description() As String
        Get
            Return Me.descriptionField
        End Get
        Set
            Me.descriptionField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property language() As String
        Get
            Return Me.languageField
        End Get
        Set
            Me.languageField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property lastModified() As Date
        Get
            Return Me.lastModifiedField
        End Get
        Set
            Me.lastModifiedField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlIgnoreAttribute()>
    Public Property lastModifiedSpecified() As Boolean
        Get
            Return Me.lastModifiedFieldSpecified
        End Get
        Set
            Me.lastModifiedFieldSpecified = Value
        End Set
    End Property

    '''<remarks/>
    Public Property lockInfo() As lockInfo
        Get
            Return Me.lockInfoField
        End Get
        Set
            Me.lockInfoField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property locked() As Boolean
        Get
            Return Me.lockedField
        End Get
        Set
            Me.lockedField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property mimeType() As String
        Get
            Return Me.mimeTypeField
        End Get
        Set
            Me.mimeTypeField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property title() As String
        Get
            Return Me.titleField
        End Get
        Set
            Me.titleField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ws.openkm.com")>
Partial Public Class lockInfo

    Private nodePathField As String

    Private ownerField As String

    Private tokenField As String

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property nodePath() As String
        Get
            Return Me.nodePathField
        End Get
        Set
            Me.nodePathField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property owner() As String
        Get
            Return Me.ownerField
        End Get
        Set
            Me.ownerField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property token() As String
        Get
            Return Me.tokenField
        End Get
        Set
            Me.tokenField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ws.openkm.com")>
Partial Public Class note

    Private authorField As String

    Private dateField As Date

    Private dateFieldSpecified As Boolean

    Private pathField As String

    Private textField As String

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property author() As String
        Get
            Return Me.authorField
        End Get
        Set
            Me.authorField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property [date]() As Date
        Get
            Return Me.dateField
        End Get
        Set
            Me.dateField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlIgnoreAttribute()>
    Public Property dateSpecified() As Boolean
        Get
            Return Me.dateFieldSpecified
        End Get
        Set
            Me.dateFieldSpecified = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property path() As String
        Get
            Return Me.pathField
        End Get
        Set
            Me.pathField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property text() As String
        Get
            Return Me.textField
        End Get
        Set
            Me.textField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.Xml.Serialization.XmlIncludeAttribute(GetType(document)),
 System.Xml.Serialization.XmlIncludeAttribute(GetType(mail)),
 System.Xml.Serialization.XmlIncludeAttribute(GetType(folder)),
 System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ws.openkm.com")>
Partial Public Class node

    Private authorField As String

    Private categoriesField() As folder

    Private createdField As Date

    Private createdFieldSpecified As Boolean

    Private keywordsField() As String

    Private notesField() As note

    Private pathField As String

    Private permissionsField As Integer

    Private subscribedField As Boolean

    Private subscriptorsField() As String

    Private uuidField As String

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property author() As String
        Get
            Return Me.authorField
        End Get
        Set
            Me.authorField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("categories", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property categories() As folder()
        Get
            Return Me.categoriesField
        End Get
        Set
            Me.categoriesField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property created() As Date
        Get
            Return Me.createdField
        End Get
        Set
            Me.createdField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlIgnoreAttribute()>
    Public Property createdSpecified() As Boolean
        Get
            Return Me.createdFieldSpecified
        End Get
        Set
            Me.createdFieldSpecified = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("keywords", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property keywords() As String()
        Get
            Return Me.keywordsField
        End Get
        Set
            Me.keywordsField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("notes", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property notes() As note()
        Get
            Return Me.notesField
        End Get
        Set
            Me.notesField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property path() As String
        Get
            Return Me.pathField
        End Get
        Set
            Me.pathField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property permissions() As Integer
        Get
            Return Me.permissionsField
        End Get
        Set
            Me.permissionsField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property subscribed() As Boolean
        Get
            Return Me.subscribedField
        End Get
        Set
            Me.subscribedField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute("subscriptors", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=True)>
    Public Property subscriptors() As String()
        Get
            Return Me.subscriptorsField
        End Get
        Set
            Me.subscriptorsField = Value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property uuid() As String
        Get
            Return Me.uuidField
        End Get
        Set
            Me.uuidField = Value
        End Set
    End Property
End Class

'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ws.openkm.com")>
Partial Public Class folder
    Inherits node

    Private hasChildrenField As Boolean

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>
    Public Property hasChildren() As Boolean
        Get
            Return Me.hasChildrenField
        End Get
        Set
            Me.hasChildrenField = Value
        End Set
    End Property
End Class
That's very lengthy because it is a bundle of objects which the email object relies on, like nodes, folders, documents and so on.

Anyway, now you can deserialize your result string to this object as follows:
Code: Select all
                    Dim temp_object As New mail 
                    Dim instance_of_XML_Serializer As New XmlSerializer(GetType(mail))

                    Try
                        Using reader As TextReader = New StringReader(query_result)
                            temp_object = DirectCast(instance_of_XML_Serializer.Deserialize(reader), mail)
                        End Using
                    Catch exception As SerializationException
                        'Report on failure.
                    End Try
If you are starting with a mail object called temp_object and want to get it INTO OpenKM, you do this:
Code: Select all
                    Dim restsharp_client As New RestSharp.RestClient("http://[ip address of server]:8080/")
                    restsharp_client.Authenticator = New RestSharp.HttpBasicAuthenticator([username], [password])
                    Dim restsharp_request = New RestSharp.RestRequest("OpenKM/services/rest/mail/create", HttpVerb.POST)
                    restsharp_request.RequestFormat = RestSharp.DataFormat.Xml
                    restsharp_request.XmlSerializer = New RestSharp.Serializers.DotNetXmlSerializer()
                    restsharp_request.AddBody(temp_object)
                    Dim response_report As IRestResponse = restsharp_client.Execute(restsharp_request)
I had a several problems with this - see above. I solved them by using the XML Serializer that was built into RestSharp. I also had a couple of NullPointerException problems, which I traced in the code to the email I was using not have any cc of bcc email recipients. Those properties had been left null, which OpenKM did not like. I solved that by doing this:
Code: Select all
                    temp_object.cc = {""}
                    temp_object.bcc = {""}
Obviously that is crude and you would want an if temp_object.cc = nothing , then .... test instead.

This is now working, although attachments are still to be properly handled. One thing, though. The system does not report it as a success. I get a 500 error code, and this line in the log:
Code: Select all
2017-01-13 16:34:29,366 [http-nio-0.0.0.0-8080-exec-10] WARN  org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor- No message body writer has been found for response class Mail.
The email IS there, so it has worked, and I presume this error just means that for some XML related reason it cannot properly report that to the IRestResponse object I created for the task.

Hope someone finds this useful.
 #43068  by ofstab
 
ofstab wrote:
Code: Select all
2017-01-13 16:34:29,366 [http-nio-0.0.0.0-8080-exec-10] WARN  org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor- No message body writer has been found for response class Mail.
The email IS there, so it has worked, and I presume this error just means that for some XML related reason it cannot properly report that to the IRestResponse object I created for the task.
OK, I have solved this last problem as well. I think it is related to the problem that 6.3.1 has with JSON/REST issues. If you add a header to the the restsharp request telling it you just want an XML response, then it works fine. It also now means that you do not need the custom restclient that I was using, and everything can go through restsharp. My new code for reading emails from openkm is (using some of jllort's code above):
Code: Select all
    Public Shared Function getMailProperties(mailId As String) As mail
        Dim mail_to_return As mail
        Try
            Dim restsharp_client As New RestSharp.RestClient("http://[ipaddress of server]:8080/OpenKM/")
            restsharp_client.Authenticator = New RestSharp.HttpBasicAuthenticator([username], [password])
            Dim restsharp_request = New RestSharp.RestRequest("services/rest/mail/getProperties" & "?mailId=" & mailId, HttpVerb.GET)
            restsharp_request.AddHeader("Accept", "application/xml")
            Dim response_received As IRestResponse = restsharp_client.Execute(restsharp_request)

            If response_received.StatusCode = HttpStatusCode.OK Then
                mail_to_return = deserialize_mail(response_received.Content.ToString)
            Else
                'error ocured
                Dim [error] As [String] = response_received.Content
                Dim errorMSG As [String] = "MailImpl (getMailProperties):" + [error].Substring([error].IndexOf(":") + 1)
                ThisAddIn.log_writer("ERROR: " & "Creating Email: " & errorMSG & " - Could not use rest API to load properly from OpenKM.")
                mail_to_return = New mail
            End If

        Catch
            ThisAddIn.log_writer("ERROR: " & "Getting Email: " & mailId & " - Could not use rest API to load properly from OpenKM.")
            mail_to_return = New mail
        End Try

        Return mail_to_return
    End Function
My code for loading emails into openkm is:
Code: Select all
    Public Shared Function createMail(mail_object_to_create As mail) As mail
        Dim mail_to_return As mail
        Try
            Dim restsharp_client As New RestSharp.RestClient("http://[ipaddress of server]:8080/OpenKM/")
            restsharp_client.Authenticator = New RestSharp.HttpBasicAuthenticator([username], [password])
            Dim restsharp_request = New RestSharp.RestRequest("services/rest/mail/create", HttpVerb.POST)
            restsharp_request.RequestFormat = RestSharp.DataFormat.Xml
            restsharp_request.AddHeader("Accept", "application/xml")
            restsharp_request.XmlSerializer = New RestSharp.Serializers.DotNetXmlSerializer()
            restsharp_request.AddBody(mail_object_to_create) 
            Dim response_received As IRestResponse = restsharp_client.Execute(restsharp_request)
            If response_received.StatusCode = HttpStatusCode.OK Then
                mail_to_return = deserialize_mail(response_received.Content.ToString)
            Else
                'error ocured
                Dim [error] As [String] = response_received.Content
                Dim errorMSG As [String] = "MailImpl (createMail):" + [error].Substring([error].IndexOf(":") + 1)
                ThisAddIn.log_writer("ERROR: " & "Creating Email: " & errorMSG & " - Could not use rest API to load properly to OpenKM.")
                mail_to_return = New mail
            End If

        Catch
            ThisAddIn.log_writer("ERROR: " & "Saving Email: " & mail_object_to_create.subject & " - Could not use rest API to load properly to OpenKM.")
            mail_to_return = New mail
        End Try

        Return mail_to_return
    End Function
The common deserialize_mail method is:
Code: Select all
    Private Shared Function deserialize_mail(mail_in_string_format As String) As mail
        Dim mail_to_return As mail
        Dim instance_of_XML_Serializer As New XmlSerializer(GetType(mail))
        Try
            Using reader As TextReader = New StringReader(mail_in_string_format)
                mail_to_return = DirectCast(instance_of_XML_Serializer.Deserialize(reader), mail)
            End Using
            If mail_to_return.cc Is Nothing Then
                mail_to_return.cc = {""}
            End If
            If mail_to_return.bcc Is Nothing Then
                mail_to_return.bcc = {""}
            End If
        Catch exception As SerializationException
            ThisAddIn.log_writer("Error: " & "Could not deserialize properly.")
            mail_to_return = New mail
        End Try
        Return mail_to_return
    End Function
Again, hope this helps someone.
 #43083  by jllort
 
Thanks for sharing it, sure will be useful for other user. After next OpenKM community release we will working in a new SDK version with new methods what actually are not present in the actual community.

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.