Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks

Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
Office365
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Unicode C) SharePoint -- Download Newer Files

Demonstrates how to download all files from a SharePoint folder that are newer than the local files.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#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");

    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-2020 Chilkat Software, Inc. All Rights Reserved.