Using Sharepoint Online 2013 building an app using JSOM.
This current part is after the submission of data to a list (Which is working correctly) a modal will open allowing users to create a PDF off that List and other Lists. The current issue is that the data from the list must be ready on the client side before I can begin making it into a PDF, I have read over the literature on the "Promise" method and that is what I have been trying recently with little success.
function getQuoteDetails() {
var d = $.Deferred(function(){
var ListName = "Quote";
var context = new SP.ClientContext.get_current();
var lstObject = context.get_web().get_lists().getByTitle(ListName);
var camlQuery = new SP.CamlQuery();
//var _valuetofind = $('#QuoteID').text();
var _valuetofind = '1';
camlQuery.set_viewXml("<View><ViewFields>" +
"<FieldRef Name='Q_ID' />" +
"<FieldRef Name='CPY_ID' />" +
"<FieldRef Name='CUST_ID' />" +
"<FieldRef Name='Q_RaiseDate' />" +
"<FieldRef Name='Q_DueDate' />" +
"<FieldRef Name='Q_Price' />" +
"<FieldRef Name='Q_GST' />" +
"<FieldRef Name='Q_TotalPrice' />" +
"<FieldRef Name='Q_PurchaseOrder' />" +
"</ViewFields>" +
"<Query><Where><Contains>" +
"<FieldRef Name='Q_ID'/>" +
"<Value Type='Number'>" + _valuetofind + "</Value></Contains></Where></Query></View>");
var listItemCollection = lstObject.getItems(camlQuery);
context.load(listItemCollection, "Include(Q_ID, CPY_ID, CUST_ID, Q_RaiseDate, Q_DueDate, Q_Price, Q_GST, Q_TotalPrice, Q_PurchaseOrder)");
context.executeQueryAsync(onGetItemsSuccess, onGetItemsFail);
function onGetItemsSuccess(sender, args) {
var listItemEnumerator = listItemCollection.getEnumerator();
while (listItemEnumerator.moveNext()) {
var quotearray = [];
quotearray['Q_ID'] = listItemEnumerator.get_current().get_item('Q_ID');
quotearray['CPY_ID'] = listItemEnumerator.get_current().get_item('CPY_ID');
quotearray['CUST_ID'] = listItemEnumerator.get_current().get_item('CUST_ID');
quotearray['Q_RasiedDate'] = listItemEnumerator.get_current().get_item('Q_RasiedDate');
quotearray['Q_DueDate'] = listItemEnumerator.get_current().get_item('Q_DueDate');
quotearray['Q_Price'] = listItemEnumerator.get_current().get_item('Q_Price');
quotearray['Q_GST'] = listItemEnumerator.get_current().get_item('Q_GST');
quotearray['Q_TotalPrice'] = listItemEnumerator.get_current().get_item('Q_TotalPrice');
quotearray['Q_PurchaseOrder'] = listItemEnumerator.get_current().get_item('Q_PurchaseOrder');
}
d.resolve(quotearray);
}
function onGetItemsFail(sender, args) {
alert('Failed to get items. Error: ' + args.get_message());
}
});
return d.promise();
}
This code is then called by:
function CreateQuotePDF() {
getQuoteDetails().done(function (quotearray) {
var quotedetails = quotearray;
alert('Nothing'); <---- quotedetails is undefined at this point
});
alert('Nothing'); <---- quotedetails is undefined at this point
}
Hope you can help out!
Some recommendations
make sure you are using jQuery version >= 1.5 since Deferred object
was introduced in jQuery 1.5
there is no need to specify CAML ViewFields and value for the second parameter of SP.ClientContext.load function at the same time since both of them are used for specifying what properties to retrieve
always prefer to include failed callback for SP.ClientContext.executeQueryAsync function in order to handle any occurred error
Having said that i would suggest you the below more generic method for getting list items:
function getListItems(listTitle,propertiesToInclude)
{
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getByTitle(listTitle);
var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
var includeExpr = 'Include(' + propertiesToInclude.join(',') + ')';
ctx.load(items,includeExpr);
var d = $.Deferred();
ctx.executeQueryAsync(function() {
var result = items.get_data().map(function(i){
return i.get_fieldValues();
});
d.resolve(result);
},
function(sender,args){
d.reject(args);
});
return d.promise();
}
In that case list items could be retrieved as shown below:
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
var listTitle = 'Documents'; //set list title here
var properties = ['Title','ID']; //specify items properties here
getListItems(listTitle,properties)
.done(function(items){
console.log(items);
})
.fail(function(error){
console.log(error.get_message()); //if any error is occurred?
});
});
Related
I am trying to link a list to a sharepoint website using javascript, the list is called 'Utilities Contract' and I want to bring back specific columns from that list. This is the code I have so far.
function retrieveSpecificListProperties(siteUrl) {
var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
this.collList = oWebsite.get_lists();
clientContext.load(collList, 'Include(Title, Id)');
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),
Function.createDelegate(this, this.onQueryFailed)
);
}
function onQuerySucceeded() {
var listInfo = '';
var listEnumerator = collList.getEnumerator();
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
listInfo += 'Title: ' + oList.get_title() +
' ID: ' + oList.get_id().toString() + '\n';
}
alert(listInfo);
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
You can use the below mentioned code to get that data:
var clientContext = new SP.ClientContext();
var oList = clientContext.get_web().get_lists().getByTitle('Utilities Contract');
var camlQuery = new SP.CamlQuery();
var collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(
function(){
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var itemTitle = oListItem.get_item('Title');
var itemId = oListItem.get_id();
},
function(sender, args){
console.log('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
});
More exhaustive samples - How to: Retrieve List Items Using JavaScript
I apologize up front for the possible lack of clarity for this question, but I'm new to Angular.js and my knowledge of it is still slightly hazy. I have done the Angular.js tutorial and googled for answers, but I haven't found any.
I have multiple select/option html elements, not inside a form element, and I'm populating them using AJAX. Each form field is populated by values from a different SharePoint list. I'm wondering if there is a way to implement this using Angular.js?
I would like to consider building this using Angular because I like some of it features such as data-binding, routing, and organizing code by components. But I can't quite grasp how I could implement it in this situation while coding using the DRY principle.
Currently, I have a single AJAX.js file and I have a Javascript file that contains an array of the different endpoints I need to connect to along with specific query parameters. When my page loads, I loop through the arrays and for each element, I call the GET method and pass it the end-point details.
The code then goes on to find the corresponding select element on the page and appends the option element returned by the ajax call.
I'm new to Angular, but from what I understand, I could create a custom component for each select element. I would place the component on the page and all the select and options that are associated with that component would appear there. The examples I've seen demonstrated, associate the ajax call with the code for the component. I'm thinking that I could use a service and have each component dependent on that service and the component would pass it's specific query details to the service's ajax call.
My current code - Program flow: main -> data retrieval -> object creation | main -> form build.
Called from index.html - creates the multiple query strings that are passed to ajax calls - ajax calls are once for each query string - the very last function in the file is a call to another function to build the form elements.
var snbApp = window.snbApp || {};
snbApp.main = (function () {
var main = {};
main.loadCount = 0;
main.init = function () {
function buildSelectOptions(){
//***
//Build select options from multiple SharePoint lists
//***
var listsArray = snbApp.splistarray.getArrayOfListsForObjects();
for(var i = 0; i < listsArray.length; i++){
var listItem = listsArray[i];
var qryStrng = listItem.list +
"?$select=" + listItem.codeDigits + "," + listItem.codeDescription + "," + listItem.ItemStatus + "&$orderby=" + listItem.codeDescription + "&$filter="+listItem.ItemStatus+" eq true" + "&$inlinecount=allpages"
var listDetails = {
listName: listItem.list,
listObj: listItem,
url: "http://myEnv/_vti_bin/listdata.svc/" + listItem.list +
"?$select=" + listItem.codeDigits + "," + listItem.codeDescription + "," + listItem.ItemStatus + "&$orderby=" + listItem.codeDescription + "&$filter="+listItem.ItemStatus+" eq true" + "&$inlinecount=allpages"
};
var clientContext = new SP.ClientContext.get_current();
clientContext.executeQueryAsync(snbApp.dataretriever.letsBuild(listDetails), _onQueryFailed);
}
//***
//Build select option from other API endpoint
//***
var listDetails = {
listName:"SNB_SecondaryActivityCodes",
url: "http://myEnv/requests/odata/v1/Sites?$filter=(IsMajor eq true or IsMinor eq true) and IsActive eq true and IsPending eq false and CodePc ne null and IsSpecialPurpose eq false&$orderby=CodePc"
};
snbApp.dataretriever.letsBuild(listDetails);
}
buildSelectOptions();
//***
//Add delay to populate fields to ensure all data retrieved from AJAX calls
//***
var myObj = setTimeout(delayFieldPopulate,5000);
function delayFieldPopulate(){
var optObj = snbApp.optionsobj.getAllOptions();
var osType = $("input[name=os_platform]:checked").val();
snbApp.formmanager.buildForm(osType, optObj);
}
};
function _onQueryFailed(sender, args) {
alert('Request failed.\nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
return main
})();
AJAX calls here - called from main/previous file:
var snbApp = window.snbApp || {};
snbApp.dataretriever = (function () {
var listsArray = snbApp.splistarray.getArrayOfListsForObjects();
function getListData(listItem) {
var eventType = event.type;
var baseURL = listItem.url;
$.ajax({
url: baseURL,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
}
})
.done(function(results){
snbApp.objectbuilderutility.buildObjectFields(results, listItem);
})
.fail(function(xhr, status, errorThrown){
//console.log("Error:" + errorThrown + ": " + myListName);
});
}
function _onQueryFailed(sender, args) {
alert('Request failed.\nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
return{
letsBuild:function(item) {
getListData(item);
}
};
})();
Builds a item name object - called from recursive AJAX calls / previous file
var snbApp = window.snbApp || {};
snbApp.objectbuilderutility = (function () {
function formatItemCode(itemCode, eventType){
if(eventType !== 'change'){ //for load event
var pattern = /^CE/;
var result = pattern.test(itemCode);
if(result){
return itemCode.slice(2);
}else{
return itemCode.slice(0,3);
}
}else{ //for change event
var pattern = /^CE/;
var result = pattern.test(itemCode);
if(result){
return itemCode.slice(2);
}else{
return itemCode.slice(3);
}
}
}
return{
buildObjectFields: function(returnedObj, listItem){ //results:returnedObj, prevItem:listItem
//***
//For SharePoint list data
//***
if (listItem.listName !== "SNB_SecondaryActivityCodes") {
var theList = listItem.listName;
var firstQueryParam = listItem.listObj.codeDigits;
var secondQueryParam = listItem.listObj.codeDescription;
var returnedItems = returnedObj.d.results;
var bigStringOptions = "";
//regex to search for SecondaryFunctionCodes in list names
var pattern = /SecondaryFunctionCodes/;
var isSecFunction = pattern.test(theList);
if(isSecFunction){
bigStringOptions = "<option value='0' selected>Not Applicable</option>";
}else{
bigStringOptions = "<option value='0' disabled selected>Select Option</option>";
}
$.each(returnedItems, function (index, item) {
var first = "";
var second = "";
for (var key in item) {
if (item.hasOwnProperty(key)) {
if (key != "__metadata") {
if (key == firstQueryParam) {
first = item[key];
}
if (key == secondQueryParam) {
second = item[key];
}
}
}
}
bigStringOptions += "<option value=" + first + " data-code=" + first + ">" + second + "</option>";
});
var str = theList.toLowerCase();
snbApp.optionsobj.updateFunctionOrActivity(theList.toLowerCase(), bigStringOptions);
//***
//For other API
//***
} else {
var theList = listItem.listName;
var bigStringOptions = "<option value='0' disabled selected>Select Option</option>";
var returnedItems = returnedObj.value;
for(var i = 0; i < returnedItems.length; i++){
var item = returnedItems[i];
//***
//change event type means the user selected a field
//***
if(listItem.eventType === "change"){
var siteCodeChange = item.SiteCodePc;
if (typeof siteCodeChange === "string" & siteCodeChange != "null") {
siteCodeChange = siteCodeChange < 6 ? siteCodeChange : siteCodeChange.slice(3);
}
bigStringOptions += "<option value='" + item.Id + "' data-code='" + siteCodeChange + "' data-isDivSite='" + item.IsDivisionSite + "' data-isDistSite='" + item.IsDistrictSite + "' data-divID='" + item.DivisionSiteId + "' data-distID='" + item.DistrictSiteId + "'>(" + siteCodeChange + ") " + item.Name + "</option>";
snbApp.formmanager.buildSelectSiteLocations(bigStringOptions);
//***
//load event which means this happens when the page is loaded
//***
}else{
var siteCodeLoad = item.SiteCodePc;
if (typeof siteCodeLoad === "string" & siteCodeLoad != "null") {
var siteCodeLoad = siteCodeLoad.length < 4 ? siteCodeLoad : siteCodeLoad.slice(0, 3);
}
bigStringOptions += "<option value='" + item.Id + "' data-code='" + siteCodeLoad + "' data-isDivSite='" + item.IsDivisionSite + "' data-isDistSite='" + item.IsDistrictSite + "' data-divID='" + item.DivisionSiteId + "' data-distID='" + item.DistrictSiteId + "'>(" + siteCodeLoad + ") " + item.Name + "</option>";
snbApp.optionsobj.updateFunctionOrActivity(theList.toLowerCase(), bigStringOptions);
}
}
}
}
};
})();
Form management - called from previous file, gets all select elements on page and appends items from the object in previous file to each select element.
var snbApp = window.snbApp || {};
//Direct interface to the form on the page
snbApp.formmanager = (function(){
var form = {};
form.content_holder = document.getElementById("content_holder");
form.sec_act_codes = document.getElementById("snb_secondary_activity_codes");
form.prim_func_codes = document.getElementById("snb_primary_function_codes");
form.sec_func_codes = document.getElementById("snb_secondary_function_codes");
form.sec_func_nums = document.getElementById("snb_secondary_function_numbers");
form.host_options = document.getElementById("snb_host_options");
form.site_locs_div = document.getElementById("site_locations_div");
form.site_locs = document.getElementById("snb_site_locations");
form.dc_or_off_prem_div = document.getElementById("dc_or_off_premise_div");
form.dc_off_prem_codes = document.getElementById("snb_dc_offpremise_codes");
var snb_secondary_activity_codes = "";
var snb_primary_function_codes = "";
var snb_secondary_function_codes = "";
var snb_secondary_function_numbers = "";
var snb_host_options = "";
var snb_site_locations = "";
var snb_dc_op = "";
//builds the server location hosting options selection
function buildLocationTypeSelector() {
var locationOptionsString = "<option value='0' disabled selected>Select Option</option>";
for (var i = 0; i < locationOptions.length; i++) {
var location = locationOptions[i];
locationOptionsString += "<option value=" + location.hostLocale + " data-code=" + location.code + ">" + location.hostLocale + "</option>";
}
$("#snb_host_options").append(locationOptionsString);
}
function buildSiteLocations(bigString){
if(bigString === undefined){
var siteLocs = document.getElementById("snb_site_locations");
var newOption = document.createElement("option");
newOption.setAttribute("value", 0);
newOption.setAttribute("disabled","disabled");
newOption.setAttribute("checked","checked");
var newText = document.createTextNode("Select Option");
newOption.appendChild(newText);
siteLocs.appendChild(newOption);
} else{
var siteLocs = document.getElementById("snb_site_locations");
siteLocs.innerHTML = bigString;
}
}
return {
form:form,
buildSelectSiteLocations: function(bigString){
buildSiteLocations(bigString);
},
buildForm: function (osType, optObj) {
buildLocationTypeSelector();
buildSecondaryFunctionNumberSelector();
buildSiteLocations();
if(osType === 'windows'){
$("#snb_secondary_activity_codes").append(optObj.windows.secondary_activity);
$("#snb_primary_function_codes").append(optObj.windows.primary_function);
$("#snb_secondary_function_codes").append(optObj.windows.secondary_function);
$("#snb_site_locations").append(optObj.windows.site_location);
$("#snb_dc_offpremise_codes").append(optObj.windows.dc_offpremise);
}else{
$("#snb_secondary_activity_codes").append(optObj.unix.secondary_activity);
$("#snb_primary_function_codes").append(optObj.unix.primary_function);
$("#snb_secondary_function_codes").append(optObj.unix.secondary_function);
$("#snb_site_locations").append(optObj.unix.site_location);
$("#snb_dc_offpremise_codes").append(optObj.unix.dc_offpremise);
}
}
};
})();
Thanks in advance.
I am trying to download an excel file on onclick event. It allow me to download excel file in chrome but not in Firefox. Not sure what is the issue. I am getting below error.
ReferenceError: event is not defined
Below is my javascript function
function hyperlinkFn(){
var htmlTbl="";
var htmlTbl1="";
var string = event.target.parentElement.name;
var tempString=[];
var temp= new Array();
for(var i=0;i<string.length;i++){
if(string[i]=="|"){
temp.push(tempString);
tempString=[];
}else{
tempString+=string[i];
}
}
userStoryModalRelease = temp[0];
userStoryModalPID = temp[1];
userStoryModalPrjName = encodeURIComponent(temp[2]);
userStoryModalTeam = encodeURIComponent(temp[3]);
userStoryAltId=encodeURIComponent(temp[4]);
userStoryModalStatus = temp[5];
userStoryModalTeam = userStoryModalTeam.replace(", ", ",");
var uri="getUserStoryDetails.htm?release=" + userStoryModalRelease + "&projectId=" + userStoryModalPID + "&projectName=" + userStoryModalPrjName +
"&team=" + userStoryModalTeam + "&alternateId=" + userStoryAltId + "&view=" + storyView;
var encode = encodeURI(uri);
window.location = encode;
}
HTML code where I am calling this function in the datatable
{
title : "Total Points",
data : function(data, type, row, meta) {
return data.totalSum+'<a name="'+ data.releaseNum +'|'+ data.projectId +'|'+ data.projectName +'|'+ data.team + '|'+ data.altId + '|total|'+'" onclick="hyperlinkFn()">'
+ '<i class="fa fa-file-excel-o font-size-15 " title="Click here to export details " aria-hidden="true"></i></a>';
},
className : "center storypoint yellow_fill pos-rel"
} ],
Change your click binding like below so that event will work:
$(document).on('click', '.classname', function(event){
var htmlTbl="";
var htmlTbl1="";
var string = event.target.parentElement.name;
var tempString=[];
var temp= new Array();
for(var i=0;i<string.length;i++){
if(string[i]=="|"){
temp.push(tempString);
tempString=[];
}else{
tempString+=string[i];
}
}
userStoryModalRelease = temp[0];
userStoryModalPID = temp[1];
userStoryModalPrjName = encodeURIComponent(temp[2]);
userStoryModalTeam = encodeURIComponent(temp[3]);
userStoryAltId=encodeURIComponent(temp[4]);
userStoryModalStatus = temp[5];
userStoryModalTeam = userStoryModalTeam.replace(", ", ",");
var uri="getUserStoryDetails.htm?release=" + userStoryModalRelease + "&projectId=" + userStoryModalPID + "&projectName=" + userStoryModalPrjName +
"&team=" + userStoryModalTeam + "&alternateId=" + userStoryAltId + "&view=" + storyView;
var encode = encodeURI(uri);
window.location = encode;
})
Note: remove onclick event from hyperlink and add class on hyperlink and replace that classname with .classname
Looks like this has already been covered here:
ReferenceError: event is not defined error in Firefox
WebKit follows IE's old behavior of using a global symbol for "event", but
Firefox doesn't. When you're using jQuery, that library normalizes the
behavior and ensures that your event handlers are passed the event
parameter.
function hyperlinkFn(){ needs to become function hyperlinkFn( event ){
I'm trying to get every single item from all lists within a SharePoint site. All of the docs and answered questions I've found in order to do this usually explain how to get all lists or all items from one list, but not every item in all lists within a site context.
I have my code below and I am able to get all lists fine, my biggest struggle is getting the items and not only from the last list (for some reason, it kept on doing this as I tested in the console - this version would probably just produce null errors instead since I made changes from before).
var allInfo = "";
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllListsAndItems);
function getAllListsAndItems() {
var context = new SP.ClientContext('https://mysites.sprcompanies.com/personal/cnorman/charpractice/');
var web = context.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded(sender, args) {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var list = listEnumerator.get_current();
allInfo += " List: " + list.get_title() + "\n";
if (list.get_itemCount() > 0) {
var query = new SP.CamlQuery();
query.set_viewXml('<View></View>');
var items = list.getItems(query);
context.load(items);
context.executeQueryAsync(onSuccess, onFail);
function onSuccess(sender, args) {
var itemsEnumerator = items.getEnumerator();
while (itemsEnumerator.moveNext()) {
var item = itemsEnumerator.get_current();
}
}
function onFail(sender, args) {
console.log('Request on items failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
}
console.log(allInfo);
}
function onQueryFailed(sender, args) {
console.log('Request on lists failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
I know the general problem area is here:
var itemsEnumerator = items.getEnumerator();
while (itemsEnumerator.moveNext()) {
var item = itemsEnumerator.get_current();
}
I originally had it append to allInfo, but all it does is produce 'not initialized' errors. I first thought that I just wasn't loading the items properly, but after testing it in the console, it does display the item collection objects so that's why I think it has something to do with the above.
Couldn't I just use a for loop to cycle through the items instead? I only need the titles of each item. I tried a for and a for in, but it again results in errors. So it's really how I'm accessing each item (using the wrong properties). Thank you in advance!
Edit:
So I put this in the item onSuccess block instead:
if (items.get_item("Title") == null) {
items.get_data().forEach(function(item) {
console.log(item.get_item('URL').get_description());
});
}
else {
items.get_data().forEach(function(item) {
console.log(item.get_item('Title'));
});
}
Both would get the 'title' of an item whether it's a regular item or a link item - the problem is that it only get the items of the last list and repeats those multiple times instead of going through every list.
For those interested in how I got the answer:
var allInfo = "";
var listsArray = [];
var itemsArray = [];
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllListsAndItems);
function getAllListsAndItems() {
var context = new SP.ClientContext(SiteURL);
var web = context.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(onQuerySuccess, onQueryFailure);
function onQuerySuccess(sender, args) {
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var list = listEnumerator.get_current();
listsArray.push(list.get_title());
}
for (var i = 0; i < listsArray.length; i++) {
var query = new SP.CamlQuery();
query.set_viewXml('<View></View>');
itemsArray[i] = lists.getByTitle(listsArray[i]).getItems(query);
itemsArray.push(itemsArray[i]);
context.load(itemsArray[i]);
}
context.executeQueryAsync(onSuccess, onFailure);
function onSuccess(sender, args) {
for (var i = 0; i < itemsArray.length; i++) {
if (listsArray[i] != "Master Page Gallery") {
allInfo += " List: " + listsArray[i] + "\n";
itemsArray[i].get_data().forEach(function(item) {
if (item.get_item("Title") == null) {
allInfo += " \t Item: " + item.get_item('URL').get_description() + "\n";
}
else if (item.get_item("Title") != null) {
allInfo += " \t Item: " + item.get_item("Title") + "\n";
}
else {
console.log("Something's wrong with this one.");
}
});
}
}
console.log(allInfo);
}
function onFailure(sender, args) {
console.log('Request on items failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
function onQueryFailure(sender, args) {
console.log('Request on lists failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
So basically I had to push every list that was loaded into an array then use that to load items from each one at a time because originally looping through it did not work since it only picked up the last list. This would produce a structure like this:
List
Item
Item
...
List
Item
Item
...
...
I am trying to retrieve data from a RSS feed (succeeding with this part) and then using the description, title and caption to be used to get Geo information (latitude, longitude, woeId) using YQL Placemaker open datatable.
This is all then needed to be output as JSON.
The permalink to test it in the YQL console is here.
Any ideas whats wrong with my xml code or what should I try instead?
<meta>
<author>Yahoo! Inc.</author>
<documentationURL>http://i-magine.mobi/</documentationURL>
<sampleQuery>select * from {table} where section="topstories" and description matches ".*jpg.*"</sampleQuery>
<description>Searches Yahoo.news RSS</description>
</meta>
<bindings>
<select itemPath="" produces="XML">
<urls>
<url>http://rss.news.yahoo.com/rss/topstories/{section}
</url>
</urls>
<inputs>
<key id='section' type="xs:string" paramType="path" required="true" />
</inputs>
<execute><![CDATA[
default xml namespace = "http://where.yahooapis.com/v1/schema.rng";
// http://www.json.org/json2.js
y.include('http://www.i-magine.mobi/json2.js');
rssStorySection = [];
rssStoryNumber = [];
rssStoryTitle = [];
rssStorySummary = [];
rssImageCaption = [];
rssStoryUrl = [];
rssImageUrl = [];
rssGeoText = [];
// var content = 'They followed him to deepest Africa after Brussels and Tokyo and found him there in Timbuktu';
var rssQuery = 'select * from rss where url = ' + "'" + 'http://rss.news.yahoo.com/rss/' + section + "'" + ' and description matches ".*jpg.*" limit 30';
res1 = y.query(rssQuery);
data1 = res1.results;
// var geoQuery = 'SELECT * FROM geo.placemaker WHERE documentContent =' + '"' + content + '"' + 'AND documentType=' + '"' + 'text/plain' + '"';
// res2 = y.query(geoQuery);
// data2 = res2.results;
for (var c=0;c<data1.item.length;c++)
{
var story = data1.item[c];
var storyTitleText0 = story.title + "";
var storyUrl = story.link + "";
var description = story.description;
var storyTitleText = storyTitleText0.replace(/\n/ig, "") + "";
var imageUrl = description.match(/http:\/\/d.*?jpg/i) + "";
var imageCaptionText0 = description.match(/alt="([^ ]).*border/) + "";
var imageCaptionText1 = imageCaptionText0.replace(/alt="/ig, "") + "";
var imageCaptionText = imageCaptionText1.replace(/" border.*/ig, "") + "";
var storySummaryText = description.replace(/<[^>]*>([\s]?)*<[^>]*>/g, "") + "";
var storySection0 = description.match(/http[^ ].*\*/i) + "";
var storySection1 = storySection0.replace(/\/\*/ig, "") + "";
var storySection = storySection1.replace(/http:\/\/us.rd.yahoo.com\/dailynews\/rss\//ig, "") + "";
var geoString = (imageCaptionText + " " + storyTitleText + " " + storySummaryText);
rssStorySection.push(storySection);
rssStoryTitle.push(storyTitleText);
rssStorySummary.push(storySummaryText);
rssImageCaption.push(imageCaptionText);
rssStoryUrl.push(storyUrl);
rssImageUrl.push(imageUrl);
rssGeoText.push(geoString);
rssStoryNumber.push(c);
var content = geoString;
var geoQuery = 'SELECT * FROM geo.placemaker WHERE documentContent =' + '"' + content + '"' + 'AND documentType=' + '"' + 'text/plain' + '"';
var res2 = y.query(geoQuery);
var data2 = res2.results;
}
var d = data1;
var e = data2;
response.object = <stories>
<c>{section}</c>
<d>{d}</d>
<e>{e}</e>
</stories>;
]]></execute>
</select>
</bindings>
</table>
A tidied up (and "working" in the sense that it brings back RSS+Placemaker results) version of your table looks like:
<?xml version="1.0" encoding="UTF-8" ?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>Peter Cowburn</author>
<documentationURL>http://stackoverflow.com/questions/6168564/creating-a-yql-opentable-to-combine-rss-data-with-placemaker-map-info</documentationURL>
<sampleQuery>select * from {table} where section='topstories'</sampleQuery>
<description>Searches Yahoo! News RSS and injects Placemaker Places</description>
</meta>
<bindings>
<select itemPath="stories.story" produces="XML">
<urls>
<url>
http://rss.news.yahoo.com/rss/{section}
</url>
</urls>
<inputs>
<key id="section" type="xs:string" paramType="path" required="true" />
</inputs>
<execute><![CDATA[
// Fetch top 30 feed items with jpg images
var feed = y.query(
'select * from rss where url=#url and description matches ".*jpg.*" limit 30',
{url: request.url}
).results;
// Build geo queries
var placeQuery = 'select * from geo.placemaker where documentContent=#text and documentType="text/plain"';
var placeQueries = [];
var title, description, caption, summary, content;
for each (result in feed.item) {
title = result.title.text().toString();
description = y.tidy(result.description.toString()).body.p;
caption = description.a.img.#alt.toString();
summary = description..*.text().toString();
content = caption + " " + title + " " + summary;
placeQueries.push({
query: y.query(placeQuery, {text: content}),
item: result,
results: null
});
}
// Execute all queries
var where = new Namespace('http://wherein.yahooapis.com/v1/schema');
var matches, match, places = [];
for (var q in placeQueries) {
matches = placeQueries[q].query.results.matches.match;
placeQueries[q].results = matches..where::place;
}
// Build response object
var stories = <stories/>;
for each (q in placeQueries) {
stories.node += <story>
{q.item}
{q.results}
</story>;
}
response.object = stories;
]]></execute>
</select>
</bindings>
</table>
You can use it by pointing to the table online (it may not be around forever!) in a query like:
use 'https://raw.github.com/salathe/yql-tables/so-6168564/yahoo/newswithplaces.xml'
as rssplaces;
select * from rssplaces where section='topstories';
(Try this in the YQL console)
The table uses some of the features available in <execute> blocks like E4X, query parameters and parallel queries which all make life easier but may be slightly foreign at first glance.
P.S. The above is offered as-is, I'm not going to be bending over backwards to field "support" questions on this. It is primarily intended as something to get you moving, an introduction to an approach which might work for you.