Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Getting errors when trying to use SP.ClientContext in an Azure function #13

Open
karthikramasubramanian opened this issue Jun 1, 2021 · 0 comments
Labels

Comments

@karthikramasubramanian
Copy link

Hi Guys,
I am following the Andrei Markeev article - (https://markeev.com/posts/jsom-azure-function/), and using csom-node in my azure function to connect with sharepoint. the AuthenticationContext method is successful and able to connect to sharepoint site using appid and appsecret. But after that in the line where we are creating the clientContext using new SP.ClientContext("url"), i am getting the error unable to find ClientContext of undefined. In all the example code, the SP object is used without any imports or requires entry. Can you guys please advise how i can resolve the issue?
I tries to use an import statement
import {SP} from "csom-node";
if i use the statement, i am able to build and debug the function. (I am using VS Code). When i debug, I am able to see the SP object and if expanded, i can also see the ClientContext function under that. but if i continue the execution, the error
Cannot read property 'ClientContext' of undefined

const csomApi = require("csom-node");
const AuthenticationContext = require('csom-node').AuthenticationContext;


csomapi.setLoaderOptions({url: settings.url});  //set CSOM library settings

var authCtx = new AuthenticationContext(settings.url);
authCtx.acquireTokenForUser(process.env.username, settings.password, function (err, data) {
    
    **var ctx = new SP.ClientContext("/");  //set root web**
    authCtx.setAuthenticationCookie(ctx);  //authenticate
    
    //retrieve SP.Web client object
    var web = ctx.get_web();
    ctx.load(web);
    ctx.executeQueryAsync(function () {
    console.log(web.get_title());
},
function (sender, args) {
    console.log('An error occured: ' + args.get_message());
});

});

@vgrem vgrem added the question label Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants