2

I am trying to upgrade an old sharepoint project to the latest sharepoint framework. The client part is built with angular. As in spfx _spPageContextInfo is not available so I tired changing the code to

this.context.pageContext.web.absoluteUrl;

format but it does not work.

I have something like this in code

        $scope["proxyScriptTag"] = ["<script type='text/javascript' src='",
        $window["_spPageContextInfo"]["webAbsoluteUrl"],
        "/Scripts/test.js",
        "'></script>"] 
and
var appUrl = $window._spPageContextInfo.webServerRelativeUrl;

I tried changing it to

        $scope["proxyScriptTag"] = ["<script type='text/javascript' src='",
        this["context"]["pageContext"]["web"]["absoluteUrl"],
        "/Scripts/test.js",
        "'></script>"]
and
var appUrl = this.context.pageContext.web.absoluteUrl;

But I get the following error

Error: this.context is undefined

This is my first time working with SharePoint so I dont completely understand what is the issue. I think the context is not passed from the main ts file but I am not sure about it.

I am stuck on this problem so Any hint or idea would be helpful.

1

In your render method add something like

window['webPartContext']=this.context;

In the js file you can access the pageContext using something like

window.webPartContext.pageContext.web.absoluteUrl
1

spfx uses typescript. in typescript this context doesnt behave like in javascript. The reason it doesnt is javascript is not strongly typed where typescript is strongly typed (typesafe).

you could utilize the flat arrow method:

$(document).ready((n, m) => yourmethod.run(n, m));

explained here:

https://basarat.gitbooks.io/typescript/docs/arrow-functions.html

you can find possible solutions and workarounds here to get the javascript version of this

https://stackoverflow.com/questions/20627138/typescript-this-scoping-issue-when-called-in-jquery-callback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.