How To Retrieve Checked-Out Documents From All Site Collections

In this blog, we will see, how to retrieve all my checked out documents, using Search. If we have SharePoint Enterprise edition, we can use Content Search Web part to achieve this without writing the code. Please refer to the following CSharp corner article on how to achieve this.
If SharePoint Standard edition Content Search Web part is not available, our organization uses SharePoint Standard edition and we need to display all my checked out documents and we can use the following mechanism:

Before we start with the code part, we have to create a managed property for the CheckOutUuser. 
Please refer to the following article on how to create a Managed Property.
In the above article, managed property has been created for SharePoint On-premise environment. If we are using Office 365 ( SharePoint Online ), please use the following path to create a Managed Property.

In SharePoint Office 365, go to SharePoint Admin Center. Click search in the left Navigation. Click Manage Search Schema. 

From there, we can add a new Managed Property, namely CheckOutUser .

In Add mapping search for the crawled property name "ows_CheckoutUser" and add it to the new managed property.
Once we are done with the creation of Managed Property, run a Full Crawl.

Now, we will move into the code part. We are going to use CSOM ( Javascript object Model ). To filter the checkout user with the current user, we first need to find the display name of the current logged in user.
 
To obtain the current user display name, we will use the code, given below:
  1. var userid = _spPageContextInfo.userId;  
  2.     var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";  
  3.     var requestHeaders = { "accept""application/json;odata=verbose" };  
  4.     $.ajax({  
  5.         url: requestUri,  
  6.         contentType: "application/json;odata=verbose",  
  7.         headers: requestHeaders,  
  8.         success: onSuccess,  
  9.         error: onError  
  10.     });  
  11.     function onSuccess(data, request) {  
  12.         var Logg = data.d;  
  13.         var loginName = Logg.Title;   
  14.      } 
Now, the variable loginName will have the value for the current logged in person. By filtering this value, we can query the documents in Search result.  For Search, we will use the code, given below:
  1. $.ajax({  
  2. url: _spPageContextInfo.webAbsoluteUrl +"/_api/search/query?querytext='IsDocument:\"True\" '&selectproperties='Title%2cOriginalPath%2cModifiedOWSDATE%2cSiteTitle%2cID%2cSPSiteURL%2cCheckOutUser'&rowlimit=10000&refiners='CheckOutUser'&refinementfilters='CheckOutUser:equals(\""+loginName+"\")'",  
  3. type: "GET",  
  4. headers: {  
  5. "accept""application/json;odata=verbose",  
  6. },  
  7. success: function(data){  
  8. var Title = null;  
  9. var taskAssignedTo = null;  
  10. var flag =0;  
  11. var walker = 0;  
  12. var content = null;  
  13. content = "<table border='0' cellspacing='0' cellpadding='0' class='table'><tr><td> Title</td><td>Name</td><td> Last Modified Date</td><td>Site Collection Title</td><td>View Properties</td></tr>";  
  14.   
  15. var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
  16.    
  17.             var html = "<div class='results'>";  
  18.             for (var i = 0; i < results.length; i++)  
  19.             {  
  20. flag =1;  
  21. var title = results[i].Cells.results[2].Value;  
  22. var OriginalPath = results[i].Cells.results[3].Value;  
  23. var modified = results[i].Cells.results[4].Value;  
  24. var SiteTitle = results[i].Cells.results[5].Value;  
  25. var id = results[i].Cells.results[8].Value;  
  26. var siteurl = results[i].Cells.results[9].Value;  
  27. var viewProperties = siteurl+"/DocLibName/Forms/DispForm.aspx?ID="+id;  
  28. content += "<tr align='left' valign='middle'>" + "<td>" + title + "</td>" + "<td><a href='"+OriginalPath+"'>" + title + "</a></td>" + "<td>" + modified + "</td>"+"<td>" + SiteTitle + "</td>" + "<td><a href='"+viewProperties+"' >" + "View Properties" + "</a></td>" +"</tr>";  
  29. }  
  30.   
  31. content+="</table>";  
  32. if(flag == 1)  
  33. {  
  34.  $("#noResults").hide();  
  35.  $("#loading").hide();  
  36. $('#resultTable').append(content);  
  37.   
  38. }  
  39. else  
  40. {  
  41. $("#loading").hide();  
  42.  $("#noResults").show();  
  43.    
  44. }  
  45. },  
  46. error: function(error){  
  47.        console.log("error"+JSON.stringify(error));  
  48. }  
  49.   
  50. });  
Now, the code, given above, will retrieve all the check out documents of the current user and bind in a table with ID "resultTable". The Search query will take a few seconds to load the data and bind them in the table. Thus, we can display the loading image and a message to the user, until the content loads in the page. For it, we can use the ootb image "gears_anv4.gif", which is present in the layouts image folder.
 
Finally, if there is no checked out documents available for the current user, we should display "No checked out documents available". For this, we can add up a Div with the required content and display it, when there is no search result. We have used a flag to find whether there is a search result or not. Also, I have added some CSS to display the result with the different colors in the alternate rows.
 
We can add a content editor Web part in a page and add the code, given below. We can save the content in TXT or HTML file. Upload into SharePoint site and provide the path to the file in the Content Link of the Content Editor Web part.

