Unable to figure out pnp.sp.web

I have been trying to use the pnp.sp.web object to query a table. This is my code:

	var web = pnp.sp.web;
let items = sp.web.lists.getByTitle("Question Categories").items.getAll();
console.log(items);
for (let i = 0; i < items.length; i++) {
	console.log(items.results[i].Title);
}

When I log to the console the items object it is of type Promise and has an Array of my 33 records. However, nothing I do with that object works. I can't get the length of the array, or any of the field values, yet I can see them in the object in console.
The pnpjs help file examples do not work for example:

sp.web.lists.getByTitle("My List").items.get().then((items: any[]) => {
console.log(items);
});

This returns an error with "Error in custom JS:SyntaxError: Unexpected token :"

I should note that the Promise I get back from the sp.web.lists.getBytitle.items.getAll() has the word next to the Promise in the console. When I expand the object I get a notice that "Value below was evaluated just now".

Hello @smithme,

Please use the below code to display in the console items array, array length and the Title of the first item in the array.

sp.web.lists.getByTitle("ListName").items.get().then(function(items) {
                //items array
                console.log(items);

                //items count
                console.log(items.length);

                //Title of the first item in the array
                console.log(items[0].Title);
            });
2 Likes

Thank you! This is exactly what I needed.

1 Like

Since sp.web.lists.getByTitle (...). items.select (...). filter (...). get (...). then (...) it is not a function, how could I get a result out of it (for example items.length). I tried a global variable, but it didn't work

fd.spRendered(function() {
window.globalVariable = 0;
sp.web.lists.getByTitle("DRUO_LSHEET").items.get().then(function(items) {
//items count
globalVariable=items.length;
console.log("inside of sp.web.lists.getByTitle: " + globalVariable);
}); //sp.web.lists.getByTitle
console.log("outside of sp.web.lists.getByTitle: " + globalVariable);
});// fd.spRendered

In the console I have the following result:
outside of sp.web.lists.getByTitle: 0
inside of sp.web.lists.getByTitle: 9

As you can see I did not manage to read outside the sp.web.lists.getByTitle value obtained inside and interesting is that the order of the results in the console is reversed.
Can you help me make the sp.web.lists.getByTitle block work as a function?
Thanks

Hello @Cumatale,

The pnp functions are running async. The console.log("outside of sp.web.lists.getByTitle: " + globalVariable); runs before the function finishes and so it is equal 0.
You can add .then() to keep code running in order, like this:

fd.spRendered(function() {
	window.globalVariable = 0;
	sp.web.lists.getByTitle("DRUO_LSHEET").items.get().then(function(items) {
		//items count
		globalVariable=items.length;
		console.log("inside of sp.web.lists.getByTitle: " + globalVariable);
    }).then(function() {
	    console.log("outside of sp.web.lists.getByTitle: " + globalVariable);}); 
});

but .(then) is also a kind of inside pnp

fd.spRendered(function() {
globalVariable = 0;
sp.web.lists.getByTitle("DRUO_LSHEET").items.get().then(function(items) {
//items count
globalVariable=items.length;
console.log("inside sp.web.lists.getByTitle: " + globalVariable);
}).then(function() {
console.log("after then: " + globalVariable);});

	console.log("outside of sp.web.lists.getByTitle: " + globalVariable);

});

Console:
outside of sp.web.lists.getByTitle: 0
inside sp.web.lists.getByTitle: 9
after then: 9

I need to get value outside pnp to create a function, something like this:

window.CAML1eq1 = function (ListName, FilterField, FilterValue, ScopeField) {
var camlF=FilterField + " eq '" + FilterValue + "'";
console.log(camlF);
window.functionRes = "functionRes";
sp.web.lists.getByTitle(ListName).items.select(ScopeField).filter(camlF).get().then(function(items) {
if (items.length > 0) {
console.log("INSIDE - The first result is: " + items[0][ScopeField]);
functionRes= items[0][ScopeField];
} //if
else {
functionRes= "INSIDE - There are not results";
} //else
}).then(function() {
console.log("AFTER THEN - The function Results is: " + functionRes);
return functionRes;
});//sp.web
} // function CAML

fd.spRendered(function() {
	var a = CAML1eq1 ("DRUO_LSHEET", "TNUMB", "6463", "OFFICECODE");
	console.log("a: " + a);
	var b= CAML1eq1 ("DRUO_OFFICES", "OFFICECODE", a, "SUPERIORID");
	console.log("b: " + b);
	var c= CAML1eq1 ("DRUO_OFFICES", "OFFICEID", b, "OFFICECODE");
	console.log("c: " + c);

});

Console:
TNUMB eq '6463'
a: undefined
OFFICECODE eq 'undefined'
b: undefined
OFFICEID eq 'undefined'
c: undefined
INSIDE - The first result is: 805381
AFTER THEN - The function Results is: 805381
AFTER THEN - The function Results is: INSIDE - There are not results

Hello @Cumatale,

With .then() you specify the order in which the function executed. All the code is outside of PNP and executed after PNP function is completed.

fd.spRendered(function() {
	window.globalVariable = 0;
	sp.web.lists.getByTitle("DRUO_LSHEET").items.get().then(function(items) {
		//items count
		globalVariable=items.length;
		console.log("inside of sp.web.lists.getByTitle: " + globalVariable);
    }).then(function() {
	    //call or create the function here
            yourFunction();
});

Hi,

For those who have to receive synchronously data from SharePoint list by JavaScript:

function getCurrentUserRoles() {  
 var userRoles=[];
 var currentUserTitle= _spPageContextInfo.userDisplayName; 
 $.ajax({  
    url: "http://intranet/docflow/_api/lists/getbytitle('Config_Procese')/Items?$select=Roluri&$filter=Title eq 'Subiecte CCD'",  
    type: "GET",
    cache: true,
    async: false, 
    headers: {  
        "accept": "application/json;odata=verbose",  
    },  
    success: function(data) {
        var jsonArray = JSON.parse(data.d.results[0].Roluri);
        if (jsonArray.length>0) {
            for (var i = 0; i < jsonArray.length; i++) { 
                var jsonRow = jsonArray[i];
                 // check users from role
                 if (jsonRow.User.length > 0) { 
                       var roleUsers = jsonRow.User.split(";");
                      // check each user 
                       for (var j = 0; j < roleUsers.length; j++) {
                             // check if user from list match with current user
                              if (currentUserTitle == roleUsers[j]) {
                                    userRoles.push(jsonRow.Rol);
                                    break;
                             }
                        }
                  }
            }
         }                                 
    },  
    error: function(error) {  
        alert(error.responseText);
    }  
});
return userRoles;

}

2 Likes