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
(Lianja) SharePoint -- Get Server Form Digest ValueDemonstrates how to get a server form digest value to be placed in the X-RequestDigest HTTP request header for POST, PUT, MERGE, and DELETE requests. A form digest value is typically valid for 1800 seconds (i.e. 30 minutes). This example persists the value to a file, and only requests a new form digest value if the existing one is near expiration.
// This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // First, let's see if we already have a persisted form digest value // that hasn't yet expired. loFac = createobject("CkFileAccess") loXml = createobject("CkXml") // My example code (below) persists the form digest XML in this format: // // <savedFormDigestValue> // <d:ExpireDateTime>2017-04-12T20:46:39Z</d:ExpireDateTime> // <d:FormDigestValue>0x3059FFB920651834540F3E6792EA73F5746B302E953FF4E808E485DB1E6C2836C7CF924644995F092453B02A94DE14A7962674B7B16780AF16EAFB8C246BCDC7,12 Apr 2017 17:08:22 -0000</d:FormDigestValue> // </savedFormDigestValue> // loDtExpire = createobject("CkDateTime") loDtNow = createobject("CkDateTime") lcFormDigestXmlFile = "qa_data/sharepoint/savedFormDigestValue.xml" if (loFac.FileExists(lcFormDigestXmlFile) = .T.) then loXml.LoadXmlFile(lcFormDigestXmlFile) // Get the expire date/time loDtExpire.SetFromTimestamp(loXml.GetChildContent("d:ExpireDateTime")) // Get the current date/time loDtNow.SetFromCurrentSystemTime() // Get both times as Unix time values lnTNow = loDtNow.GetAsUnixTime(.F.) lnTExpire = loDtExpire.GetAsUnixTime(.F.) // If tNow >= tExpire, then fall through. // Otherwise, just use the cached digest value. if (lnTNow < lnTExpire) then ? "Cached digest value is not yet expired." ? "X-RequestDigest: " + loXml.GetChildContent("d:FormDigestValue") release loFac release loXml release loDtExpire release loDtNow return endif endif // If we got to this point, the cached digest value either does not exist, or expired. loHttp = createobject("CkHttp") // If SharePoint Windows classic authentication is used, then set the // Login, Password, LoginDomain, and NtlmAuth properties. loHttp.Login = "SHAREPOINT_USERNAME" loHttp.Password = "SHAREPOINT_PASSWORD" loHttp.LoginDomain = "SHAREPOINT_NTLM_DOMAIN" loHttp.NtlmAuth = .T. // 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. // Apparently, SharePoint needs an "Accept" request header equal to "application/xml", // otherwise SharePoint will return an utterly incomprehensible and useless error message. lcSavedAccept = loHttp.Accept loHttp.Accept = "application/xml" // Note: The last argument ("utf-8") is meaningless here because the body is empty. loResp = loHttp.PostXml("https://SHAREPOINT_HTTPS_DOMAIN/_api/contextinfo","","utf-8") if (loHttp.LastMethodSuccess <> .T.) then ? loHttp.LastErrorText release loFac release loXml release loDtExpire release loDtNow release loHttp return endif // Restore the default Accept header loHttp.Accept = lcSavedAccept if (loResp.StatusCode <> 200) then // A response status code not equal to 200 indicates failure. ? "Response status code = " + str(loResp.StatusCode) ? "Response body:" ? loResp.BodyStr release loResp release loFac release loXml release loDtExpire release loDtNow release loHttp return endif loXml.LoadXml(loResp.BodyStr) release loResp // 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> // // Cache the digest value, and also an expiration time. If this code is run again // before the digest expires, we'll just get it from the file. loXml2 = createobject("CkXml") loXml2.Tag = "savedFormDigestValue" loXml2.NewChild2("d:FormDigestValue",loXml.GetChildContent("d:FormDigestValue")) lnTimeoutInSec = loXml.GetChildIntValue("d:FormDigestTimeoutSeconds") ? "Timeout in seconds = " + str(lnTimeoutInSec) // Convert this to an expire timestamp. // Let's make it expire 30 seconds prior to the actual timeout, just to be safe. if (lnTimeoutInSec > 30) then lnTimeoutInSec = lnTimeoutInSec - 30 endif loDtExpire.SetFromCurrentSystemTime() loDtExpire.AddSeconds(lnTimeoutInSec) loXml2.NewChild2("d:ExpireDateTime",loDtExpire.GetAsTimestamp(.F.)) // Persist the digest and expire time to a file. loXml2.SaveXml(lcFormDigestXmlFile) ? "Here is the new form digest value:" ? "X-RequestDigest: " + loXml.GetChildContent("d:FormDigestValue") release loFac release loXml release loDtExpire release loDtNow release loHttp release loXml2 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.