简体   繁体   中英

using spservices to search a list

I am trying to search a sharepoint list called mollyList. It has column name Food, Calories, weight... ect. I modified this sample code below, but I keep getting an error: POST /_vti_bin/Lists.asmx 500 (Internal Server Error)

And I have no idea what is the issue? Can someone check out my code?

 <!-- begin snippet: js hide: false --> 
 <body> <table><tbody><tr><td align="right">Calories</td> <td align="left"><input id="firstName" type="text"/></td></tr> <tr><td align="right">weight:</td> <td align="left"><input id="wt" type="text"/></td></tr> <tr><td align="right">weight:</td> <td align="left"><input id="wt2" type="text"/></td></tr></tbody></table> <p><input id="sb" type="button" value="Search"/> </p> <ul id="searchResults"></ul> </body> 

$(document).ready(function () {
$("#sb").click(function(){
    alert("this");
$("#searchResults").empty();
var query = "";
var key = "";

//Build Query from input
if($("#firstname").val()){
key = $("#firstname").val();
query = "<Query><Where><Or><Contains><FieldRef Name='Food'/><Value Type='Text'>"+ key +"</Value></Contains><Contains><FieldRef Name='calories'/><Value Type='Text'>"+ key +"</Value></Contains></Or></Where></Query>";
}

// Pass query to Function
if(query)
sendQuery(query);
else
$("#searchResults").append("Please enter atleast one value");

//If no results found
if (!$("#searchResults").html())
{
$("#searchResults").append("No Results Found");
}

});
});

function sendQuery(spQuery)
{
var thisSite = $().SPServices.SPGetCurrentSite();
//function to get all the lists
$().SPServices({
operation: "GetListItems",
async:false,
webURL: $(this).attr("/Lists/mollyList/AllItems.aspx"),
listName: $(this).attr("mollyList"),
CAMLQuery: spQuery,
CAMLRowLimit: 100,   
completefunc: function(xData, Status) {

}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<body>
<table><tbody><tr><td align="right">Look Up:</td>
<td align="left"><input id="firstname" type="text"/></td></tr>
</tbody></table>
<p><input id="sb" type="button" value="Search"/> </p>
<ul id="searchResults"></ul>
Generate SPQuery and pass to the function: 



</body>

Your sendQuery() function looks wrong to me, specifically these two lines:

webURL: $(this).attr("/Lists/mollyList/AllItems.aspx"),
listName: $(this).attr("mollyList"),

It's not clear what listName: $(this).attr("mollyList"), is achieving for you. If you already know that the list name is "mollyList" then the correct code should be listName: "mollyList",

If you're trying to access a list on a different site, then webURL should be the URL of the site on which the list resides, presumably in the following syntax: webUrl: "http://server.com/site/web",

The documentation for the GetListItems operation suggests that listName should be the display name of your list, while webURL is optional if you're executing the code on the site where the list resides.

Also note that if you're working with SharePoint 2010 or above, you also have the built-in JavaScript object model that you can work with. You can find Microsoft's documentation on the JavaScript client object model here: https://msdn.microsoft.com/en-us/library/hh185015(v=office.14).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM