Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v11.0.0+

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 Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkFileAccessW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDateTimeW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkStringBuilderW sbJson;
    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;

    success = FALSE;

    //  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 == FALSE) {
        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();
    lastModLocal = 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.
            CkDateTimeW_SetFromTimestamp(lastModLocal,CkFileAccessW_getFileTimeStr(fac,CkStringBuilderW_getAsString(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 = CkDateTimeW_DiffSeconds(lastModLocal,lastModRemote);
            if (numSeconds < 0) {
                wprintf(L"The local file is older than the remote file.\n");
                bDownload = TRUE;
            }

        }

        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 == FALSE) {
                wprintf(L"%s\n",CkHttpW_lastErrorText(http));
                CkHttpW_Dispose(http);
                CkStringBuilderW_Dispose(sbJson);
                CkFileAccessW_Dispose(fac);
                CkJsonObjectW_Dispose(json);
                CkDateTimeW_Dispose(lastModRemote);
                CkDateTimeW_Dispose(lastModLocal);
                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);
    CkDateTimeW_Dispose(lastModLocal);
    CkStringBuilderW_Dispose(localPath);
    CkStringBuilderW_Dispose(fileUrl);

    }