how to use the python-sharepoint library to download files from list?
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I am using this library https://github.com/ox-it/python-sharepoint to connect to a SharePoint 2013 list with list attachments.

I can authenticate, access the list fields, including the full URL to the file I want, and it seems this library does have is_file() and open() methods - however, I am not able to download the file and save it to disk.

from sharepoint import SharePointSite, basic_auth_opener 

opener = basic_auth_opener(server_url, "domain/username", "password")
site = SharePointSite(server_url, opener)

sp_list = site.lists['ListName']
for row in sp_list.rows:
   print row.id, row.Title, row.Author['name'], row.Created, row.EncodedAbsUrl
   # download file here 
awarded to dekkard

Crowdsource coding tasks.

2 Solutions


Try this:

f = row.open()
local_file = open(row.LinkFilename, "w")
local_file.write(f.read())
local_file.close()
Winning solution

Basically you call these methods on the list row (which is of type SharePointListRow).
The open() method is actually the method of urllib2's opener, which you usually use like so:

import urllib2
opener = urllib2.build_opener()
response = opener.open('http://www.example.com/')
print ('READ CONTENTS:', response.read())
print ('URL          :', response.geturl())
# ....

So you should be able to use it like this (I don't have any Sharepoint site to check this though):

from sharepoint import SharePointSite, basic_auth_opener

opener = basic_auth_opener(server_url, "domain/username", "password")
site = SharePointSite(server_url, opener)

sp_list = site.lists['ListName']
for row in sp_list.rows():
    print row.id, row.Title, row.Author['name'], row.Created, row.EncodedAbsUrl
    # download file here

    print("LinkFilename: ", row.LinkFilename)

    if row.is_file():
        try:
            print( "Request url: ", opener.relative(urllib.quote(sp_list.meta['Title']) + '/' + urllib.quote(row.LinkFilename.encode('utf-8'))) )

            response = row.open()
            file_data = response.read()

            # TODO: process the file data, e.g. write to disk

        except urllib2.HTTPError, error:
            print("ERROR: ", error.read())
thanks for the detailed reply! however when i tried this i received the error - 'http error 400: bad request' full error output is here: http://pastebin.com/ag1ayjaU do you have any other suggestions?
weaverk almost 7 years ago
HTTP Error 400: Bad Request is a generic HTTP error code and doesn't indicate a specific problem. It'd be best to check the logs on your Sharepoint server for any clues. If that's not possible, check the correctness of the parameters you pass to basic_auth_opener(). I updated the code with a debug printing of the request URL (I hope I got all the properties right) and wrapped the request in a try-catch to just print an error if it occurs.
dekkard almost 7 years ago
I got sidetracked on some other things and never got this working, but i do appreciate the help!
weaverk almost 7 years ago
View Timeline