PowerBuilder
PowerBuilder
SharePoint -- Get Server Form Digest Value
Demonstrates 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.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Fac
oleobject loo_Xml
oleobject loo_DtExpire
oleobject loo_DtNow
string ls_FormDigestXmlFile
integer li_TNow
integer li_TExpire
oleobject loo_Http
string ls_SavedAccept
oleobject loo_Resp
oleobject loo_Xml2
integer li_TimeoutInSec
li_Success = 0
// 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.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
if li_rc < 0 then
destroy loo_Fac
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
// 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>
//
loo_DtExpire = create oleobject
li_rc = loo_DtExpire.ConnectToNewObject("Chilkat.CkDateTime")
loo_DtNow = create oleobject
li_rc = loo_DtNow.ConnectToNewObject("Chilkat.CkDateTime")
ls_FormDigestXmlFile = "qa_data/sharepoint/savedFormDigestValue.xml"
if loo_Fac.FileExists(ls_FormDigestXmlFile) = 1 then
loo_Xml.LoadXmlFile(ls_FormDigestXmlFile)
// Get the expire date/time
loo_DtExpire.SetFromTimestamp(loo_Xml.GetChildContent("d:ExpireDateTime"))
// Get the current date/time
loo_DtNow.SetFromCurrentSystemTime()
// Get both times as Unix time values
li_TNow = loo_DtNow.GetAsUnixTime(0)
li_TExpire = loo_DtExpire.GetAsUnixTime(0)
// If tNow >= tExpire, then fall through.
// Otherwise, just use the cached digest value.
if li_TNow < li_TExpire then
Write-Debug "Cached digest value is not yet expired."
Write-Debug "X-RequestDigest: " + loo_Xml.GetChildContent("d:FormDigestValue")
destroy loo_Fac
destroy loo_Xml
destroy loo_DtExpire
destroy loo_DtNow
return
end if
end if
// If we got to this point, the cached digest value either does not exist, or expired.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
// If SharePoint Windows classic authentication is used, then set the
// Login, Password, LoginDomain, and NtlmAuth properties.
loo_Http.Login = "SHAREPOINT_USERNAME"
loo_Http.Password = "SHAREPOINT_PASSWORD"
loo_Http.LoginDomain = "SHAREPOINT_NTLM_DOMAIN"
loo_Http.NtlmAuth = 1
// 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.
ls_SavedAccept = loo_Http.Accept
loo_Http.Accept = "application/xml"
// Note: The last argument ("utf-8") is meaningless here because the body is empty.
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpStr("POST","https://SHAREPOINT_HTTPS_DOMAIN/_api/contextinfo","","utf-8","application/xml",loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Fac
destroy loo_Xml
destroy loo_DtExpire
destroy loo_DtNow
destroy loo_Http
destroy loo_Resp
return
end if
// Restore the default Accept header
loo_Http.Accept = ls_SavedAccept
if loo_Resp.StatusCode <> 200 then
// A response status code not equal to 200 indicates failure.
Write-Debug "Response status code = " + string(loo_Resp.StatusCode)
Write-Debug "Response body:"
Write-Debug loo_Resp.BodyStr
destroy loo_Fac
destroy loo_Xml
destroy loo_DtExpire
destroy loo_DtNow
destroy loo_Http
destroy loo_Resp
return
end if
loo_Xml.LoadXml(loo_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.
loo_Xml2 = create oleobject
li_rc = loo_Xml2.ConnectToNewObject("Chilkat.Xml")
loo_Xml2.Tag = "savedFormDigestValue"
loo_Xml2.NewChild2("d:FormDigestValue",loo_Xml.GetChildContent("d:FormDigestValue"))
li_TimeoutInSec = loo_Xml.GetChildIntValue("d:FormDigestTimeoutSeconds")
Write-Debug "Timeout in seconds = " + string(li_TimeoutInSec)
// Convert this to an expire timestamp.
// Let's make it expire 30 seconds prior to the actual timeout, just to be safe.
if li_TimeoutInSec > 30 then
li_TimeoutInSec = li_TimeoutInSec - 30
end if
loo_DtExpire.SetFromCurrentSystemTime()
loo_DtExpire.AddSeconds(li_TimeoutInSec)
loo_Xml2.NewChild2("d:ExpireDateTime",loo_DtExpire.GetAsTimestamp(0))
// Persist the digest and expire time to a file.
loo_Xml2.SaveXml(ls_FormDigestXmlFile)
Write-Debug "Here is the new form digest value:"
Write-Debug "X-RequestDigest: " + loo_Xml.GetChildContent("d:FormDigestValue")
destroy loo_Fac
destroy loo_Xml
destroy loo_DtExpire
destroy loo_DtNow
destroy loo_Http
destroy loo_Resp
destroy loo_Xml2