Exemplo n.º 1
0
    def __init__(self,
                 host,
                 port=0,
                 auth=None,
                 username=None,
                 password=None,
                 protocol='http',
                 path=None):
        if not port:
            port = 443 if protocol == 'https' else 80
        self.path = path or ''
        if not self.path.endswith('/'):
            self.path = '%s/' % self.path

        # oneDrive: need to split /site/ and path
        # in our config site is /personal/UF_OCX_msf_geneva_msf_org/
        # path is /Documents/Tests/
        self.baseurl = '{0}://{1}:{2}{3}/'.format(
            protocol, host, port, '/'.join(self.path.split('/')[0:3]))
        ctx_auth = AuthenticationContext(self.baseurl)

        #if len(self.path.split('/')) < 5:
        #    self.path = '%sDocuments/' % self.path
        if ctx_auth.acquire_token_for_user(username, cgi.escape(password)):
            self.request = ClientRequest(ctx_auth)
            self.request.context = ClientContext(self.baseurl, ctx_auth)

            if not ctx_auth.provider.FedAuth or not ctx_auth.provider.rtFa:
                raise ConnectionFailed(ctx_auth.get_last_error())
        else:
            raise ConnectionFailed(ctx_auth.get_last_error())
Exemplo n.º 2
0
    def login(self):
        ctx_auth = AuthenticationContext(self.baseurl)

        if ctx_auth.acquire_token_for_user(self.username, cgi.escape(self.password)):
            self.request = ClientRequest(ctx_auth)
            self.request.context = ClientContext(self.baseurl, ctx_auth)

            if not ctx_auth.provider.FedAuth or not ctx_auth.provider.rtFa:
                raise ConnectionFailed(ctx_auth.get_last_error())

        else:
            raise ConnectionFailed(ctx_auth.get_last_error())
Exemplo n.º 3
0
    def handle(self):
        email, password = self.email, self.password
        authentication_context = AuthenticationContext(settings.SHAREPOINT_URL)
        authentication_context.acquire_token_for_user(email, password)

        if not authentication_context.get_last_error():
            request = ClientRequest(authentication_context)
            os.makedirs(settings.BIOS_ROOT, exist_ok=True)
            for bio in self._get_bios_list(
                authentication_context, settings.BIOS_URL
            ):
                if not 'template' in bio.lower():
                    options = RequestOptions(
                        '{0}mexico/Shared Documents/Resumes/{1}'.format(
                            settings.SHAREPOINT_URL, bio
                        )
                    )
                    options.set_header('Accept', 'application/json')
                    options.set_header('Content-Type', 'application/json')
                    data = request.execute_query_direct(options)
                    docx = open('{0}/{1}'.format(
                        settings.BIOS_ROOT,
                        bio.replace(
                            "a%C3%A1", "á"
                        ).replace(
                            "e%CC%81", "é"
                        ).replace(
                            "i%C3%AD", "í"
                        ).replace(
                            "o%C3%B3", "ó"
                        ).replace(
                            "u%C3%BA", "ú"
                        ).replace(
                            "%C3%91", "Ñ"
                        ).replace(
                            "A%C3%81", "Á"
                        ).replace(
                            "E%C3%89", "É"
                        ).replace(
                            "I%C3%8D", "Í"
                        ).replace(
                            "O%C3%93", "Ó"
                        ).replace(
                            "U%C3%9A", "Ú"
                        )
                    ), "wb")
                    docx.write(data.content)
                    docx.close()
        else:
            self.error = authentication_context.get_last_error()
Exemplo n.º 4
0
def FetchMSPData(auth_creds, endpoint, entrylist):

    print("Fetching data for: ", endpoint)
    MSP_ROOT_URL = auth_creds[0]
    MSP_USERNAME = auth_creds[1]
    MSP_PASS = auth_creds[2]
    ctx_auth = AuthenticationContext(MSP_ROOT_URL)
    if ctx_auth.acquire_token_for_user(MSP_USERNAME, MSP_PASS):
        request = ClientRequest(ctx_auth)

        dataList = GetAllData(
            auth_creds, request, "/sites/pwa/_api/projectdata/{0}".format(endpoint)
        )

        d = {}

        if len(entrylist) == 1 and entrylist[0] == "all":
            ParseAndAddAllToDict(dataList, d)
        else:
            for entry in entrylist:
                ParseAndAddToDict(dataList, entry, d)

        data_df = pd.DataFrame(d)
        print("Success data fetching for: ", endpoint)
        return data_df

    else:
        print(ctx_auth.get_last_error())
