2

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?

2 Answers 2

1

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.

Sync instructions

Sign up to request clarification or add additional context in comments.

2 Comments

Would this still work if I clear out the files in my directory? I would ideally want to just upload the files and then clear out my directory when needed without removing what is housed in SharePoint.
You can mark the folder to unsync then clear them out. If you do not unmark (sync) the folder, then when you delete files they will delete from the Sharepoint Library. It is a true sync. On the bright side you when you sync the library just moves pointers to your client machine, so you are not wasting valuable space on your harddrive unless you request the library to download all files.
0

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.'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.