Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) SharePoint -- Download Newer FilesSee more SharePoint ExamplesDemonstrates how to download all files from a SharePoint folder that are newer than the local files.
IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkFileAccess.pb" IncludeFile "CkDateTime.pb" Procedure ChilkatExample() ; This requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. ; ------------------------------------------------------------------------- ; The following comments apply to SharePoint Windows classic authentication. ; ------------------------------------------------------------------------- ; For example, imagine our SharePoint endpoint is https://xyzoffice.mycompany.com/ ; The SHAREPOINT_NTLM_DOMAIN would be "mycompany.com" ; The SHAREPOINT_HTTPS_DOMAIN would be "xyzoffice.mycompany.com" ; Also, the SHAREPOINT_USERNAME would be just the name, not a full email address. ; for example, "chilkat" instead of "chilkat@mycompany.com" http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; If SharePoint Windows classic authentication is used, then set the ; Login, Password, LoginDomain, and NtlmAuth properties. CkHttp::setCkLogin(http, "SHAREPOINT_USERNAME") CkHttp::setCkPassword(http, "SHAREPOINT_PASSWORD") CkHttp::setCkLoginDomain(http, "SHAREPOINT_NTLM_DOMAIN") CkHttp::setCkNtlmAuth(http, 1) ; ------------------------------------------------------------------------- ; The more common case is to use SharePoint Online authentication (via the SPOIDCRL cookie). ; If so, do not set Login, Password, LoginDomain, and NtlmAuth, and instead ; establish the cookie as shown at SharePoint Online Authentication ; ------------------------------------------------------------------------- ; First we'll download a list of all the files in the /Documents folder. ; This provides the names and last-modified date/times of the files located ; on the SharePoint server. CkHttp::setCkAccept(http, "application/json;odata=verbose") CkHttp::setCkAcceptCharset(http, "utf-8") sbJson.i = CkStringBuilder::ckCreate() If sbJson.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkHttp::ckQuickGetSb(http,"https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files",sbJson) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sbJson) ProcedureReturn EndIf ; Before proceeding, make sure the local directory where we'll be downloading files exists. fac.i = CkFileAccess::ckCreate() If fac.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkFileAccess::ckDirEnsureExists(fac,"qa_output/sharepoint/Documents") ; OK.. load the JSON and iterate over each file json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(json,sbJson) numFiles.i = CkJsonObject::ckSizeOfArray(json,"d.results") Debug "Number of Files in the SharePoint /Documents folder = " + Str(numFiles) lastModRemote.i = CkDateTime::ckCreate() If lastModRemote.i = 0 Debug "Failed to create object." ProcedureReturn EndIf lastModLocal.i localPath.i = CkStringBuilder::ckCreate() If localPath.i = 0 Debug "Failed to create object." ProcedureReturn EndIf fileUrl.i = CkStringBuilder::ckCreate() If fileUrl.i = 0 Debug "Failed to create object." ProcedureReturn EndIf i.i = 0 While i < numFiles CkJsonObject::setCkI(json, i) filename.s = CkJsonObject::ckStringOf(json,"d.results[i].Name") sLastModified.s = CkJsonObject::ckStringOf(json,"d.results[i].TimeLastModified") Debug Str(i + 1) + ": " + filename + " (" + sLastModified + ")" CkDateTime::ckSetFromTimestamp(lastModRemote,sLastModified) bDownload.i = 0 ; Check to see if the local file exists. If not, then download. CkStringBuilder::ckSetString(localPath,"qa_output/sharepoint/Documents/") CkStringBuilder::ckAppend(localPath,filename) If CkFileAccess::ckFileExists(fac,CkStringBuilder::ckGetAsString(localPath)) <> 1 Debug "This file does not exist locally." bDownload = 1 Else ; Get the local file's date time and compare with the remote file date/time. lastModLocal = CkFileAccess::ckGetLastModified(fac,CkStringBuilder::ckGetAsString(localPath)) If CkFileAccess::ckLastMethodSuccess(fac) = 1 ; Get the difference in seconds between the local and remote last-modified times. ; if the return value is negative, then the caller's time is ; older than the argument. (in this case, a negative return value means ; the local file is older than the remote file. ; Note: The DiffSeconds method was found to be missing in the Chilkat .NET build ; (and possibly in other builds). It will be present in the v9.5.0.67 release and later. numSeconds.i = CkDateTime::ckDiffSeconds(lastModLocal,lastModRemote) If numSeconds < 0 Debug "The local file is older than the remote file." bDownload = 1 EndIf CkDateTime::ckDispose(lastModLocal) Else Debug "Unable to get the local file's last-modified date/time." Debug CkFileAccess::ckLastErrorText(fac) EndIf EndIf If bDownload = 1 CkStringBuilder::ckSetString(fileUrl,"https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files('") CkStringBuilder::ckAppend(fileUrl,filename) CkStringBuilder::ckAppend(fileUrl,"')/$value") Debug "Downloading " + filename success = CkHttp::ckDownload(http,CkStringBuilder::ckGetAsString(fileUrl),CkStringBuilder::ckGetAsString(localPath)) If success <> 1 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sbJson) CkFileAccess::ckDispose(fac) CkJsonObject::ckDispose(json) CkDateTime::ckDispose(lastModRemote) CkStringBuilder::ckDispose(localPath) CkStringBuilder::ckDispose(fileUrl) ProcedureReturn EndIf ; Set the local file's last-modified date/time to that of the server's. CkFileAccess::ckSetLastModified(fac,CkStringBuilder::ckGetAsString(localPath),lastModRemote) EndIf i = i + 1 Wend Debug "All finished." CkHttp::ckDispose(http) CkStringBuilder::ckDispose(sbJson) CkFileAccess::ckDispose(fac) CkJsonObject::ckDispose(json) CkDateTime::ckDispose(lastModRemote) CkStringBuilder::ckDispose(localPath) CkStringBuilder::ckDispose(fileUrl) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.