Exemplo n.º 5
0
    def execute(self, container, args):
        l = self.logger

        try:

            try:
                ctx_auth = AuthenticationContext(url=args.sharepointurl)
                if ctx_auth.acquire_token_for_user(
                        username=args.sharepointusername,
                        password=args.sharepointpass):
                    ctx = ClientContext(args.sharepointurl, ctx_auth)
                    filename = args.output
                    path = args.sharepointfpath
                    checkin_type = args.uplshr_checkintype
                    return upload_data_to_sharepoint(path, filename, ctx,
                                                     checkin_type, l)
                else:
                    print(ctx_auth.get_last_error())
                    return

            except Exception as e:
                l.error("Exception happened boards search " + str(e))

        except Exception as e:
            l.error("Exception happened during connection establishment " +
                    str(e))
Exemplo n.º 6
0
def upload_one_file(base_url, folder_url, file_name, user_name, pwd):
    ctx_auth = AuthenticationContext(url=base_url)
    if not ctx_auth.acquire_token_for_user(username=user_name, password=pwd):
        print(ctx_auth.get_last_error())
        exit()

    upload_binary_file(file_name, base_url, folder_url, ctx_auth)

    checkin_file(file_name, base_url, folder_url, ctx_auth)
Exemplo n.º 7
0
def main():
    ctx_auth = AuthenticationContext(webUrl)
    try:
        if ctx_auth.acquire_token_for_user(username, password):
            runRestQuery(ctx_auth, "web")
        else:
            print(ctx_auth.get_last_error())

        showEndpoints(uniquEndpoints)

    except Exception as e:
        print(e)
Exemplo n.º 8
0
def authSharpoint() -> ClientContext:
    '''
    Authenticates with sharepoint
    '''
    ctx_auth = AuthenticationContext(env.sp_url)
    if ctx_auth.acquire_token_for_user(env.sp_username, env.sp_password):
        ctx = ClientContext(env.sp_url, ctx_auth)
        web = ctx.web
        ctx.load(web)
        ctx.execute_query()
        return(ctx)
    else:
        print(ctx_auth.get_last_error())
        return(False)
Exemplo n.º 9
0
def openShiftPlan(creds):
    ctx_auth = AuthenticationContext(siteURL)
    if ctx_auth.acquire_token_for_user(creds[0], creds[1]):
        ctx = ClientContext(siteURL, ctx_auth)
        web = ctx.web
        ctx.load(web)
        ctx.execute_query()
        print("Web title: {0}".format(web.properties['Title']))
    else:
        print(ctx_auth.get_last_error())

    response = File.open_binary(ctx, relativeURL)
    #print(ctx.service_root_url())
    #save data to BytesIO stream
    bytes_file_obj = io.BytesIO()
    bytes_file_obj.write(response.content)
    bytes_file_obj.seek(0)  #set file object to start

    #read file into pandas dataframe
    return pd.read_excel(bytes_file_obj, sheet_name='Daily - Infra')
