PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_SbJson
oleobject loo_Fac
oleobject loo_Json
long li_NumFiles
oleobject loo_LastModRemote
oleobject loo_LastModLocal
oleobject loo_LocalPath
oleobject loo_FileUrl
long i
string ls_Filename
string ls_SLastModified
long li_BDownload
long li_NumSeconds
li_Success = 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"
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
// If SharePoint Windows classic authentication is used, then set the
// Login, Password, LoginDomain, and NtlmAuth properties.
loo_Http.Login = "SHAREPOINT_USERNAME"
loo_Http.Password = "SHAREPOINT_PASSWORD"
loo_Http.LoginDomain = "SHAREPOINT_NTLM_DOMAIN"
loo_Http.NtlmAuth = 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.
loo_Http.Accept = "application/json;odata=verbose"
loo_Http.AcceptCharset = "utf-8"
loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Http.QuickGetSb("https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files",loo_SbJson)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_SbJson
return
end if
// Before proceeding, make sure the local directory where we'll be downloading files exists.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
loo_Fac.DirEnsureExists("qa_output/sharepoint/Documents")
// OK.. load the JSON and iterate over each file
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.LoadSb(loo_SbJson)
li_NumFiles = loo_Json.SizeOfArray("d.results")
Write-Debug "Number of Files in the SharePoint /Documents folder = " + string(li_NumFiles)
loo_LastModRemote = create oleobject
li_rc = loo_LastModRemote.ConnectToNewObject("Chilkat.CkDateTime")
loo_LastModLocal = create oleobject
li_rc = loo_LastModLocal.ConnectToNewObject("Chilkat.CkDateTime")
loo_LocalPath = create oleobject
li_rc = loo_LocalPath.ConnectToNewObject("Chilkat.StringBuilder")
loo_FileUrl = create oleobject
li_rc = loo_FileUrl.ConnectToNewObject("Chilkat.StringBuilder")
i = 0
do while i < li_NumFiles
loo_Json.I = i
ls_Filename = loo_Json.StringOf("d.results[i].Name")
ls_SLastModified = loo_Json.StringOf("d.results[i].TimeLastModified")
Write-Debug string(i + 1) + ": " + ls_Filename + " (" + ls_SLastModified + ")"
loo_LastModRemote.SetFromTimestamp(ls_SLastModified)
li_BDownload = 0
// Check to see if the local file exists. If not, then download.
loo_LocalPath.SetString("qa_output/sharepoint/Documents/")
loo_LocalPath.Append(ls_Filename)
if loo_Fac.FileExists(loo_LocalPath.GetAsString()) <> 1 then
Write-Debug "This file does not exist locally."
li_BDownload = 1
else
// Get the local file's date time and compare with the remote file date/time.
loo_LastModLocal.SetFromTimestamp(loo_Fac.GetFileTimeStr(loo_LocalPath.GetAsString(),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.
li_NumSeconds = loo_LastModLocal.DiffSeconds(loo_LastModRemote)
if li_NumSeconds < 0 then
Write-Debug "The local file is older than the remote file."
li_BDownload = 1
end if
end if
if li_BDownload = 1 then
loo_FileUrl.SetString("https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files('")
loo_FileUrl.Append(ls_Filename)
loo_FileUrl.Append("')/$value")
Write-Debug "Downloading " + ls_Filename
li_Success = loo_Http.Download(loo_FileUrl.GetAsString(),loo_LocalPath.GetAsString())
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_SbJson
destroy loo_Fac
destroy loo_Json
destroy loo_LastModRemote
destroy loo_LastModLocal
destroy loo_LocalPath
destroy loo_FileUrl
return
end if
// Set the local file's last-modified date/time to that of the server's.
loo_Fac.SetLastModified(loo_LocalPath.GetAsString(),loo_LastModRemote)
end if
i = i + 1
loop
Write-Debug "All finished."
destroy loo_Http
destroy loo_SbJson
destroy loo_Fac
destroy loo_Json
destroy loo_LastModRemote
destroy loo_LastModLocal
destroy loo_LocalPath
destroy loo_FileUrl