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:
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".
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);
});
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
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
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:
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
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;