Guest User

Untitled

a guest
Jun 22nd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. var CmalQuery="";
  2. var fieldName="";
  3. $(document).ready(function () {
  4.  
  5. $('#btnSearch').on('click', function () {
  6. txtValue = $('#txtSearch').val();
  7. fn();
  8. });
  9. $('#btnList').on('click', function () {
  10. fn1();
  11. });
  12. $('select').on('change', function () {
  13. ListName = this.value;
  14. retrieveFieldsOfListView(ListName);
  15. txtValue = $('#txtSearch').val();
  16. // or $(this).val()
  17. });
  18. });
  19.  
  20.  
  21.  
  22. function fn() {
  23. var context = SP.ClientContext.get_current();
  24. var user = context.get_web().get_currentUser();
  25. //Get the Announcements list. Alter this code to match the name of your list
  26. var list = context.get_web().get_lists().getByTitle(ListName);
  27. //Create a new CAML query
  28. var caml = new SP.CamlQuery();
  29. //Create the CAML that will return only items with the titles that contains with 'txtValue'
  30. // var CmalQuery = "<View><Query><Where><Or>";
  31. // CmalQuery += "<Contains><FieldRef Name='EmpName' /><Value Type='Text'>" + txtValue + "</Value></Contains>";
  32. // CmalQuery += "<Contains><FieldRef Name='Company' /><Value Type='Text'>" + txtValue + "</Value></Contains>";
  33. // CmalQuery += "</Or></Where></Query></View>"
  34. caml.set_viewXml(CmalQuery);
  35. //Specify the query and load the list oject
  36. var returnedItems = list.getItems(caml);
  37. context.load(returnedItems);
  38. //Run the query asynchronously, passing the functions to call when a response arrives
  39. context.executeQueryAsync(onQuerySuccess, onQueryFail);
  40. function onQuerySuccess(sender, args) {
  41. //Get an enumerator for the items in the list
  42. var enumerator = returnedItems.getEnumerator();
  43. //Formulate HTML from the list items
  44. var markup = 'Items in the Employee Details list that contains with ' + '"' + txtValue + '"' + ': <br><br><table class="table table-striped">';
  45. //Loop through all the items
  46. while (enumerator.moveNext()) {
  47. var listItem = enumerator.get_current();
  48. markup += '<tr><td>' + listItem.get_item(fieldName).get_lookupValue() + '</td></tr>';
  49. // markup += '<td>' + listItem.get_item('JobTitle') + '</td>';
  50. // markup += '<td> ' + listItem.get_item('Title') + '</td> ';
  51. // markup += '<td>' + listItem.get_item('ManagerName').get_lookupValue() + '</td></tr>';
  52. //Display the formulated HTML in the displayDiv element
  53. }
  54. markup += '</table>'
  55. $('#resultsDiv').html(markup);
  56. }
  57. function onQueryFail(sender, args) {//Formulate HTML to display details of the error
  58. var markup = '<p>The request failed: <br>';
  59. markup += 'Message: ' + args.get_message() + '<br>';
  60. //Display the details
  61. $('#resultsDiv').html(markup);
  62. }
  63.  
  64.  
  65. }
  66. function fn1() {
  67. var context = SP.ClientContext.get_current();
  68. var user = context.get_web().get_currentUser();
  69. var oWebsite = context.get_web();
  70. this.collList = oWebsite.get_lists();
  71. context.load(collList);
  72.  
  73. context.executeQueryAsync(onQuerySucceeded,onQueryFailed);
  74. function onQuerySucceeded() {
  75. var listInfo = '<table class="table table-striped"><tr>';
  76. var listEnumerator = collList.getEnumerator();
  77.  
  78. while (listEnumerator.moveNext()) {
  79. var oList = listEnumerator.get_current();
  80.  
  81. listInfo += '<tr><td>Title: ' + oList.get_title() + '</td></tr>';
  82.  
  83. $("#ddlTitles").append($("<option></option>").html(oList.get_title()));
  84. }
  85. listInfo +='</tr></table>'
  86. $('#resultsDiv').html(listInfo);
  87.  
  88.  
  89.  
  90. }
  91. function onQueryFailed(sender, args) {
  92. alert('Request failed. ' + args.get_message() +
  93. 'n' + args.get_stackTrace());
  94. }
  95. }
  96.  
  97. function retrieveFieldsOfListView(ListName) {
  98.  
  99. var context = new SP.ClientContext.get_current();
  100. var web = context.get_web();
  101. var list = web.get_lists().getByTitle(ListName);
  102. var view = list.get_views().getByTitle('All Items');
  103. var listFields = view.get_viewFields();
  104. context.load(listFields);
  105. context.executeQueryAsync(printFieldNames, onError);
  106.  
  107.  
  108. function printFieldNames() {
  109. var e = listFields.getEnumerator();
  110. CmalQuery += "<View><Query><Where><Contains><Or>";
  111. while (e.moveNext()) {
  112. fieldName = e.get_current();
  113.  
  114. CmalQuery += "<FieldRef Name='"+fieldName+"' /><Value Type='Text'>" + txtValue + "</Value>";
  115.  
  116. console.log(fieldName);
  117.  
  118. }
  119. CmalQuery += "</Or></Contains></Where></Query></View>";
  120. }
  121.  
  122. function onError(sender, args) {
  123. console.log(args.get_message());
  124. }
  125. }
  126.  
  127. CmalQuery += "<FieldRef Name="+fieldName+" /><Value Type=Text>" + txtValue + "</Value>";
  128.  
  129. CmalQuery += "<View><Query><Where><Contains><Or>";
  130. while (e.moveNext()) {
  131. fieldName = e.get_current();
  132.  
  133. CmalQuery += "<FieldRef Name='"+fieldName+"' /><Value Type='Text'>" + txtValue + "</Value>";
  134.  
  135. console.log(fieldName);
  136.  
  137. }
  138. CmalQuery += "</Or></Contains></Where></Query></View>";
  139.  
  140. <View><Query><Where><Contains>
  141. <Or>
  142. <FieldRef Name='fieldName1' /><Value Type='Text'>txtValue1</Value>
  143. <FieldRef Name='fieldName2' /><Value Type='Text'>txtValue2</Value>
  144. <FieldRef Name='fieldName3' /><Value Type='Text'>txtValue3</Value>
  145. </Or>
  146. </Contains></Where></Query></View>
  147.  
  148. <View><Query><Where><Contains>
  149. <Or>
  150. <Or>
  151. <FieldRef Name='fieldName1' /><Value Type='Text'>txtValue1</Value>
  152. <FieldRef Name='fieldName2' /><Value Type='Text'>txtValue2</Value>
  153. </Or>
  154. <FieldRef Name='fieldName3' /><Value Type='Text'>txtValue3</Value>
  155. </Or>
  156. </Contains></Where></Query></View>
  157.  
  158. <Or>
  159. <Eq><FieldRef Name='Status' /><Value Type='Text'>(3) TestStatus3</Value></Eq>
  160. <Eq><FieldRef Name='Status' /><Value Type='Text'>(2) TestStatus2</Value></Eq>
  161. </Or>
  162.  
  163. <And>
  164. <Or>
  165. <Eq><FieldRef Name='Status' /><Value Type='Text'>(3) TestStatus3</Value></Eq>
  166. <Eq><FieldRef Name='Status' /><Value Type='Text'>(2) TestStatus2</Value></Eq>
  167. </Or>
  168. <Eq><FieldRef Name='Status' /><Value Type='Text'>(1) TestStatus1</Value></Eq>
  169. </And>
Advertisement
Add Comment
Please, Sign In to add comment