Reputation: 439
I am trying to upload all items from a directory on my computer into a SharePoint site. Is it possible to create a script that will perform this task?
Upvotes: 2
Views: 4499
Reputation: 1
Sure, it is possible you can upload a whole folder into Sharepoint by creating put requests. I did write the code below to upload a directory into Sharepoint server. You must provide user authentication, check the files in folder whether you will upload or not and make an input ('y' or 'n' no case sensitive) and the full path required to upload data on Sharepoint server. Server response outputs should be 201 or 200 - OK to correctly upload files.
import requests
import os
import sys
from requests_ntlm import HttpNtlmAuth
import glob
path = 'C:\Python27' #CHANGE THIS it is an upload path where the files are stored
files = next(os.walk(path))[2]
print "File array:"
print files
print "\n"
print 'Total number of files in', path, 'is', len(files)
print "\n"
print "Files listed in directory:"
print "\n"
i = 0
for i in range(0,len(files)):
print(path+"\\."+files[i])
i += 1
print "\n"
status = []
play = True
while play:
answer = raw_input('Do you want to continue to upload corresponding files? (Y)es or (N)o: \n').lower()
while True:
if answer == 'y':
print 'Script will start uploading... \n'
print 'Check if status codes are 200 (200 OK - The request has succeeded) '
print 'or 201 (201 CREATED - The request has been fulfilled and has resulted'
print 'in one or more new resources being created). If not, try again. \n'
for i in range(0, len(files)):
filename = files[i]
session = requests.Session()
session.auth = HttpNtlmAuth('SharepointDomain\\username','password', session) #CHANGE THIS
file = open(path + "\\" + filename, 'rb')
bytes = bytearray(file.read())
resp = requests.put('Full directory path including hostname where the files will be uploaded' + filename, data=bytes, auth=session.auth)
print "Status response for file #",i+1, "is", resp.status_code
status = 'ok'
break
elif answer == 'n':
play = False
break
else:
answer = raw_input('Incorrect input. Press \"Y" to continue or \"N" to leave": ').lower()
print 'Program will exit.'
Upvotes: 0
Reputation: 1031
This is probably not the site for this question.
BUT...if you are using Sharepoint on Office 365 you can sync the contents of a sharepoint library to your computer. Then any document you put in the folder gets uploaded to your sharepoint site.
Upvotes: 1