Fikre D

Fikre D

  • NA
  • 9
  • 472

SharePoint list item export

Oct 24 2018 3:40 PM
I have this link export to word from sharePoint list item, it works perfrctly, but i wnat
to Bold the title, add today's date on top and when i export it i want to be descending order.
i need you help please
 
https://www.c-sharpcorner.com/code/2727/export-to-word-utility-in-sharepoint-2013online-using-javas.aspx
  1. <script src="//code.jquery.com/jquery-1.10.1.min.js"></script>  
  2. <script>  
  3. var tempTableId;  
  4. var tempTitle;  
  5. var row;  
  6. var rowData;  
  7. var context;  
  8. var selectedItems;  
  9. var selectedList;  
  10. var oList;  
  11. var oFieldCollection;  
  12. var collListItem;  
  13. var mainExportContainer;  
  14. var table;  
  15. var fieldEnumerator;  
  16. var oField;  
  17. var fieldsArray = ['ID','Title'];  
  18. //var fieldsArray = ['ID','Title','Male','Female','new_x0020_test']; //Insert the internal name of the column of your list.Note : ID column and Title is mandatory.Please uncomment the line.  
  19. var viewname;  
  20. var listColumns;  
  21. function exportSelectedItemsToWord() {  
  22. this.context = SP.ClientContext.get_current();  
  23. this.selectedItems = SP.ListOperation.Selection.getSelectedItems(context);  
  24. this.selectedList = SP.ListOperation.Selection.getSelectedList();  
  25. this.oList = SP.ClientContext.get_current().get_web().get_lists().getById(selectedList);  
  26. // to get the selected item and the selected List.  
  27. var camlQuery = new SP.CamlQuery();  
  28. var stringCamlQuery = "<View><ViewFields>"  
  29. stringCamlQuery = "<View>"  
  30. "<Query>"  
  31. "<Where>"  
  32. "<In>"  
  33. "<FieldRef Name='ID' />"  
  34. "<Values>";  
  35. for (var i = 0; i < selectedItems.length; i++) {  
  36. stringCamlQuery += "<Value Type='Integer'>" + selectedItems[i].id + "</Value>";  
  37. }  
  38. stringCamlQuery += "</Values>"  
  39. "</In>"  
  40. "</Where>"  
  41. "</Query>"  
  42. "</View>";  
  43. var strColumnsInclude = 'Include(';  
  44. for (var i = 0; i < fieldsArray.length - 1; i++) {  
  45. strColumnsInclude += fieldsArray[i] + ', ';  
  46. }  
  47. strColumnsInclude += fieldsArray[i] + ')';  
  48. camlQuery.set_viewXml(stringCamlQuery);  
  49. this.collListItem = oList.getItems(camlQuery);  
  50. context.load(this.collListItem, strColumnsInclude);  
  51. context.executeQueryAsync(Function.createDelegate(thisthis.onQueryexportSucceeded), Function.createDelegate(thisthis.onQueryexportFailed));  
  52. }  
  53. //Creating a html table of the retrieved selected item.  
  54. function onQueryexportSucceeded() {  
  55. var fieldName, fieldValue;  
  56. $('#mainExportContainer').html('');  
  57. mainExportContainer = $('<div></div>').attr('id''mainExportContainer').css('display''none');  
  58. $("<h1 align='center'>List Item(s)</h2>").appendTo("#mainExportContainer");// Heading of the Word document Page.  
  59. itemEnumerator = this.collListItem.getEnumerator();  
  60. var $table;  
  61. while (itemEnumerator.moveNext()) {  
  62. this.oListItem = itemEnumerator.get_current();  
  63. tempTableId = 'listItem_' + this.oListItem.get_item('ID');  
  64. tempTitle = this.oListItem.get_item('Title');  
  65. $('<h2 align="center">' + tempTitle + '</h2>').appendTo('#mainExportContainer');  
  66. $table = $('<table></table>');  
  67. $table.attr('id', tempTableId);  
  68. $("<thead><tr><td><b>Column Name</b></td><td><b>Column Value</b></td><tr></thead>").appendTo($table);  
  69. //Creating Column Name and Column Value as a rows.  
  70. var strValues;  
  71. var j;  
  72. for (var i = 0; i < fieldsArray.length; i++) {  
  73. fieldName = fieldsArray[i];  
  74. var tempfieldName = fieldName.replace(/\_[^_]*\_/g, ' ');  
  75. row = $('<tr></tr>');  
  76. rowData = $('<td></td>').addClass('fieldName').text(tempfieldName);  
  77. row.append(rowData);  
  78. fieldValue = this.oListItem.get_item(fieldName);  
  79. if (fieldValue != null) {  
  80. if (fieldValue.constructor.toString() == SP.FieldUserValue.toString()) {  
  81. rowData = $('<td></td>').addClass('fieldValue').text(strip(fieldValue.get_lookupValue()));  
  82. }  
  83. else if (fieldValue.constructor.toString() == SP.FieldLookupValue.toString()) {  
  84. rowData = $('<td></td>').addClass('fieldValue').text(strip(fieldValue.get_lookupValue()));  
  85. }  
  86. else if (fieldValue.constructor.toString() == "function Array() { [native code] }") {  
  87. for (j = 0; j < fieldValue.length - 1; j++) {  
  88. strValues += fieldValue[j] + ", ";  
  89. }  
  90. strValues += fieldValue[j];  
  91. rowData = $('<td></td>').addClass('fieldValue').text(strip(strValues));  
  92. }  
  93. else {  
  94. rowData = $('<td></td>').addClass('fieldValue').text(strip(fieldValue));  
  95. }  
  96. }  
  97. else {  
  98. rowData = $('<td></td>').addClass('fieldValue').text('');  
  99. }  
  100. row.append(rowData);  
  101. $table.append(row);  
  102. }  
  103. $table.appendTo($('#mainExportContainer'));  
  104. $('<br style="page-break-before: always">').appendTo($('#mainExportContainer'));  
  105. }  
  106. $("#mainExportContainer table tr td:first-child").css('background-color''grey');  
  107. $("#mainExportContainer img").remove();  
  108. var htmlData = '<html xmlns:office="urn:schemas-microsoft-com:office:office" xmlns:word="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40"><head><xml><word:WordDocument><word:View>Print</word:View><word:Zoom>90</word:Zoom><word:DoNotOptimizeForBrowser/></word:WordDocument></head><body>' + document.getElementById('mainExportContainer').innerHTML + "</body></html>";  
  109. exportElementToWord(htmlData);  
  110. //Passing the Html to microsoft uri to export it as Word file  
  111. }  
  112. function onQueryexportFailed(sender, args) {  
  113. alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  114. }  
  115. function exportElementToWord(html) {  
  116. if (navigator.appName === "Microsoft Internet Explorer") {  
  117. var iframe = document.getElementById('htmlDownloadFrame');  
  118. iframe = iframe.contentWindow || iframe.contentDocument;  
  119. iframe.document.open("text/html""replace");  
  120. iframe.document.write(html);  
  121. iframe.document.close();  
  122. iframe.focus();  
  123. iframe.document.execCommand('SaveAs'true'Word.doc');  
  124. else {  
  125. if (console && console.log) {  
  126. console.log('Trying to call getCsvFileForIE with non IE browser.');  
  127. }  
  128. }  
  129. }  
  130. function strip(html) {  
  131. var tmp = document.createElement("DIV");  
  132. tmp.innerHTML = html;  
  133. return tmp.textContent || tmp.innerText || "";  
  134. }  
  135. </script>  
  136. <div id="mainExportContainer" style="display: none;">  
  137. </div>  
  138. <iframe id="htmlDownloadFrame" style="display: none;"></iframe>  
  139. <td>  
  140. <input type="button" id="btnSubmit" value="Export to word" onclick=" exportSelectedItemsToWord()" style="  
  141. margin-left: 163px ;margin-bottom:14px; color: #fff;  
  142. background-color: #6496c8;  
  143. border: none;  
  144. border-radius: 15px;  
  145. box-shadow: 0 10px #27496d;"/>  
  146. </td>