Tuesday, June 5, 2018

get list of files in a sharepoint directory using python

Leave a Comment

I have a url for sharepoint directory(intranet) and need an api to return list of files in that directory given the url. how can I do that using python?

2 Answers

Answers 1

I have a url for sharepoint directory

Assuming you asking about a library, you can use SharePoint's REST API and make a web service call to:

https://yourServer/sites/yourSite/_api/web/lists/getbytitle('Documents')/items?$select=Title 

This will return a list of documents at: https://yourServer/sites/yourSite/Documents

See: https://msdn.microsoft.com/en-us/library/office/dn531433.aspx

You will of course need the appropriate permissions / credentials to access that library.

Answers 2

You need to do 2 things here.

  1. Get a list of files (which can be directories or simple files) in the directory of your interest.
  2. Loop over each item in this list of files and check if the item is a file or a directory. For each directory do the same as step 1 and 2.

You can find more documentation at https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-files-attached-to-list-items-by-using-rest

def getFilesList(directoryName):     ...     return filesList  # This will tell you if the item is a file or a directory. def isDirectory(item):     ...     return true/false 

Hope this helps.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment