Chilkat Examples

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

C Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
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

 

 

 

(C) SharePoint -- Upload File

Demonstrates how to upload a file to a SharePoint folder.

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_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkXml.h>
#include <C_CkHttpRequest.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    HCkHttpResponse resp;
    HCkXml xml;
    HCkHttpRequest req;
    BOOL bUseTls;

    //  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 = CkHttp_Create();

    //  If SharePoint Windows classic authentication is used, then set the
    //  Login, Password, LoginDomain, and NtlmAuth properties.
    CkHttp_putLogin(http,"SHAREPOINT_USERNAME");
    CkHttp_putPassword(http,"SHAREPOINT_PASSWORD");
    CkHttp_putLoginDomain(http,"SHAREPOINT_NTLM_DOMAIN");
    CkHttp_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
    //  -------------------------------------------------------------------------

    //  When creating, updating, and deleting SharePoint entities, we'll need
    //  to first get the server's form digest value to send in the X-RequestDigest header.
    //  This can be retrieved by making a POST request with an empty body to
    //  http://<site url>/_api/contextinfo and extracting the value of the
    //  d:FormDigestValue node in the XML that the contextinfo endpoint returns.
    resp = CkHttp_PostXml(http,"https://SHAREPOINT_HTTPS_DOMAIN/_api/contextinfo","","utf-8");
    if (CkHttp_getLastMethodSuccess(http) != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        return;
    }

    if (CkHttpResponse_getStatusCode(resp) != 200) {
        //  A response status code not equal to 200 indicates failure.
        printf("Response status code = %d\n",CkHttpResponse_getStatusCode(resp));
        printf("Response body:\n");
        printf("%s\n",CkHttpResponse_bodyStr(resp));
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        return;
    }

    xml = CkXml_Create();
    CkXml_LoadXml(xml,CkHttpResponse_bodyStr(resp));
    CkHttpResponse_Dispose(resp);

    printf("%s\n",CkXml_getXml(xml));

    //  The response XML looks like this:

    //  <?xml version="1.0" encoding="utf-8" ?>
    //  <d:GetContextWebInformation xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" m:type="SP.ContextWebInformation">
    //      <d:FormDigestTimeoutSeconds m:type="Edm.Int32">1800</d:FormDigestTimeoutSeconds>
    //      <d:FormDigestValue>0x3059FFB920651834540F3E6792EA73F5746B302E953FF4E808E485DB1E6C2836C7CF924644995F092453B02A94DE14A7962674B7B16780AF16EAFB8C246BCDC7,12 Apr 2017 17:08:22 -0000</d:FormDigestValue>
    //      <d:LibraryVersion>15.0.4569.1000</d:LibraryVersion>
    //      <d:SiteFullUrl>https://SHAREPOINT_HTTPS_DOMAIN</d:SiteFullUrl>
    //      <d:SupportedSchemaVersions m:type="Collection(Edm.String)">
    //          <d:element>14.0.0.0</d:element>
    //          <d:element>15.0.0.0</d:element>
    //      </d:SupportedSchemaVersions>
    //      <d:WebFullUrl>https://SHAREPOINT_HTTPS_DOMAIN</d:WebFullUrl>
    //  </d:GetContextWebInformation>
    // 

    //  We'll use the d:FormDigestValue in the X-RequestDigest request header
    //  for the actual file upload.

    //  --------------------------------------------------------------------
    //  Now upload a file named "penguins2.jpg" to the "/Documents" folder.

    req = CkHttpRequest_Create();
    CkHttpRequest_putHttpVerb(req,"POST");
    //  Change "/Documents" to whatever folder is the destination.
    CkHttpRequest_putPath(req,"/_api/web/GetFolderByServerRelativeUrl('/Documents')/Files/add(url='penguins2.jpg',overwrite=true)");
    //  Give the location of the file to be uploaded.
    //  Note: The file is not loaded into memory at this point.  It is never fully loaded
    //  into memory.  Instead, the file data is streamed directly from the file when
    //  when the upload occurs.
    CkHttpRequest_StreamBodyFromFile(req,"qa_data/jpg/penguins2.jpg");

    //  Add the X-RequestDigest header field.
    CkHttpRequest_AddHeader(req,"X-RequestDigest",CkXml_getChildContent(xml,"d:FormDigestValue"));

    //  Do the upload using https (TLS). Port 443 is the default port for https.
    bUseTls = TRUE;
    resp = CkHttp_SynchronousRequest(http,"SHAREPOINT_HTTPS_DOMAIN",443,bUseTls,req);
    if (CkHttp_getLastMethodSuccess(http) != TRUE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkXml_Dispose(xml);
        CkHttpRequest_Dispose(req);
        return;
    }

    //  The response body contains XML.  Load it into our xml object..
    CkXml_LoadXml(xml,CkHttpResponse_bodyStr(resp));

    if (CkHttpResponse_getStatusCode(resp) != 200) {
        printf("Response status code = %d\n",CkHttpResponse_getStatusCode(resp));
        printf("%s\n",CkXml_getXml(xml));
        printf("Failed\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkXml_Dispose(xml);
        CkHttpRequest_Dispose(req);
        return;
    }

    CkHttpResponse_Dispose(resp);

    //  Show the successful XML response.
    printf("%s\n",CkXml_getXml(xml));
    printf("Success.\n");

    //  A successful XML response looks like this:

    //  <?xml version="1.0" encoding="utf-8" ?>
    //  <entry xml:base="https://SHAREPOINT_HTTPS_DOMAIN/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
    //      <id>Web/GetFileByServerRelativeUrl('/Documents/penguins2.jpg')</id>
    //      <category term="SP.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    //      <link rel="edit" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)" />
    //      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author" type="application/atom+xml;type=entry" title="Author" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)/Author" />
    //      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CheckedOutByUser" type="application/atom+xml;type=entry" title="CheckedOutByUser" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)/CheckedOutByUser" />
    //      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ListItemAllFields" type="application/atom+xml;type=entry" title="ListItemAllFields" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)/ListItemAllFields" />
    //      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/LockedByUser" type="application/atom+xml;type=entry" title="LockedByUser" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)/LockedByUser" />
    //      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)/ModifiedBy" />
    //      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Versions" type="application/atom+xml;type=feed" title="Versions" href="Web/GetFileByServerRelativeUrl(&apos;/Documents/penguins2.jpg&apos;)/Versions" />
    //      <title />
    //      <updated>2017-04-12T17:16:44Z</updated>
    //      <author>
    //          <name />
    //      </author>
    //      <content type="application/xml">
    //          <m:properties>
    //              <d:CheckInComment />
    //              <d:CheckOutType m:type="Edm.Int32">0</d:CheckOutType>
    //              <d:ContentTag>{FC8D1A58-9544-496C-8D9C-19B501A8B322},2,3</d:ContentTag>
    //              <d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus>
    //              <d:ETag>"{FC8D1A58-9544-496C-8D9C-19B501A8B322},2"</d:ETag>
    //              <d:Exists m:type="Edm.Boolean">true</d:Exists>
    //              <d:Length m:type="Edm.Int64">99832</d:Length>
    //              <d:Level m:type="Edm.Byte">255</d:Level>
    //              <d:MajorVersion m:type="Edm.Int32">0</d:MajorVersion>
    //              <d:MinorVersion m:type="Edm.Int32">1</d:MinorVersion>
    //              <d:Name>penguins2.jpg</d:Name>
    //              <d:ServerRelativeUrl>/Documents/penguins2.jpg</d:ServerRelativeUrl>
    //              <d:TimeCreated m:type="Edm.DateTime">2017-04-12T17:14:19Z</d:TimeCreated>
    //              <d:TimeLastModified m:type="Edm.DateTime">2017-04-12T17:16:44Z</d:TimeLastModified>
    //              <d:Title m:null="true" />
    //              <d:UIVersion m:type="Edm.Int32">1</d:UIVersion>
    //              <d:UIVersionLabel>0.1</d:UIVersionLabel>
    //          </m:properties>
    //      </content>
    //  </entry>
    // 
    // 


    CkHttp_Dispose(http);
    CkXml_Dispose(xml);
    CkHttpRequest_Dispose(req);

    }

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.