Exemplo n.º 10
0
	def main(self, fcreate, fname, furl):
		file_to_create = fcreate
		file_name = fname
	#### --- Create Sharepoint Authorized Connection ---- ####
		ctx_auth = AuthenticationContext(url)
		if ctx_auth.acquire_token_for_user(username, password):
			ctx= ClientContext(url,ctx_auth)
			ctx.request_form_digest()
			request = ClientRequest(ctx)
			full_url = ("{0}/_api/web/GetFolderByServerRelativeUrl('{1}')/Files/add(url='{2}', overwrite=true)".format(url, furl, file_to_create))
			options = RequestOptions(full_url)
			options.set_header('Accept', 'application/json; odata=nometadata')
			options.set_header('Content-Type', 'application/octet-stream')
			options.set_header('Content-Length', str(os.path.getsize(file_name)))
			options.set_header('X-RequestDigest', ctx.contextWebInformation.form_digest_value)
			options.method = HttpMethod.Post
	### --- Upload File to Sharepoint Site ---- ####
			with open(file_name, 'rb') as outfile:
				ctx.authenticate_request(options)
				data = requests.post(url=full_url, data=outfile, headers=options.headers, auth=options.auth)
	### --- Verify succuess of upload ---- ###
				if data.status_code == 200:
					print("success")
				else:
					print(data.status_code)
					return data.json()['error']
			
		else:
			print(ctx_auth.get_last_error())

			
### --- Script Start ---- ####			
#fileOpenBackLogCreate()
#fileBillingsCreate()
#msdShipDate()

#read_file()
Exemplo n.º 11
0
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

tenant_url = "https://company.sharepoint.com"
site_url = "https://company.sharepoint.com/sites/sbdev"

ctx_auth = AuthenticationContext(tenant_url)

if ctx_auth.acquire_token_for_user("*****@*****.**",
                                   "mypassword"):
    ctx = ClientContext(site_url, ctx_auth)
    lists = ctx.web.lists
    ctx.load(lists)
    ctx.execute_query()

    for l in lists:
        print(l.properties["Title"])

else:
    print(ctx_auth.get_last_error())
Exemplo n.º 12
0
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions
from settings import settings
import json

if __name__ == '__main__':
    context_auth = AuthenticationContext(url=settings['url'])
    if context_auth.acquire_token_for_user(username=settings['username'],
                                           password=settings['password']):
        """Read Web client object"""
        request = ClientRequest(settings['url'], context_auth)
        options = RequestOptions("{0}/_api/web/".format(settings['url']))
        options.set_header('Accept', 'application/json')
        options.set_header('Content-Type', 'application/json')
        data = request.execute_query_direct(options)
        s = json.loads(data.content)
        web_title = s['Title']
        print "Web title: " + web_title

    else:
        print context_auth.get_last_error()
