Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Excel) 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. Dim fac As Chilkat.FileAccess Set fac = Chilkat.NewFileAccess Dim xml As Chilkat.Xml Set xml = Chilkat.NewXml ' 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> ' Dim dtExpire As Chilkat.CkDateTime Set dtExpire = Chilkat.NewCkDateTime Dim dtNow As Chilkat.CkDateTime Set dtNow = Chilkat.NewCkDateTime formDigestXmlFile = "qa_data/sharepoint/savedFormDigestValue.xml" If (fac.FileExists(formDigestXmlFile) = True) Then success = xml.LoadXmlFile(formDigestXmlFile) ' Get the expire date/time success = dtExpire.SetFromTimestamp(xml.GetChildContent("d:ExpireDateTime")) ' Get the current date/time success = dtNow.SetFromCurrentSystemTime() ' Get both times as Unix time values tNow = dtNow.GetAsUnixTime(False) tExpire = dtExpire.GetAsUnixTime(False) ' If tNow >= tExpire, then fall through. ' Otherwise, just use the cached digest value. If (tNow < tExpire) Then Debug.Print "Cached digest value is not yet expired." Debug.Print "X-RequestDigest: "; xml.GetChildContent("d:FormDigestValue") Exit Sub End If End If ' If we got to this point, the cached digest value either does not exist, or expired. Dim http As Chilkat.Http Set http = Chilkat.NewHttp ' If SharePoint Windows classic authentication is used, then set the ' Login, Password, LoginDomain, and NtlmAuth properties. http.Login = "SHAREPOINT_USERNAME" http.Password = "SHAREPOINT_PASSWORD" http.LoginDomain = "SHAREPOINT_NTLM_DOMAIN" http.NtlmAuth = 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. ' Note: The last argument ("utf-8") is meaningless here because the body is empty. Set resp = http.PostXml("https://SHAREPOINT_HTTPS_DOMAIN/_api/contextinfo","","utf-8") If (http.LastMethodSuccess <> True) Then Debug.Print http.LastErrorText Exit Sub End If If (resp.StatusCode <> 200) Then ' A response status code not equal to 200 indicates failure. Debug.Print "Response status code = "; resp.StatusCode Debug.Print "Response body:" Debug.Print resp.BodyStr Exit Sub End If success = xml.LoadXml(resp.BodyStr) ' 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. Dim xml2 As Chilkat.Xml Set xml2 = Chilkat.NewXml xml2.Tag = "savedFormDigestValue" xml2.NewChild2 "d:FormDigestValue",xml.GetChildContent("d:FormDigestValue") timeoutInSec = xml.GetChildIntValue("d:FormDigestTimeoutSeconds") Debug.Print "Timeout in seconds = "; timeoutInSec ' Convert this to an expire timestamp. ' Let's make it expire 30 seconds prior to the actual timeout, just to be safe. If (timeoutInSec > 30) Then timeoutInSec = timeoutInSec - 30 End If success = dtExpire.SetFromCurrentSystemTime() success = dtExpire.AddSeconds(timeoutInSec) xml2.NewChild2 "d:ExpireDateTime",dtExpire.GetAsTimestamp(False) ' Persist the digest and expire time to a file. success = xml2.SaveXml(formDigestXmlFile) Debug.Print "Here is the new form digest value:" Debug.Print "X-RequestDigest: "; xml.GetChildContent("d:FormDigestValue") |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.