This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

mfws and python

Former Member
Former Member
I'm currently starting a project to interact with mfiles using python.  I've managed to get authentication tokens with this code:

#testbed for requests (http api) and mfiles REST interface
import requests
import json
#try to retrieve authentication tokens
authentication = 'localhost/.../authenticationtokens.aspx'
data = json.dumps({"Username":"yourname", "Password":"yourpassword"})
h= requests.post(authentication, data = data)
print(h.reason,'reason')
print('#')
print(h._content)

Anyone interested in working on this jointly?

New discovery.  The data line must be changed to :

data = json.dumps({"Username":"yourname", "Password":"yourpassword",
                  "VaultGuid":"{1E5F2435-AFB8-4A7D-92D3-45B04D1268D9}"})

or the token won't work for individual vault access.

  • Former Member
    Former Member
    Adding this code to the above returns a string rep of the root window:

    baseurl ='http://localhost/m-files/REST'
    headers = {"content-type":"aplication/json",'X-Authentication':h.text[10:-2]}
    foobar = requests.get(''.join((baseurl,"/views/items.aspx")), headers = headers)
    print(foobar.text)
  • Former Member
    Former Member
    The project is coming along nicely. The following makes a quick graphical window to test the quick search function:

    #testbed for requests (http api) and mfiles REST interface
    import requests #third party. all others std library
    import json
    from tkinter import *
    from tkinter.ttk import *

    baseurl = 'http://localhost/m-files/REST'

    def authenticate():
    authentication = ''.join((baseurl, '/server/authenticationtokens.aspx'))
    data = json.dumps({
    "Username": "yourname", "Password": "yourpassword",
    "VaultGuid": "{1E5F2435-AFB8-4A7D-92D3-45B04D1268D9}"
    })
    return json.loads(requests.post(authentication, data = data).text)['Value']

    def search():
    #reads search term from entry widget, joins it to the url
    #queries mfiles using authentication tokens and url
    #returns the query results
    searchterm = Ent.get()
    url = ''.join((baseurl, "/Objects.aspx?q=", searchterm))
    headers = {"content-type":"aplication/json", 'X-Authentication':authtoken}
    condition_results(json.loads(requests.get(url, headers=headers).text))

    def condition_results(results):
    #displays one line for each item, the content of the line being the
    #value of key 'EscapedTitleWithID'. Could be any of: ['AccessedByMeUtc',
    #'CheckedOutAt', 'Files', 'EscapedTitleWithID', 'ObjVer', 'AccessedByMe',
    #'CheckedOutAtDisplayValue', 'VisibleAfterOperation', 'LastModified',
    #'ObjectCheckedOutToThisUser', 'SingleFile', 'LastModifiedDisplayValue',
    #'ObjectVersionFlags', 'ObjectCheckedOut', 'Title', 'LastAccessedByMe',
    #'CheckedOutAtUtc', 'PathInIDView', 'DisplayID', 'LastModifiedUtc',
    #'Class', 'ObjectGUID', 'CheckedOutTo', 'CreatedUtc', 'Score',
    #'CreatedDisplayValue', 'ThisVersionLatestToThisUser',Created']
    print('****************************')
    for item in results['Items']:
    print(item['EscapedTitleWithID'])
    print('****************************')

    if __name__ == '__main__':
    authtoken = authenticate()
    trial = Tk()
    Label(trial, text='test for vault lookup').grid()
    Button(trial, text='Go', command=search).grid()
    Ent = Entry(trial)
    Ent.grid(column=1, row=1)
    trial.mainloop()
  • Former Member
    Former Member
    New update on the project. The following code will check out an object. I am assuming that you have received an authentication token as in the previous posts.

    url = ''.join((baseurl,'/objects/184/54/checkedout.aspx?_method=PUT'))
    somedata = json.dumps({'Value':2})
    headers = {"content-type":"application/json", 'X-Authentication':authtoken}
    query = requests.post(url, headers=headers, data = somedata)

    Changing the Value to 0 checks the object back in. If you check it out in python you can't check it in from the m-files client without forcing a check in.


    The big problem I'm having is trying to modify a property. Let's say I am trying to use the resource '/objects/(type)/(objectid)/(version)/properties/(id). In my particular installation, '/objects/184/54/1235'. I can use GET and not have a problem, but when I try to put I receiver server codes 500 or 405 depending on the code that I am trying.

    If I was going to modify this string property to say, "mfiles is great", what should the code look like in javascript? I can't find a good example of modifying a single property in the documentation.




  • Former Member
    Former Member
    This one was a bit tricky to me. Finally solved my own problem(after trying every syntax I could imagine) and maanged to write to /objects/(type)/(objectid)/(version)/properties/(id):

    url = ''.join((baseurl,'/objects/184/54/-1/properties/1235')) #used this one to get
    posturl = ''.join((url,'.aspx?_method=PUT'))
    data = json.dumps({'PropertyDef':'1235', 'TypedValue':{'DataType':'13', 'Value': 'Some Text'}})
    newquery = requests.post(posturl, headers = headers,data = data)

    When accessing another resource such as /objects/(type)/(objectid)/(version)/title, all the mfws expects is json.dumps({"Value":somevalue}).
  • glowworm,
    I wanted to see how your project was coming along... If I would have seen this earlier I would have worked on this with you.
  • Former Member
    Former Member
    Hi all,
    just wanted to ask if there is any news on python supported functionality? Beside the REST api that is.
  • We currently have nothing to announce regarding extended compatibility with python.

    If you are running python on Windows then you may be able to use our COM API, if it's installed and available (developer.m-files.com/.../), but the typical approach from python would be to interact with our REST API (developer.m-files.com/.../).

    Regards,

    Craig.
  • Former Member
    Former Member
    Hello,

    Sorry for responding to an old thread, but I thought that the next person looking for Python support for M-Files might end up here as well. I have created library for easier access to the M-Files API from python. Available at pypi.org/.../. Maybe someone will find it useful.
  • Former Member
    Former Member

    Hello,

    Sorry for responding to an old thread, but I thought that the next person looking for Python support for M-Files might end up here as well. I have created library for easier access to the M-Files API from python. Available at pypi.org/.../. Maybe someone will find it useful.


    Hello,

    thank you for this!

    Is it possible to provide some simple example for using this library for testing purposes?

    import M_Files_Python

    class MFilesClient(api='localhost/.../', user=None, password=None, vault='{XXXXXX-XXXX-XXXXX-XXXXXX-XXXXXXXXXXXX}'):
    classes()
  • Former Member
    Former Member


    Hello,

    Sorry for responding to an old thread, but I thought that the next person looking for Python support for M-Files might end up here as well. I have created library for easier access to the M-Files API from python. Available at pypi.org/.../. Maybe someone will find it useful.


    Hello,

    thank you for this!

    Is it possible to provide some simple example for using this library for testing purposes?

    import M_Files_Python

    class MFilesClient(api='localhost/.../', user=None, password=None, vault='{XXXXXX-XXXX-XXXXX-XXXXXX-XXXXXXXXXXXX}'):
    classes()

    [/quote]

    Sure thing, updated the repo with examples :)

    github.com/.../examples