Upload and Delete file to Sharepoint with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what up everybody i am lou and today we're going to talk about how to upload documents to sharepoint so we're going to be uploading documents and deleting documents uh to sharepoint we're using python in this case um it's actually pretty simple but it's actually pretty simple i use it in one of my projects where we needed to um just from a high level not to go too much detail into it but ultimately we we generated pdf documents as as we generate the pdf documents we renamed the documents then what we end up doing was uploading the documents to sharepoint um so we kind of had a you know got everything streamlined and um yeah again it wasn't hard at all it's actually pretty pretty simple but i want to share with you also we are in the same position on how to upload documents to sharepoint this hopefully this video helps you out so um let's get started so before before i get to this i did mention we're using python all right so that's number one um the other thing we are using the share pump library to do this so again if you're following along make sure to install this library again it's the share plump and we'll go through some of these configurations um it's pretty straightforward but ultimately again you gotta authenticate which is one piece of it which kind of explains here so for this example we're using office 365. on-prem would be different of course um but that's not part of this example this example that i'm showing you is not for on-prem is for office 365. now there is a way to do it on-prem and it's actually easier i think and i could you know i could make a video you want me to show you how to do it on-prem but in this case again office 365. nevertheless go ahead and get this installed if you don't have it because you will need it and uh let's get started so the first thing i want to show y'all is um i have a config file now this is a copy of my config file i'm not going to show you my config file but i just want to kind of show you the layout of it um against a json file if you're not familiar with json i'm not going to go too detail in this video because that's not the purpose of this video just go ahead and google it it's nothing complex but ultimately just from a high level you have a key and you have a value so my case my key is sharepoint my object has additional key value pairs inside which i have in this case the user password the url site and the library so again those y'all familiar with sharepoint know that they're the sharepoint site and you got what's called sharepoint libraries so that's kind of we have to specify in here um some of the some of the items in here as you could tell um is standard like you are going to have you know the sharepoint site does point to the again sharepoint.com but ultimately it will be your your domain of your company you know that you're operating under if you don't um that's if you have a domain registered if you just have a office 365 account i believe it's something like i can't remember what it is it'll be whatever name you create at the time then it'll be like microsoft on that's what it's called like microsoft on dot something and then of course it'll be sharepoint.com but you know either way uh it will be that name then of course you will specify your site this will be your variable whatever your site is i'll show y'all again my site because i want to show you know just kind of um i want to show you everything from beginning to end um then of course it would be whatever document ultimately whatever file right whatever directory um whatever directory name you're going to upload it to that's that is what it's going to be um so again i'm going to show you a json file so let's get started what i'm going to do first is i'm going to create a a sharepoint file i'm also going to create while i'm at it is a project file all right so i got those two in place so let's get started with the sharepoint file [Music] um all right so what i'm going to end up doing here is let me close out of that let's see so i'm gonna i need to um i already got share plump you know installed on my um my virtual environment so i'm good there so i'm gonna just start bringing everything in share pump import site office 365 and the version all right so the other thing that i need to bring in is going to be oops json os um i need to bring this in because my config file the json file i need to be able to be able to read it so that's the reason why i bring that in i bring in my os because i want to specify the my root pass of where my project is going to be running in running from so instead of me having to specify instead of me having to specify um the full pass of this config file um because again my code is gonna run in the same folder where my config file is ultimately i'm gonna i'm i want to i want my root directory directory to be um dynamic right so i don't want to hardcode that in um so let's go ahead and get that root directory pass directory name um os pass absolute pass file all right so that should give me that the next thing is i'm going to now create a variable for my config um directory pass i'm going to call it config pass os my bad not os uh what am i doing i'm going to do a join i'm going to concatenate my root directory with my um config file name so in this case it would be root and then i'm gonna specify the name which would be config.i [Music] all right so that's that the next thing i'm going to do is i'm going to read config file so let's go ahead and read that file so now i'm going to specify again this variable because this is ultimately going to give me the root pass oh that's going to give me back the actual path would be c drive you know so no whatever the the full pass is right that's kind of the purpose behind that i'm gonna call this config file uh let's go ahead and kind of this config json.load and i'm gonna specify what am i loading and then i'm gonna just ultimately assign my um uh my object to my config object so in this case i want the sharepoint again coin going back if i go to the example i have a sharepoint and i have this object ultimately what i'm doing is i'm assigning this object to my config variable all right so now below what i'm going to end up doing is i'm going to define my my variable so i can kind of use throughout [Music] um soccer use throughout so now i'm going to define my variables so i could use throughout my project so in this case there are five main variables that i need that i ultimately that i extracted from the config file so if i go back username password url sign in library so i'm going to actually specify that over here so i'm going to call this username and it's going to be the config which is again this this variable over here and then i'm going to specify what my key is in this case it happens to be either and i'm going to do the same thing for password and i believe i called it a password yep that's what it's called password and then i'm going to call this sharepoint url uh this is called url sharepoint site and i call this site then the last one would be sharepoint um i'm gonna call this sharepoint um it's called a sharepoint library all right you know what let me give a different sharepoint doc so let me go ahead and specify that which is called dog um dog library which is what it's called all right so now that i got that specified uh next what i'm going to end up doing is i'm going to create a sharepoint class it just kind of makes it easier for me to um to call it in my other script so let's go ahead and call it sharepoint class and then inside this class i'm going to have different um functions so one of the functions that i'm going to have is going to be my off so i need to authenticate so that's going to be my my first one so in this one i'm going to have i need to retrieve my cookies so [Music] so i'm going to create an object for that this is part of the office 365 again it come if i go back on top i have an office 365. um you know what i make this a little bit bigger there it goes should be fine uh part of the share pump there is an office 365 uh class that's what i'm bringing in this requires one of the sharepoint site so again this kind of goes back to my sharepoint site i'm going to bring in that object that variable i mean so sharepoint site uh username and what else password yep there you go so now i have that set at the end there is because this is a class i have a method which is called get cookies which is what i'm kind of pretty much assigning into this variable the next one is going to be called site and again kind of going back i have a class called site on top in this in this site i'm specifying one of my site url so let me go ahead and specify the sharepoint site whatever the version so the version in my case would be i am using um office so there is a version that you could tell on down the list there's a 2016 19 i'm using the v365 because i'm using office 365. i believe that 2019 and 2016 may work still with office 365. i believe so in a way but you can test that out so the other one would be awesome cookies which would be my awesome cookies that i assigned that i have over here all right so let's see what else um that will be for my site the next thing would be i'm going to return this object back so anytime i call to authenticate i'm going to get my my cookie information i'm gonna get all of that gonna get returned back so this is the first piece right you need to authenticate that's number one uh the next thing that i'm going to do is i'm going to create a message for connect folder which is what i'm calling so the connect folder let me specify folder name so the the connect folder this method is going to be um anytime that i upload a document right to sharepoint or even delete a document i need to specify where i'm uploading the document to so so the way i'm going to construct it is [Music] as i specify where to upload to or delete from i'm also going to be authenticating the same time so that's where i'm going to add this connect folder is it's actually going to be authenticating the connecting to the right folder so that's kind of the purpose behind it so which means i want to bring i'm going to end up calling um i'm going to end up creating this object called all site which is ultimately is going to call this method up here which would authenticate me so this is again this is for me to get authenticated uh the next thing is i'm gonna create a share point um my directory pass and this is gonna be join so i'm going to end up joining my sharepoint doc again this is my my um my directory pass my in sharepoint they call libraries uh document libraries i guess so i'm on i'm i'm connecting to that url but at the same time i need to specify my folder name so i'm gonna have a since i'm bringing in a variable called folder name that's where that comes in so ultimately i'm concatenating that together right i have my my um library pass for sharepoint but i need to specify which folder that i want to connect to i'm just kind of you know joining those concatenating those two values together so that's that piece the next thing here would be i'm going to create my uh folder object so this would be uh self all site which is what i needed and then i'm gonna end up oops they're the folder um method and this is going to end up taking uh this full directory pass which would which will be sharepoint directory right which it has again it has my file name my folder name concatenated to it um in this case i'm going to be returning i want to return that folder object again so far we're not we're not connecting yet we're not uploading nothing yet but what i'm doing is i'm laying down the uh the message that are needed to kind of better streamline the process right so one of the things i would tell you as somebody who again when you're when you learn how to program your new by one point then you become a better programmer i'm a better programmer now than i was but there was some time in the past or anytime i used to write a piece of script i didn't i didn't use classes best case maybe had a function or two here and there but everything was all just pretty much procedural based and it was so messy man and so i mean like all over the place hard to understand anytime you go back a few months later it was like a big mess so what i what i have learned to do over time is try to separate everything out right put some thought into it separate it out through some message to do certain things especially things that repeat themselves there's no need to copy that code over and over again just had one have a piece of have a function a method to handle that that's kind of what i'm trying to do here so the next method that i'm going to create is going to be called upload file now this is the piece that we actually want to do we want to be able to upload to sharepoint so that's what we're going to do here so in this case i'm going to have uh so file is going to be um my actual directory path to the file that i want to upload that's what that's going to be which means this should be the full pass on where that so you know where to go to read that file then i'm going to have file name um and then i'm gonna have the folder name because i need to specify what folder and sharepoint i'm gonna upload it to um oops all right so the next thing is i'm going to end up calling my connect folder um oops i'm going to end up calling my connect folder [Music] function so in this case remember what the connect folder is doing is it's going to authenticate for us automatically then it's also going to provide back the object of where to upload the the file to so it's kind of doing a few things it's authenticating us so we don't need to authenticate it will do it for us as you can tell here it's calling it up over here then on top of that it's going to end up um giving us the object back on where to be able to upload like pretty much the the url uh for the api on where to upload the the document um so that's kind of the purpose for this connect folder probably should have called it something different but that's fine it is what it is uh so let me specify the um in this case what am i doing with folder name let me specify the folder name and that's it it's folder name only okay so now i have my folder object which is only going to be used inside this method so the next thing is going to be we want to read the file so with open and i'm going to specify the file in this case i'm uploading a um a pdf file so my mode is going to be rb uh read binary keep that in mind if you're uploading a text file your mode will be r it's going to read the text file but when you're uploading a binary file it's different the mode is rb so that's the reason why i'm specifying rb in this case so i'm going to call it pdf file product i mean again my case is pdf doesn't have to be pdf let me just call it file uh file object whatever that is right because it may not be a pdf you may be dealing with a text file i don't know so again just i'm going to leave it at that file object um so i'm going to call it file context and it's going to end up being file object dot read oops dot read so because again i want to read the data on the file in this case just happens to be binary so that's what i'm trying to do here the next thing that i'm going to end up doing is i'm going to end up um selecting the let's see what am i doing here all right so the next thing that i'm going to end up doing here is i'm going to call my folder the odd this object that i have over here right folder because you got to remember it's bringing it back from my authenticating my on authenticating to my site and then specifying the folder paths that i want to upload my file so that's what i'm doing here and then i'm calling the upload file again this upload file has nothing to do with this upload file we just happen to call it the same name but this upload file is actually coming it's a message that's part of the shareplump so just want to clear that out um then i'm going to specify the file context again the data that's in my file and then i need to specify a file name which again i'm passing in a file name up here on top so now that i have that in order what i'm going to do is i'm not going to be iterating through so just think about it right like when you're working on a project you may have oh i have these 100 files that i need to upload to sharepoint i may have this thousand of files and it uploaded sharepoint i may have a thousand files and each file is separated by a year right you may have it by 2019 2020 so they need to go in different folders at sharepoint well you're going to have to loop again create some sort of iteration i'm not going to add that in this file this file was is more so to get everything configured with sharepoint now i'm going to create another file and i'm going to call projects this is a file where you will do your iteration this is where you will kind of build that logic to kind of for example read some kind of local directory iterate over if you have a lot of files and then of course start publishing all these files to sharepoint that's what we're going to do here now before we do that what i'm going to end up doing is i'm going to [Music] open up my sharepoint uh site that i'm going to be testing on so again i don't mind i'm not going i mean it doesn't matter if you know what my url is so i'm not going to hide or nothing but this is the the domain of my kind of going back to the config file this will be yours this is happened to be mine and then i have my site in this case called the level mint because this is what i use to do the level when i'm doing some testing for my other clients um so that's kind of where what you would use in your case now what i'm gonna do uh again libraries we talked about libraries earlier the the uh for sharepoint it's part of your documents that's what they call it so what i'm going to end up doing here for for this example i'm going to create a new folder and i'm going to call it youtube right because that's you know youtube so we're going to call it youtube keep it consistent that's how you know i'm showing you on youtube so i just call it that that's fine um all right so now that i got that created what i'm gonna do now is let's just create a basic script that we're gonna upload we're gonna ultimately upload this file see the sample pdf file this is the file that we're gonna end up uploading to uh sharepoint um so what i what i need to do first is i need to bring in my sharepoint i'm to call this file and i'm going to import my class right again i i created this class car sharepoint and that's what i'm importing in all right so now that i have that let me just create a um a variable file directory pass right so this one this is going to be the path of where this is that so let me just copy it right quick um copy pass there you go so this is my full path again if you're iterating over you know you you won't have to build some logic to kind of go through each folder and so on but just kind of trying to show y'all as a quick example uh next is i'm going to call it file name so ultimate when we uploaded the sharepoint um we're we're going to specify a file name now technically the file could be the same name right so we could just call it whatever that you know same name as as we as it it stands today or this is the opportunity for you to create a new name you may want to create a new name and that's fine um so this is ultimately where you would do it the next thing is we're going to specify the folder name all right so in this case i'm going to call it 20 20. so by that what what's going to happen here is i have a folder that i created right this is the url path which is called youtube now in my case though i want to upload documents uh by year because maybe the file that i'm going to be dealing with is going to be on a per year basis right so i got 2019 files when 2020 comes i want to upload data files to 2020 folder you know so on and so on um so because that's the way i'm doing it in this example i'm going to call i'm going to create this um variable name for file name called 2020. uh the next thing is now i'm going to end up calling my sharepoint class upload file and now let's specify i'm gonna specify my file directory pass because that's what that's the first um my first parameter here is is file all right guys i'm back actually um my camera died on me so i need to put another uh battery in so hopefully i could pick up from where i left off at so let me let me just start from where i left off and hopefully it should be fine um so the next argument here is going to be let's see what is the next argument uh file name alright so i'm going to specify the file name i have on top um what is the folder name all right the next thing is going to be folder name so ultimately what we have here i specified the path of the file i specified the file name again in my case i'm gonna call it the same but we'll test it out by changing it as well then and and i want to create a new folder so one of the things that as well just kind of let y'all know and you don't need to create the folder either in sharepoint so again i'll show you an example if i if i go back to my sharepoint site i don't i'm going to youtube there's nothing in here i don't have a folder called 2020. there's nothing in here no i'm going to run run this script what should happen is it should create a folder called 2020 then on top of that it should upload this document into that folder under this file name so let's take a look at it see if it works uh did not work okay so let's see what happened um let's see alright so let's take a look let's see what what am i let's see what do i have wrong what am i missing um let's see i have file content um i am reading the object so that's fine so this seemed to fill on what line 22 oh i think i know what it's doing uh i wonder if it's because i didn't specify see i don't think i had to but maybe i do username and then you just specify that this is the password i did specify it over here so that's actually fine that's the only thing i could see so let me let's let's run it again all right let's clear that out let's take a look at it um what are we having man come on let's see oh you know what damn it i just realized it see see where it appears in sharepoint sites this is not supposed to be sharepoint site this is supposed to be sharepoint url damn it man almost man almost had it running on one try but it is what it is um all right so let's run it again all right no errors that's good no errors everything looks good now let's let's go back to my sharepoint and i'm going to hit refresh so there you go so now we have a new folder 2020 and there's our sample file against this it's an actual file i just it's a random file i just downloaded online but again that's the same file that i have and um that that we're working on so let me just just so you kind of see for yourself here's the sample file if i open that up there you go right same file so so again this worked from a sense of it uploading alright so at least we know that is working properly so now let me go back and um so now let's say we want to delete the file alright so this something that went to the process to upload a file and hopefully this makes sense to everybody if we look at the code this is all just configuration right all this is doing is saying let's read the config file let's assign i'm gonna assign those the variables the the keys to two variables which i'm gonna end up using to authenticate which i'm using down here right the url username password etc so that's all this is really for now this class is pretty much is used for anything sharepoint related so in my case i have a a message to authenticate i have what i'm calling connect folder which also means in order to connect to a folder we have to specify what folder want to connect to so in my case i connect it to uh i'm connecting to the youtube folder it could be something else another why why youtube if i go back to my config file see where it says document name i'm again i'm not going to show you my config that i'm using this project but ultimately what i end up doing in my document name i specified youtube if this is gonna be a variable um then of course don't your library you can leave blank that you could like you could specify it in in your iteration and in my case though i know which folder i'm dealing with right like i have my main folder which is called youtube and then inside the youtube folder i may have many other folders i may have it by ear whatever it may be right it just depends but that's just the way i did it on mine um but ultimately that's what the connect folder is doing right and then of course we have the upload folder upload file and again you can tell we're calling this method over here which authenticates us and gets us connected to the right sharepoint folder and so all we have to do here is pass which file we want to read right we got it in this case we got to read the file first which i'm happy to be dealing with pdf files if you're dealing with non-pdf file like text files change your mode to r but in my case it's read binary and then we're going we're going to upload the file so we're uploading the context which is the actual data and we're specifying the file name what is the file name that we're going to um that we're going to be calling so that's kind of what's what's happening here again the folder name up here is what specifies which folder to connect to and keep in mind if a folder does not exist it will automatically get created in sharepoint now what we want to do next is we're going to create a method to delete file because we want to delete the file now so we're going to specify a file name and which folder it belongs to again pretty straightforward which folder belongs to your sharepoint then we're going to specify the name that's how we know to delete the file um so i'm going to end up calling when i'm going to um um cause this method the connect folder because i need to be able to connect again that connect message is going to authenticate me then you know assign me to the right sharepoint directory so let me specify the file name and then i'm going to [Music] call the delete file message again that's not part of i know i call my delete file but this is not related to my method it's related to the share plump method and then in here i'm going to specify the file name that simple so now if i go back to my project i'm going to call that new method delete file i'm going to specify file name then i'm going to specify the folder oops the the folder which is again 2020. so again if i go back as you can tell i got a folder called 2020 then if i click in it oops let's see what happens there it goes if i click on it i have a file called sample pdf so what we're going to do next is we are going to let's go ahead and run the delete oh there you go they ran no issues so now if i come back to sharepoint i'm going to hit refresh and no file my file is deleted so again very simple when you think about it it's really not that complex it's just more getting you're getting everything configured as you could tell though what i end up doing here i have what i call a project file i would strongly recommend don't put all of this together man don't create you know your authentication and all this other logic that you're doing here and then including your project file because ultimately what happens is this in my mind this is strictly more just kind of get things configured to get things connected again i am using the share pump um library and it makes it a lot easier but nevertheless though there are certain things that i wanted to kind of streamline that it's more unique to me which is why i have this sharepoint file and i have that working doing the way i want to do which is the authentication connecting to the folder things of that nature right in this case reading specific files and then of course i just call it as you can tell my project file is a lot smaller this project file will be the file that you will build your logic to let's say for example iterate over a directory pass to get the list of all files uploaded and then of course you know you can repeat the process if you have multiple files let's say you have a folder that has a 2019 to 2020 and let's say a 2021 file and then of course you could build you know um um a script let's see you have a 2019 2020 21 2021 uh folders and then of course as um you can build your your script to ultimately iterate through that using like the um os walker or i mean it's up to you how you want to do it but you'll be able to read identify all the files and from there you that's where you would just call again cozy message right up let's upload the file based on these variable names which would be whatever folder you're going to upload it to again it's up to you how you want to do it but nevertheless this gives you a good baseline good and hopefully a good understanding on how to connect to sharepoint um through the api again it's pretty straightforward and not not that complex and then like the mechanics on what's happening here when we're dealing with uploading a file deleting a file um you if you're gonna do something very basic like hey i just you know i'm going to iterate over the one file very ad hoc then there's certain things that you could do you don't have to do it the way i did right you could like just kind of authenticate on your one script you know be able to connect to um call your folder um because the upload of the upload message that that's part of share pump and then you start uploading your content directly right very basic script but you can do it that way and when you're doing ad hoc i get it right but more something that's systemic that gonna be again you're gonna be building on that's part of your application you're not do it that way man separate it out build it right um hopefully this helps y'all out man hopefully this gives you a little bit of insight again what i'm planning to do with this channel is i want to show y'all like again projects and i'm showing y'all stuff that i'm going through myself so the reason why i'm showing you this specifically about sharepoint because i actually finished working on a project that i had to do this for um a little bit there the the overall scope of that project will ultimately generate you know there's a process that generated pdf files and saving it to a directory then locally then i have another process that doing the sharepoint piece which is ultimately picking up those files and uploading to sharepoint so again just kind of high level that's kind of what the project was about but it was um you know just want to share with you all those who are new who may be who may not know how to upload to sharepoint um in this case office 365. hopefully this helps you out and give you some guidance some understanding i'm planning to do more videos like that so more of my projects that i'm working on again real world projects i want to share with y'all how i was able to solve these problems so hopefully you like it give me thumbs up you know follow me check out my other videos peace
Info
Channel: I am Lu
Views: 12,297
Rating: 4.9631338 out of 5
Keywords:
Id: BV6SLhOF3zU
Channel Id: undefined
Length: 44min 55sec (2695 seconds)
Published: Sun Sep 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.