13

I am trying to find any way possible to get a SharePoint list in Python. I was able to connect to SharePoint and get the XML data using Rest API via this video: https://www.youtube.com/watch?v=dvFbVPDQYyk... but not sure how to get the list data into python. The ultimate goal will be to get the SharePoint data and import into SSMS daily.

Here is what I have so far..

import requests
from requests_ntlm import HttpNtlmAuth
url='URL would go here'
username='username would go here'
password='password would go here'
r=requests.get(url, auth=HttpNtlmAuth(username,password),verify=False)

I believe these would be the next steps. I really only need help getting the data from SharePoint in Excel/CSV format preferably and should be fine from there. But any recommendations would be helpful..

#PARSE XML VIA REST API
#PRINT INTO DATAFRAME AND CONVERT INTO CSV
#IMPORT INTO SQL SERVER
#EMAIL RESULTS
John
  • 141
  • 1
  • 2
  • 6

4 Answers4

9
from shareplum import Site
from requests_ntlm import HttpNtlmAuth

server_url = "https://sharepoint.xxx.com/"
site_url = server_url + "sites/org/"

auth = HttpNtlmAuth('xxx\\user', 'pwd')
site = Site(site_url, auth=auth, verify_ssl=False)
sp_list = site.List('list name in my share point')
data = sp_list.GetListItems('All Items', rowlimit=200)
David Castro
  • 193
  • 1
  • 6
  • 6
    Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – Trenton McKinney Sep 19 '19 at 17:04
  • @shzyincu domain my friend (I was using a corporate network) – David Castro Jul 09 '21 at 18:40
  • @TrentonMcKinney thanks i will consider in the future to add a description or title at least – David Castro Jul 09 '21 at 18:41
  • if possible, change "https://sharepoint.xxx.com/" to "https://xxx.sharepoint.com/". Regards from México.... @DavidCastro – JWBG Mar 16 '23 at 15:09
6

this can be done using SharePlum and Pandas following is the working code snippet

import pandas as pd     # importing pandas to write SharePoint list in excel or csv
from shareplum import Site 
from requests_ntlm import HttpNtlmAuth

cred = HttpNtlmAuth(#userid_here, #password_here)
site = Site('#sharePoint_url_here', auth=cred)

sp_list = site.List('#SharePoint_list name here') # this creates SharePlum object
data = sp_list.GetListItems('All Items') # this will retrieve all items from list

# this creates pandas data frame you can perform any operation you like do within 
# pandas capabilities     

data_df = pd.DataFrame(data[0:])
data_df.to_excel("data.xlsx")

please rate if this helps. Thank you in advance!

  • 1
    how to get the List version history by python? – Andrew Jul 02 '20 at 12:38
  • I get "certificate to verify failed: unable to get local issuer certificate" :( please some one help, sharepoint2013 is where I need to connect – shzyincu Jul 08 '21 at 13:49
3

I know this doesn't directly answer your question (and you probably have an answer by now) but I would give the SharePlum library a try. It should hopefully simplify the process you have for interacting with SharePoint.

Also, I am not sure if you have a requirement to export the data into a csv but, you can connect directly to SQL Server and insert your data more directly.

I would have just added this into the comments but don't have a high enough reputation yet.

0

I can help with most of these issues

import requests
import xml.etree.ElementTree as ET
import csv

from requests_ntlm import HttpNtlmAuth

response = requests.get("your_url", auth=HttpNtlmAuth('xxxx\\username','password'))
tree =  ET.ElementTree(ET.fromstring(response.content))
tree.write('file_name_xml.xml')
root = tree.getroot()

#Create csv file 
csv_file = open('file_name_csv.csv', 'w', newline = '', encoding='ansi')
csvwriter = csv.writer(csv_file)

col_names = ['Col_1', 'Col_2', 'Col_3', 'Col_n']
csvwriter.writerow(col_names)

field_tag = ['dado_1', 'dado_2', 'dado_3', 'dado_n']

#schema XML microsoft 
ns0 = "http://www.w3.org/2005/Atom"
ns1 = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
ns2 = "http://schemas.microsoft.com/ado/2007/08/dataservices"


for member in root:
    if member.tag == '{' + ns0 + '}entry':
        for element in member:
            if element.tag == '{' + ns0 + '}content':
                data_line = []

                for field in element[0]:            
                    for count in range(0, len(field_tag)):
                        if field.tag == '{' + ns2 + '}' + field_tag[count]:
                            data_line.append(field.text)


                csvwriter.writerow(data_line)

csv_file.close()
  • Hi Morgana. Is there a way to pull Column names and field tags from the XML data? The table I'm pulling from, it would be a bit tedious to write into a list manually. Perhaps you could also clarify what field_tags are? Any details add on your logic you're using would be helpful. – Hofbr Oct 07 '20 at 14:45