Get SharePoint List with Python
Asked Answered
A

4

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
Aramaic answered 20/3, 2017 at 12:0 Comment(0)
P
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)
Prayer answered 19/9, 2019 at 16:42 Comment(4)
Here are some guidelines for How do I write a good answer?. This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From review.Amp
@shzyincu domain my friend (I was using a corporate network)Prayer
@TrentonMcKinney thanks i will consider in the future to add a description or title at leastPrayer
if possible, change "sharepoint.xxx.com" to "xxx.sharepoint.com". Regards from México.... @DavidCastroProphylactic
J
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!

Juryrigged answered 27/1, 2020 at 19:50 Comment(2)
how to get the List version history by python?Chiffon
I get "certificate to verify failed: unable to get local issuer certificate" :( please some one help, sharepoint2013 is where I need to connectGithens
M
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.

Mercier answered 21/3, 2019 at 13:48 Comment(0)
H
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()
Hamlett answered 23/1, 2020 at 19:27 Comment(1)
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.Klarrisa

© 2022 - 2024 — McMap. All rights reserved.