PureBasic Requires Chilkat v11.0.0+
PureBasic
SharePoint -- Download Newer Files
See more SharePoint Examples
Demonstrates how to download all files from a SharePoint folder that are newer than the local files.Chilkat PureBasic Downloads
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkDateTime.pb"
Procedure ChilkatExample()
success.i = 0
; 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 = CkHttp::ckQuickGetSb(http,"https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files",sbJson)
If success = 0
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 = CkDateTime::ckCreate()
If lastModLocal.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
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.
CkDateTime::ckSetFromTimestamp(lastModLocal,CkFileAccess::ckGetFileTimeStr(fac,CkStringBuilder::ckGetAsString(localPath),0))
; 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
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 = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkStringBuilder::ckDispose(sbJson)
CkFileAccess::ckDispose(fac)
CkJsonObject::ckDispose(json)
CkDateTime::ckDispose(lastModRemote)
CkDateTime::ckDispose(lastModLocal)
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)
CkDateTime::ckDispose(lastModLocal)
CkStringBuilder::ckDispose(localPath)
CkStringBuilder::ckDispose(fileUrl)
ProcedureReturn
EndProcedure