You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/spfx/web-parts/basics/working-with-requestdigest.md
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
1
# Working with the original __RequestDigest
2
2
3
-
There is a lot of code written to work with the classic SharePoint pages that you can use with the SharePoint Framework, but sometimes certain components or variables aren't there. One example is the __REQUESTDIGEST form field. In an ideal world, you wouldn't use a global variable to access the digest, you'd just use the updated **HttpRequest** object to make your SharePoint call, and it will handle all the digest / auth logic for you (including things like expired tokens). The [Connect your client-side web part to SharePoint (Hello world part 2)](https://github.com/SharePoint/sp-dev-docs/wiki/HelloWorld,-Talking-to-SharePoint) article shows you how to do this.
3
+
There is a lot of code written to work with classic SharePoint pages that you can use with the SharePoint Framework, but sometimes certain components or variables aren't there. One example is the `__REQUESTDIGEST` form field. In an ideal world, you wouldn't use a global variable to access the digest, you'd just use the updated `HttpRequest` object to make your SharePoint call, and it will handle all the digest/auth logic for you (including things like expired tokens). The [Connect your client-side web part to SharePoint (Hello world part 2)](https://dev.office.com/sharepoint/docs/spfx/web-parts/get-started/connect-to-sharepoint) article demonstrates how to do this.
4
4
5
-
However, if your existing code uses some older constructs, through the power of clientside code and DOM manipulation, it's fairly easy to add these back to a page. The key is to hook into the **onInit** method in the base web part class, and pull the create the DOM element that you expect to be there. Here's an example that creates the __REQESTDIGEST form element.
5
+
However, if your existing code uses some older constructs, through the power of client-side code and DOM manipulation, it's fairly easy to add these back to a page. The key is to hook into the `onInit` method in the base web part class and create the DOM element that you expect to be there. Here's an example that creates the `__REQUESTDIGEST` form element:
6
6
7
7
```JavaScript
8
8
public onInit<T>():Promise<T>
9
9
{
10
10
// does the digest exist?
11
11
if ( !document.getElementById('__REQUESTDIGEST') )
12
12
{
13
-
// OK, the request digest does not exist. Let's create it.
13
+
// OK, the request digest does not exist. Let's create it.
14
14
// first, grab the digest value out of the contextWebInfo object (if it exists).
15
15
var digestValue: string;
16
16
try{
17
17
digestValue = (window as any)._spClientSidePageContext.contextWebInfo.FormDigestValue;
18
18
}
19
19
catch (exception){
20
-
// there is no digest on this page, so just return. This can easily happen on the local workbench
20
+
// there is no digest on this page, so just return. This can easily happen on the local workbench
21
21
returnPromise.resolve();
22
22
}
23
23
24
24
if (digestValue){
25
-
// OK, now lets create the digest input form. It looks like this -
25
+
// OK, now lets create the digest input form. It looks like this:
26
26
// <input type="hidden" name="__REQUESTDIGEST" id="__REQUESTDIGEST" value="blahblahblahblahblahblah, July23 -0000 or something like that">
@@ -40,12 +40,12 @@ However, if your existing code uses some older constructs, through the power of
40
40
}
41
41
```
42
42
43
-
>**Note:** There is a better way to get the current digest value that will handle all of the caching / expiring / refetching / etc. Give this a try. You'll need to import digestCacheServiceKey and IDigestCache from sp-client-base
43
+
>**Note:** There is a better way to get the current digest value that will handle all of the caching, expiring, refetching, etc. Give this a try (You'll need to import `digestCacheServiceKey` and `IDigestCache` from **sp-client-base**):
44
44
45
45
```JavaScript
46
46
var digestCache:IDigestCache =this.context.serviceScope.consume(digestCacheServiceKey);
0 commit comments