Thus, the final full code is given below:
  1. <style type="text/css" >  
  2. .table {  
  3. border-collapse: collapse;  
  4. }  
  5. .table tr td {  
  6. padding: 10px;  
  7. text-align: left;  
  8. border: 1px solid #ccc;  
  9. }  
  10. .table>tbody>tr:first-child {  
  11. background:#F2F2F2;  
  12. color:#003399;  
  13.   
  14.   
  15. font-weight:bold;  
  16. text-align:left;  
  17. text-decoration:none;  
  18. white-space:nowrap;  
  19. border-collapse:collapse;  
  20. }  
  21. .table>tbody>tr {  
  22.   
  23.   
  24. }  
  25.     /*  Define the background color for all the ODD background rows  */  
  26.     .table tr:nth-child(odd){   
  27.         background: #D6EBFF;  
  28.     }  
  29.     /*  Define the background color for all the EVEN background rows  */  
  30.     .table tr:nth-child(even){  
  31.         background: #ffffff;  
  32.     }  
  33. </style>  
  34. <script src="/SiteAssets/jquery-1.8.3.min.js"></script>  
  35. <script src="/SiteAssets/pagination.js"></script>  
  36. <script type="text/javascript">  
  37. $("#ctl00_onetidHeadbnnr2").src="/_layouts/images/keysight.png";  
  38.  $("#noResults").hide();  
  39. var userid = _spPageContextInfo.userId;  
  40.     var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";  
  41.     var requestHeaders = { "accept""application/json;odata=verbose" };  
  42.     $.ajax({  
  43.         url: requestUri,  
  44.         contentType: "application/json;odata=verbose",  
  45.         headers: requestHeaders,  
  46.         success: onSuccess,  
  47.         error: onError  
  48.     });  
  49.     function onSuccess(data, request) {  
  50.         var Logg = data.d;  
  51.         var loginName = Logg.Title;  
  52.             $.ajax({  
  53. url: _spPageContextInfo.webAbsoluteUrl +"/_api/search/query?querytext='IsDocument:\"True\" '&selectproperties='Title%2cOriginalPath%2cModifiedOWSDATE%2cSiteTitle%2cID%2cSPSiteURL%2cCheckOutUser'&rowlimit=10000&refiners='CheckOutUser'&refinementfilters='CheckOutUser:equals(\""+loginName+"\")'",  
  54. type: "GET",  
  55. headers: {  
  56. "accept""application/json;odata=verbose",  
  57. },  
  58. success: function(data){  
  59.   
  60. var Title = null;  
  61. var taskAssignedTo = null;  
  62. var flag =0;  
  63. var walker = 0;  
  64. var content = null;  
  65. content = "<table border='0' cellspacing='0' cellpadding='0' class='table'><tr><td> Title</td><td>Name</td><td> Last Modified Date</td><td>Site Collection Title</td><td>View Properties</td></tr>";  
  66.   
  67. var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
  68.    
  69.             var html = "<div class='results'>";  
  70.             for (var i = 0; i < results.length; i++)  
  71.             {  
  72. flag =1;  
  73. var title = results[i].Cells.results[2].Value;  
  74. var OriginalPath = results[i].Cells.results[3].Value;  
  75. var modified = results[i].Cells.results[4].Value;  
  76. var SiteTitle = results[i].Cells.results[5].Value;  
  77. var id = results[i].Cells.results[8].Value;  
  78. var siteurl = results[i].Cells.results[9].Value;  
  79. var viewProperties = siteurl+"/DocLibName/Forms/DispForm.aspx?ID="+id;  
  80. content += "<tr align='left' valign='middle'>" + "<td>" + title + "</td>" + "<td><a href='"+OriginalPath+"'>" + title + "</a></td>" + "<td>" + modified + "</td>"+"<td>" + SiteTitle + "</td>" + "<td><a href='"+viewProperties+"' >" + "View Properties" + "</a></td>" +"</tr>";  
  81. }  
  82.   
  83. content+="</table>";  
  84. if(flag == 1)  
  85. {  
  86.  $("#noResults").hide();  
  87.  $("#loading").hide();  
  88. $('#resultTable').append(content);  
  89.   
  90. }  
  91. else  
  92. {  
  93. $("#loading").hide();  
  94.  $("#noResults").show();  
  95.    
  96. }  
  97. },  
  98. error: function(error){  
  99.        console.log("error"+JSON.stringify(error));  
  100. }  
  101.   }); 
  102. }  
  103.     function onError(error) {  
  104.        console.log("error"+error);  
  105.     }   
  106. </script>  
  107.   
  108. <div id="resultTable"></div>  
  109.  <div id ="noResults" align="center" style="color:Black;background-color:Aquamarine;display:none;">  
  110.         No Checked Out Documents Available  
  111.     </div>  
  112.     <div id="loading" align="left" style="color:Black;font-size:10pt;">  
  113.     <img src="/_layouts/images/gears_anv4.gif" />       Please wait while the content loads.        
  114.     </div> 
Conclusion

Since, we have used JavaScript object model code, we can reuse this code in a SharePoint Hosted app without editing anything. There is only one limitation with this method i.e, it needs crawl to function properly. If crawl fails, the latest checked out documents will not appear in the search result. Until the document is crawled, the document will not be listed in the content. Thus, we can configure the Incremental crawl with less duration gap.
Ebook Download
View all
Learn
View all