def setUpClass(cls): super(TestSharePointListDataService, cls).setUpClass() credential = ClientCredential( settings['client_credentials']['client_id'], settings['client_credentials']['client_secret']) cls.client = ListDataService.connect_with_credentials( settings['url'], credential)
def test1_connect_with_app_principal(self): credentials = ClientCredential( settings.get('client_credentials').get('client_id'), settings.get('client_credentials').get('client_secret')) ctx = ClientContext.connect_with_credentials(settings['url'], credentials) self.assertIsInstance(ctx.authentication_context.provider, ACSTokenProvider) self.assertIsInstance(ctx.authentication_context.provider.token, TokenResponse) self.assertTrue(ctx.authentication_context.provider.token.is_valid)
import os import tempfile from office365.runtime.auth.clientCredential import ClientCredential from office365.sharepoint.client_context import ClientContext from office365.sharepoint.listitems.caml import CamlQuery from settings import settings download_path = tempfile.mkdtemp() client_creds = ClientCredential( settings['client_credentials']['client_id'], settings['client_credentials']['client_secret']) ctx = ClientContext(settings['url']).with_credentials(client_creds) list_obj = ctx.web.lists.get_by_title("Tasks123") items = list_obj.get_items(CamlQuery.create_all_items_query()) ctx.execute_query() for item in items: if item.properties[ 'Attachments']: # 1. determine whether ListItem contains attachments # 2. Explicitly load attachments for ListItem attachment_files = item.attachmentFiles ctx.load(attachment_files) ctx.execute_query() # 3. Enumerate and save attachments for attachment_file in attachment_files: download_file_name = os.path.join( download_path, os.path.basename(attachment_file.properties["FileName"])) with open(download_file_name, 'wb') as fh:
import os import tempfile from office365.runtime.auth.clientCredential import ClientCredential from office365.sharepoint.client_context import ClientContext from settings import settings ctx = ClientContext.connect_with_credentials( settings.get('url') + "/sites/team", ClientCredential( settings.get('client_credentials').get('client_id'), settings.get('client_credentials').get('client_secret'))) file_url = '/sites/team/Shared Documents/big_buck_bunny.mp4' download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url)) with open(download_path, "wb") as local_file: source_file = ctx.web.get_file_by_server_relative_url(file_url) source_file.download(local_file) ctx.execute_query() print("[Ok] file has been downloaded: {0}".format(download_path))
def credentials(self): return ClientCredential(self.client_id, self.client_secret)