class SharePoint(object):
    def __init__(self, url, username, password, log):
        """
        SharePoint object initialization.
        :param url: SharePoint destination URL (See DEFAULT_SP_URL above for example)
        :param username: Username to access SharePoint (See DEFAULT_SP_USERNAME above for example)
        :param log: where to send log messages
        :type log: logging.Logger
        """
        self._url = url
        self._username = username
        self._password = password
        self.log = log

        #
        # Authenticate for this run
        #
        log.debug(
            "SharePoint: Acquiring AuthenticationContext {} for {}".format(
                url, username))
        self._ctx_auth = AuthenticationContext(url=url)

        #
        # Looks like, at the time of this writing, the Office365-REST_Python-Client
        # library exception handling leaves a lot to be desired as their internal
        # method calls don't test the results of lower-level method calls in order to
        # percolate up problems they encounter.
        #
        # NOTE: Errors will actually go to the log file but not standard output for some
        #       unknown reason
        #
        log.debug("SharePoint: Acquiring token from {} for {}".format(
            url, username))
        self._ctx_auth.acquire_token_for_user(username=username,
                                              password=password)

        #
        # Kludgy test to see if a problem occurred
        # Admittedly this test will break if a different authentication provider is used
        #
        if self._ctx_auth.provider.token is None:
            _msg = "SharePoint: Unable to get SharePoint authorization. Error: {}".format(
                self._ctx_auth.get_last_error())
            log.critical(_msg)
            raise ValueError(_msg)

        log.debug(
            "SharePoint: Authentication token obtained from {} for {}".format(
                url, username))

        #
        # Get cookies
        #
        log.debug("SharePoint: Getting cookies from {} for {}".format(
            url, username))
        self._ctx = ClientContext(self.url, self._ctx_auth)
        log.debug("SharePoint: Obtained ClientContext from {} for {}".format(
            url, username))

    @property
    def url(self):
        return self._url

    @property
    def username(self):
        return self._username

    @property
    def password(self):
        return self._password

    def list_files(self, path):
        """
        Get a list of the filenames in passed folder.

        :param path: Path to the folder from which to get the list of files
        :type path: string
        :return: List of filenames found in the folder
        """
        self.log.debug(
            "list_files: Get list of files for path: {}".format(path))

        #
        # Build and execute the folder query
        #
        folder = self._ctx.web.get_folder_by_server_relative_url(path)
        self._ctx.load(folder)
        self._ctx.execute_query()

        #
        # Build and execute the files query from the folder query results
        #
        files = folder.files
        self._ctx.load(files)
        self._ctx.execute_query()

        ##print ("files ", files)

        #
        # Build list of files found in the path
        #
        #_file_list = []
        _file_dict = {}
        for _f in files:
            self.log.debug("list_files: Filename: {}".format(
                _f.properties["Name"]))
            _file_dict[_f.properties["Name"]] = _f.properties["TimeCreated"]
        #
        # Return the list of files
        #
        ##print ("_file_dict = ", _file_dict)
        #return _file_list
        return _file_dict

    def check_archived_folder(self, src_path):
        """
        After downloading files, they're moved to an Archived folder on SharePoint.
        Check to make sure the Archiv folders exists or create them if they do not.
        The Archived folder path is comprised of two parts, "Archived" and "Month Year"
        folder as in Archived/Oct 2018 and is placed within the download folder path like:
        .../Shared Documents/NPT/Sep 2018           Download source path
        .../Shared Documents/NPT/Archived/Sep 2018  Archived path
        :param src_path: Download source base directory (ie. /sites/SageDS/Shared Documents/NPT/Oct 2018)
        """
        self.log.debug("check_archived_folders: src_path:{}".format(src_path))

        #
        # Create some useful variables from download source folder
        # Incoming src_path variable should be like: /sites/SageDS/Shared Documents/NPT/Sep 2018
        #
        _base_path = os.path.dirname(
            src_path
        )  # Grab the head of src_path (ie. /sites/SageDS/Shared Documents/NPT)
        _date_folder = os.path.basename(
            src_path)  # Grab the base of src_path (ie. Sep 2018)

        self.log.debug(
            "check_archived_folders: base_path:{} date_folder:{}".format(
                _base_path, _date_folder))

        #
        # Get the list of folders in our base folder
        #
        _folder = self._ctx.web.get_folder_by_server_relative_url(_base_path)
        _folders = _folder.folders
        self._ctx.load(_folders)
        self._ctx.execute_query()

        #
        # Look for Archived folder in base folder
        #
        _archived_found = False
        for _f in _folders:
            self.log.debug(
                "check_archived_folders: is 'Archived' = '{}'".format(
                    _f.properties["Name"]))
            if "Archived" == _f.properties["Name"]:
                self.log.debug(
                    "check_archived_folders: 'Archived' folder found, no need to create"
                )
                _archived_found = True

        #
        # Did we find an Archived folder
        #
        if not _archived_found:
            #
            # Nope, need to add it
            #
            self.log.debug(
                "check_archived_folders: Creating 'Archived' folder")
            _folders.add("Archived")
            self._ctx.execute_query()
            self.log.debug("check_archived_folders: Created 'Archived' folder")

        #
        # Now check for a date folder within the Archived folder
        #
        _base_path += "/Archived"
        _folder = self._ctx.web.get_folder_by_server_relative_url(_base_path)
        _folders = _folder.folders
        self._ctx.load(_folders)
        self._ctx.execute_query()

        #
        # Look for the date folder obtained from original path within the Archived folder
        #
        _date_folder_found = False
        for _f in _folders:
            self.log.debug("check_archived_folders: is '{}' = '{}'".format(
                _date_folder, _f.properties["Name"]))
            if _date_folder == _f.properties["Name"]:
                self.log.debug(
                    "check_archived_folders: '{}' folder found, no need to create"
                    .format(_date_folder))
                _date_folder_found = True

        #
        # Did we find a date folder within the Archived folder
        #
        if not _date_folder_found:
            #
            # Nope, need to create one
            #
            self.log.debug(
                "check_archived_folders: Creating '{}' folder".format(
                    _date_folder))
            _folders.add(_date_folder)
            self._ctx.execute_query()
            self.log.debug(
                "check_archived_folders: '{}' folder created ".format(
                    _date_folder))

    def download_files(self, src, dest, file_pattern, period, worksheet_names,
                       table_names, exec_id, script_path):
        #print ("In download_files")
        """
        Download all the files in a src SharePoint directory to local dest directory.

        :param src: Source SharePoint folder from which to download files
        :type src: string
        :param dest: Destination local directory into which to download files
        :type dest: string
        :return: Number of files downloaded
        """
        #
        # Create some useful variables from download source folder
        # Incoming src variable should be like: /sites/SageDS/Shared Documents/NPT/Oct 2018
        #
        _base_path = os.path.dirname(
            src
        )  # Grab the head of dir (ie. /sites/SageDS/Shared Documents/NPT)
        #print("_base_path :", _base_path)
        _date_folder = os.path.basename(
            src)  # Grab the base of dir (ie. Oct 2018)
        #print("_date_folder :", _date_folder)
        _move_to_path = "{}/Archived/{}/".format(_base_path, _date_folder)
        ##print("_move_to_path :", _move_to_path)

        self.log.debug(
            "download_files: Starting src:{} base:{} date:{}".format(
                src, _base_path, _date_folder))

        #
        # Make sure Archived folder exists in SharePoint before moving on
        #
        self.check_archived_folder(src)

        #
        # Get the list of filenames in the SharePoint folder
        #
        _files = self.list_files(src)

        ##print ("_files :", _files)

        #
        # Keep a count of the number of files downloaded
        #
        _num_downloaded = 0

        #
        # Make sure there's something to download
        #
        if not len(_files):
            self.log.info(
                "download_files: No files found to download from {} of {}".
                format(src, self.url))
            return _num_downloaded

        self.log.info(
            "files_found: Number of files {} found from {} of {}".format(
                len(_files), src, self.url))

        #
        # Walk the list of files, downloading each one into destination directory
        #

        for _f, _fct in _files.items():
            #print ("In _files Loop")
            _spn = "{}/{}".format(src, _f)  # Source pathname
            ##print ('_spn =', _spn)
            _dpn = os.path.join(dest, _f)  # Destination pathname
            ##print ('_dpn =', _dpn)

            _upper_file_name = _f.upper()
            ##print ('_upper_file_name =', _upper_file_name)
            # self.log.info('_upper_file_name = ' + _upper_file_name)
            _upper_file_pattern = file_pattern.upper()
            # self.log.info('_upper_file_pattern = ' + _upper_file_pattern)
            ##print ('_upper_file_pattern =', _upper_file_pattern)

            file_pattern_match = re.findall(_upper_file_pattern,
                                            _upper_file_name)
            # self.log.info('file_pattern_match = ' + str(file_pattern_match))
            if file_pattern_match:
                ##print ("Found files with the pattern {}".format(file_pattern))
                self.log.debug("download_files: Downloading {} to {}".format(
                    _spn, _dpn))
                # self.log.info("download_files: Downloading {} to {}".format(_spn, _dpn))
                # Insert Log
                proc_name = _f
                statement_type = "Download File -> " + _f
                table_name = 'null'
                # success_failure = "Download Complete"
                # self.log.info('exec_id = ' + str(exec_id) + ' proc_name = ' + proc_name + ' statement_type = '
                #               + statement_type)
                qrs.insert_log(exec_id, proc_name, statement_type, table_name,
                               'null', "Started")

                #
                # Download the file
                #
                try:
                    with open(_dpn, 'wb') as _ofd:
                        _response = File.open_binary(self._ctx, _spn)
                        _ofd.write(_response.content)
                    # success_failure = "Download Complete"
                    qrs.insert_log(exec_id, proc_name, statement_type,
                                   table_name, 'null', "Completed")
                except Exception as e:
                    _msg = "{} error encountered creating destination file {}".format(
                        e, _dpn)
                    log.critical(_msg)
                    raise ValueError(_msg)
                dct = datetime.fromtimestamp(os.path.getmtime(_dpn))
                ##print ("down load time : " ,dct)
                ##print ("type of _fct = ", type(_fct))
                #_fp.append(dct)
                ##print ("_fp = ", _fp)
                #valid_files_dict[_f] = _fp
                ##print ("valid_files_dict = ", valid_files_dict)

                sqlcmd = "INSERT INTO [T_STG_FILES_DOWNLOADED]" \
                         + "([FILE_NAME_PATTERN],[FILE_NAME],[DOWNLOADED_TIME],[PERIOD],[PROJECT],[FILE_CREATION_TIME]) " \
                         + "VALUES(" \
                         + "'" + file_pattern +  "'," \
                         +  "'" + _f +  "'," \
                         + "CAST(left('" + str(dct) + "',23)" + " AS datetime),'" \
                         + period +  "'," \
                         +"'" + qrs.Project_Name + "'," \
                         + "CAST ('" +_fct + "' AS datetime))"
                ##print('sqlcmd = ', sqlcmd)
                qrs.dml_cursor(sqlcmd)
                LOG.info(
                    "Uploaded to data base strarts:Work sheet {} Table: {} ".
                    format(worksheet_names, table_names))
                os_cmd = script_path + 'Opex_dynamic_upload_to_DB.py ' + str(
                    exec_id
                ) + ' "' + _dpn + '" "' + table_names + '" "' + worksheet_names + '"'
                print('Initiating System Command -> \n' + os_cmd)
                qrs.run_os_cmd(os_cmd)
                LOG.info(
                    "All Work sheet {} Uploaded to Table: {} was successful".
                    format(worksheet_names, table_names))
                #print ("file = " ,  "'" + dest + "\\" +_f + "'")
                #print ("worksheet_names = "  ,  "'" + worksheet_names+ "'")
                #print ("table_names = "  ,  "'" + table_names + "'")

                _num_downloaded += 1

                #
                # File downloaded, move it to Archived folder
                #
                _to_path = _move_to_path + _f  #commented by Mukesh not archieve for testing

                self.log.debug("download_files: Moving {} to {}".format(
                    _spn, _to_path))
                #
                # Moving the file
                #
                try:
                    print("in moving block")
                    LOG.info("\n Sharepoint:File {} has been archived in {} ".
                             format(self._ctx, _spn, _to_path))
                    _resp = File.move(
                        self._ctx, _spn, _to_path
                    )  #commented by Mukesh not archieve for testing
                    ##print ("in moving block after MOve")
                except Exception as e:
                    _msg = "{} error encountered moving {} to  file {}".format(
                        e, _spn, _to_path)
                    log.critical(_msg)
                    raise ValueError(_msg)
                #_resp = File.move(self._ctx, _spn, _to_path)

                #
                # Was move successful
                #
                if _resp.status_code != 200:
                    self.log.error(
                        "download_files: Error: {} moving {} to {}".format(
                            _resp.status_code, _spn, _to_path))
                else:
                    self.log.debug("download_files: Moved {} to {}".format(
                        _spn, _to_path))

        #
        # Return count of files downloaded
        #
        return _num_downloaded
