Hello guys,
Here I am explaining how to implement the OOB
'Export to Excel' feature of SharePoint list
using RPC (Remote Procedure Calls) with Javascript CSOM code.
What is RPC :
This is a protocol from Windows SharePoint
Services(WSS) , helps in exchanging the data between client and the server
which runs with WSS. As all of you know the foundation for Sharepoint Server is
came from WSS.
So for this kind of functionalities we can
make use of this RPC. Here more about RPC from msdn.
Here I am using this below rpc url to
generate the excel report of a list
"/_vti_bin/owssvr.dll?CS=109&Using=_layouts/query.iqy&List={"
+ listId + "}" + "&View={"
+ viewId + "}&CacheControl=1";
The parameters are List GUID and the View
GUID.
Get List GUID :
Go to your list settings page: List Settingsà in
Url you will find
http:// purnaexperiments:1234/_layouts/15/listedit.aspx?List=%7BCEB4F6D3%2D06AC%2D43FF%2DA95E%2DA158144585A3%7D
Replace %7B with
“{“,
%2D with “–“ and
%7D with “}”
After this the you get the GUID like ={CEB4F6D3-06AC-43FF-A95E-A158144585A3}.
Get View Guid :
Go to List Viewà Click Modify this ViewàYour
url will look like this follow url
Copy the value after the string ‘View’ in
above url and
Replace %7B with
“{“,
%2D with “–“ and
%7D with “}”
After this the you get the View GUID like ={ E42BBCD5-8A0F-4092-8B3D-52D9EF805AAB}.
In below code I am getting the LIST GUID and
View GUID from their names.
You can use this function directly to generate the
report with List Title and View Title
Code :
- <input id="btnGenerate" onclick=" GeneratePCMReport('MyCustomList', 'All Items’);" value="Generate Excel Report" />
- // Here the list name is 'MyCustomList' and the view name is ‘All Items’
- <script type="text/javascript">
- var exporturl = null;
- var listPages = null;
- var view = null;
- var web = null;
- function GeneratePCMReport(listName, viewTitle)
- {
- if (navigator.appName == "Microsoft Internet Explorer")
- {
- var context = new SP.ClientContext.get_current();
- web = context.get_web();
- context.load(web);
- listPages = context.get_web().get_lists().getByTitle(listName);
- context.load(listPages);
- view = listPages.get_views().getByTitle(viewTitle);
- context.load(view);
- context.executeQueryAsync(getlistInfoSuccess, getlistInfoFailed);
- }
- else {
- alert("Please use Internet Explorer to generate the Excel Report");
- }
- }
- function getlistInfoSuccess(sender, args)
- {
- var listId = listPages.get_id();
- var viewId = view.get_id();
- exporturl = web.get_url() + "/_vti_bin/owssvr.dll?CS=109&Using=_layouts/query.iqy&List={" + listId + "}" + "&View={" + viewId + "}&CacheControl=1";
- window.location.href = exporturl;
- }
- function getlistInfoFailed(sender, args)
- {
- alert('Issue in getting the Report, Please try later')
- }
- </script>
Note : As you know the oob 'Export to Excel' works only in IE, the above code also works only in IE.
Since we are calling the same oob function through csom code. That is the reason i have included a condition (navigator.appName == "Microsoft Internet Explorer") in above code to check the browser type.
Thanks,
Purna
Thanks a lot. :) It is Really helpful. Can you please suggest me how to export this excel data in powerpoint using CSOM? Just by clicking on the 'btnGenerate'? Thanks :)
ReplyDeletehow i can change file name
ReplyDelete