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
(Unicode C) SharePoint -- Download Newer FilesSee more SharePoint ExamplesDemonstrates how to download all files from a SharePoint folder that are newer than the local files.
#include <C_CkHttpW.h> #include <C_CkStringBuilderW.h> #include <C_CkFileAccessW.h> #include <C_CkJsonObjectW.h> #include <C_CkDateTimeW.h> void ChilkatSample(void) { HCkHttpW http; HCkStringBuilderW sbJson; BOOL success; HCkFileAccessW fac; HCkJsonObjectW json; int numFiles; HCkDateTimeW lastModRemote; HCkDateTimeW lastModLocal; HCkStringBuilderW localPath; HCkStringBuilderW fileUrl; int i; const wchar_t *filename; const wchar_t *sLastModified; BOOL bDownload; int numSeconds; // 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 = CkHttpW_Create(); // If SharePoint Windows classic authentication is used, then set the // Login, Password, LoginDomain, and NtlmAuth properties. CkHttpW_putLogin(http,L"SHAREPOINT_USERNAME"); CkHttpW_putPassword(http,L"SHAREPOINT_PASSWORD"); CkHttpW_putLoginDomain(http,L"SHAREPOINT_NTLM_DOMAIN"); CkHttpW_putNtlmAuth(http,TRUE); // ------------------------------------------------------------------------- // 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. CkHttpW_putAccept(http,L"application/json;odata=verbose"); CkHttpW_putAcceptCharset(http,L"utf-8"); sbJson = CkStringBuilderW_Create(); success = CkHttpW_QuickGetSb(http,L"https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files",sbJson); if (success != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbJson); return; } // Before proceeding, make sure the local directory where we'll be downloading files exists. fac = CkFileAccessW_Create(); CkFileAccessW_DirEnsureExists(fac,L"qa_output/sharepoint/Documents"); // OK.. load the JSON and iterate over each file json = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(json,sbJson); numFiles = CkJsonObjectW_SizeOfArray(json,L"d.results"); wprintf(L"Number of Files in the SharePoint /Documents folder = %d\n",numFiles); lastModRemote = CkDateTimeW_Create(); localPath = CkStringBuilderW_Create(); fileUrl = CkStringBuilderW_Create(); i = 0; while (i < numFiles) { CkJsonObjectW_putI(json,i); filename = CkJsonObjectW_stringOf(json,L"d.results[i].Name"); sLastModified = CkJsonObjectW_stringOf(json,L"d.results[i].TimeLastModified"); wprintf(L"%d: %s (%s)\n",i + 1,filename,sLastModified); CkDateTimeW_SetFromTimestamp(lastModRemote,sLastModified); bDownload = FALSE; // Check to see if the local file exists. If not, then download. CkStringBuilderW_SetString(localPath,L"qa_output/sharepoint/Documents/"); CkStringBuilderW_Append(localPath,filename); if (CkFileAccessW_FileExists(fac,CkStringBuilderW_getAsString(localPath)) != TRUE) { wprintf(L"This file does not exist locally.\n"); bDownload = TRUE; } else { // Get the local file's date time and compare with the remote file date/time. lastModLocal = CkFileAccessW_GetLastModified(fac,CkStringBuilderW_getAsString(localPath)); if (CkFileAccessW_getLastMethodSuccess(fac) == TRUE) { // 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 = CkDateTimeW_DiffSeconds(lastModLocal,lastModRemote); if (numSeconds < 0) { wprintf(L"The local file is older than the remote file.\n"); bDownload = TRUE; } CkDateTimeW_Dispose(lastModLocal); } else { wprintf(L"Unable to get the local file's last-modified date/time.\n"); wprintf(L"%s\n",CkFileAccessW_lastErrorText(fac)); } } if (bDownload == TRUE) { CkStringBuilderW_SetString(fileUrl,L"https://SHAREPOINT_HTTPS_DOMAIN/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files('"); CkStringBuilderW_Append(fileUrl,filename); CkStringBuilderW_Append(fileUrl,L"')/$value"); wprintf(L"Downloading %s\n",filename); success = CkHttpW_Download(http,CkStringBuilderW_getAsString(fileUrl),CkStringBuilderW_getAsString(localPath)); if (success != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbJson); CkFileAccessW_Dispose(fac); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(lastModRemote); CkStringBuilderW_Dispose(localPath); CkStringBuilderW_Dispose(fileUrl); return; } // Set the local file's last-modified date/time to that of the server's. CkFileAccessW_SetLastModified(fac,CkStringBuilderW_getAsString(localPath),lastModRemote); } i = i + 1; } wprintf(L"All finished.\n"); CkHttpW_Dispose(http); CkStringBuilderW_Dispose(sbJson); CkFileAccessW_Dispose(fac); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(lastModRemote); CkStringBuilderW_Dispose(localPath); CkStringBuilderW_Dispose(fileUrl); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.