Exemplo n.º 14
0
    ctx = parent_web.context
    webs = parent_web.webs
    ctx.load(webs)
    ctx.execute_query()
    result = result + list(webs)
    for web in webs:
        result = get_all_webs(web, result)
    return result


if __name__ == '__main__':
    ctxAuth = AuthenticationContext(url=settings['url'])
    if ctxAuth.acquire_token_for_app(
            client_id=settings['client_credentials']['client_id'],
            client_secret=settings['client_credentials']['client_secret']):
        ctx = ClientContext(settings['url'], ctxAuth)
        # web = load_web(ctx)
        # web = create_web(ctx)
        # update_web(web)
        # delete_web(web)
        #root_web = ctx.web
        #ctx.load(root_web)
        #ctx.execute_query()
        #print_webs_recursively(root_web)
        all_webs = get_all_webs(ctx.web)
        for w in all_webs:
            print(w.properties['Url'])
        #print_webs_recursively(all_webs)
    else:
        print(ctxAuth.get_last_error())
Exemplo n.º 15
0
    def post_entities(entities: list):

        ctx_auth = AuthenticationContext(URL)

        ctx_auth.acquire_token_for_user(USERNAME, PASSWORD)
        if ctx_auth.provider.token:
            ctx = ClientContext(URL, ctx_auth)
        else:
            error = ctx_auth.get_last_error()
            logging.error(error)
            raise Exception(error)

        for _, entity in enumerate(entities):
            if entity['_deleted'] and not PROCESS_DELETED:
                logging.debug(
                    f"entity {entity['_id']} marked as deleted and will not be processed"
                )
                continue

            list_object = ctx.web.lists.get_by_title(entity[LIST_NAME])

            try:
                list_item_name = entity.get(LIST_ITEM_NAME)
                if list_item_name is None:
                    item_properties_metadata = {}
                else:
                    item_properties_metadata = {
                        '__metadata': {
                            'type': list_item_name
                        }
                    }
                keys_to_send = entity['Keys']
                values_to_send = {
                    key: str(entity[key])
                    for key in keys_to_send
                }
                item_properties = {
                    **item_properties_metadata,
                    **values_to_send
                }

                existing_item = None
                if entity.get('ID'):
                    try:
                        existing_item = list_object.get_item_by_id(
                            entity.get('ID'))
                        ctx.load(existing_item)
                        ctx.execute_query()
                    except Exception as ie:
                        logging.warning(
                            "Item lookup by ID resulted in an exception from Office 365 {}"
                            .format(ie))
                        if (
                                hasattr(ie, 'code') and ie.code
                                == "-2147024809, System.ArgumentException"
                        ) or (hasattr(ie, 'message') and ie.message ==
                              "Item does not exist. It may have been deleted by another user."
                              ):
                            existing_item = None
                        else:
                            raise Exception from ie

                if not existing_item:
                    logging.info("Creating new item")
                    list_object.add_item(item_properties)
                    ctx.execute_query()
                else:
                    logging.info("Existing item found")
                    if entity.get('SHOULD_DELETE') is not None and bool(
                            entity.get('SHOULD_DELETE')):
                        response = delete_list_item(ctx, entity[LIST_NAME],
                                                    entity.get('ID'))
                    else:
                        response = update_list_item(ctx, entity[LIST_NAME],
                                                    entity.get('ID'),
                                                    values_to_send)
                    response.raise_for_status()

            except Exception as e:
                error_message = f"An exception occurred during processing of an entity: {e} ({json.dumps(entity)}"
                logging.error(error_message)
                raise Exception(error_message) from e
    groups = ctx.web.site_groups
    groupProperties = {'__metadata': {'type': 'SP.Group'}, 'Title': groupName}
    group = groups.add(groupProperties)
    ctx.execute_query()
    print "Group : {0} has been created".format(group.properties["Title"])

    "Retrieve group users"
    users = group.users
    ctx.load(users)
    ctx.execute_query()
    for user in users:
        print "User : {0}".format(user.properties["Title"])

    "Remove a group"
    groups.remove_by_login_name(groupName)
    ctx.execute_query()
    print "Group : {0} has been deleted".format(groupName)


