CSOMNode icon indicating copy to clipboard operation
CSOMNode copied to clipboard

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

Open karthikramasubramanian opened this issue 4 years ago • 0 comments

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());
});
  

});

karthikramasubramanian avatar Jun 01 '21 21:06 karthikramasubramanian