cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Load Data from Sharepoint Site to Delta table in Databricks

Aidonis
New Contributor III

Hi New to the community so sorry if my post lacks detail.

I am trying to create a connection between databricks and a sharepoint site to read excel files into a delta table

I can see there is a FiveTran partner connection that we can use to get sharepoint data into databricks but I wanted to ask the community if they know of any other ways of connecting sharepoint to databricks.

Thanks,

1 ACCEPTED SOLUTION

Accepted Solutions

Ajay-Pandey
Esteemed Contributor III

Hi @Aidan Heffernanโ€‹ you can use Sharepoint Rest API to connect with databricks

Please refer below code-

 from office365.sharepoint.client_context import ClientContext
 from office365.runtime.auth.client_credential import ClientCredential
    
 sharepoint_base_url = "https://site.sharepoint.com/sites/group/"
 READ_DIR = "/sites/Group/Shared Documents/Folder/Subfolder
    
 # setup sharepoint access
 client_credentials = ClientCredential(client_id, client_secret)
 ctx = ClientContext(sharepoint_base_url).with_credentials(client_credentials)"
    
 def download_sharepoint_file(file_url):
     temp_dir = tempfile.mkdtemp()
     download_path = os.path.join(temp_dir, os.path.basename(file_url))
     with open(download_path, "wb") as local_file:
         file = ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()
     return download_path
    

Ajay Kumar Pandey

View solution in original post

2 REPLIES 2

Ajay-Pandey
Esteemed Contributor III

Hi @Aidan Heffernanโ€‹ you can use Sharepoint Rest API to connect with databricks

Please refer below code-

 from office365.sharepoint.client_context import ClientContext
 from office365.runtime.auth.client_credential import ClientCredential
    
 sharepoint_base_url = "https://site.sharepoint.com/sites/group/"
 READ_DIR = "/sites/Group/Shared Documents/Folder/Subfolder
    
 # setup sharepoint access
 client_credentials = ClientCredential(client_id, client_secret)
 ctx = ClientContext(sharepoint_base_url).with_credentials(client_credentials)"
    
 def download_sharepoint_file(file_url):
     temp_dir = tempfile.mkdtemp()
     download_path = os.path.join(temp_dir, os.path.basename(file_url))
     with open(download_path, "wb") as local_file:
         file = ctx.web.get_file_by_server_relative_path(file_url).download(local_file).execute_query()
     return download_path
    

Ajay Kumar Pandey

Aidonis
New Contributor III

Thanks for this @Ajay Pandeyโ€‹  this worked a charm ๐Ÿ˜

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group