if __name__ == '__main__':
    ctxAuth = AuthenticationContext(url=settings['url'])
    if ctxAuth.acquire_token_for_user(username=settings['username'],
                                      password=settings['password']):
        ctx = ClientContext(settings['url'], ctxAuth)

        read_groups(ctx)
        #crudGroup(ctx)

    else:
        print ctxAuth.get_last_error()
Exemplo n.º 17
0
    def post_entities(entities: list):
        ctx_auth = None
        token_acquired = False
        ctx = None
        values_to_send = None
        item_properties = None
        list_object = None

        for index, entity in enumerate(entities):
            if ctx_auth is None:
                ctx_auth = AuthenticationContext(URL)

            if token_acquired is False:
                ctx_auth.acquire_token_for_user(USERNAME, PASSWORD)
                if ctx_auth.provider.token is not None:
                    ctx = ClientContext(URL, ctx_auth)
                    list_object = ctx.web.lists.get_by_title(entity[LIST_NAME])

                    token_acquired = True

            if token_acquired:
                try:
                    list_item_name = entity.get(LIST_ITEM_NAME)
                    if list_item_name is None:
                        item_properties_metadata = {}
                    else:
                        item_properties_metadata = {
                            '__metadata': {
                                'type': list_item_name
                            }
                        }
                    keys_to_send = entity['Keys']
                    values_to_send = {
                        key: str(entity[key])
                        for key in keys_to_send
                    }
                    item_properties = {
                        **item_properties_metadata,
                        **values_to_send
                    }

                    existing_item = None
                    if entity.get('ID'):
                        try:
                            existing_item = list_object.get_item_by_id(
                                entity.get('ID'))
                            ctx.load(existing_item)
                            ctx.execute_query()
                        except Exception as ie:
                            logging.warning(
                                "Item lookup by ID resulted in an exception from Office 365 {}"
                                .format(ie))
                            if (
                                    hasattr(ie, 'code') and ie.code
                                    == "-2147024809, System.ArgumentException"
                            ) or (hasattr(ie, 'message') and ie.message ==
                                  "Item does not exist. It may have been deleted by another user."
                                  ):
                                existing_item = None
                            else:
                                raise

                    if not existing_item:
                        logging.info("Creating new item")
                        new_item = list_object.add_item(item_properties)
                        ctx.execute_query()
                        entity['status'] = "OK: Sent to {}".format(
                            entity[LIST_NAME])
                        entity['sharepoint_item'] = new_item.properties
                    else:
                        logging.info("Existing item found")
                        update_result = update_list_item(
                            ctx, entity[LIST_NAME], entity.get('ID'),
                            values_to_send)
                        if update_result.status_code > 299:
                            raise Exception(update_result.text)
                        else:
                            entity['status'] = 'OK: updated successfully'

                except Exception as e:
                    logging.error("An exception has occurred: {}".format(e))
                    entity[
                        'status'] = "ERROR: An exception has occurred: {}".format(
                            e)
                    error_message = "An exception occurred during processing of current entity: {0} {1} ({2}".format(
                        e.errno, e.strerror, json.dumps(entity))
                    raise Exception(error_message)
            else:
                error = ctx_auth.get_last_error()
                logging.error(error)
                entity['status'] = "ERROR: {}".format(error)
                raise Exception(error)

        return True
import json
from settings import settings
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext

if __name__ == '__main__':

    context_auth = AuthenticationContext(url=settings['url'])
    if context_auth.acquire_token_for_user(
            username=settings['user_credentials']['username'],
            password=settings['user_credentials']['password']):
        """Read Web client object"""
        ctx = ClientContext(settings['url'], context_auth)
        options = RequestOptions("{0}/_api/web/".format(settings['url']))
        options.set_header('Accept', 'application/json')
        options.set_header('Content-Type', 'application/json')
        data = ctx.execute_request_direct(options)
        json = json.loads(data.content)
        web_title = json['Title']
        print("Web title: {0}".format(web_title))

    else:
        print(context_auth.get_last_error())
        print "Task '{0}' has been created".format(
            task_item.properties["Title"])


def generate_contacts(context):
    contacts_list = ctx.web.lists.get_by_title("Contacts")
    fake = Faker()
    for idx in range(0, 1):
        name = fake.name()
        contact_properties = {
            '__metadata': {
                'type': 'SP.Data.ContactsListItem'
            },
            'Title': name
        }
        contact_item = contacts_list.add_item(contact_properties)
        context.execute_query()
        print "Contact '{0}' has been created".format(
            contact_item.properties["Title"])


if __name__ == '__main__':
    ctx_auth = AuthenticationContext(url=settings['url'])
    if ctx_auth.acquire_token_for_user(username=settings['username'],
                                       password=settings['password']):
        ctx = ClientContext(settings['url'], ctx_auth)
        generate_tasks(ctx)
        #generate_contacts(ctx)
    else:
        print ctx_auth.get_last_error()