locked
Call JavaScript from C# RRS feed

  • Question

  • Hello everyone,

    I am working on a custom web part where the users can create a List View with filtered dates and have that view exported to Excel. This is how the process will go:

      • User enters Start Date and End Date
      • Create View filtered with entered dates
      • Export View to Excel
      • Delete View

    So far, I have the code to create the view filtered with the dates and to delete the view. As for the Export to Excel code, I was testing a javascript function that pertains to the Export to Excel button found on the SharePoint ribbon. However, I can't get that code working on a C# function. A screenshot of the UI is below:

    This is the code I am trying to get to work in a C# function, but I haven't been successful.

    public void ExportExcel()
    {
        string strListName = ddListName.SelectedItem.ToString();
    
        SPList objList = SPContext.Current.Web.Lists[strListName];
    
        string strListGUID = objList.ID.ToString().ToUpper().Replace("-", "\u00252D");
        string strViewGUID = objList.DefaultView.ID.ToString().ToUpper().Replace("-", "\u00252D");
                
        string strCommand = "javascript:EnsureSSImporter();" + "javascript:ExportList('\u002f_vti_bin\u002fowssvr.dll?CS=65001\u0026" + "Using=_layouts\u002fquery.iqy\u0026" + "List=\u00257B" + strListGUID + "\u00257D\u0026" + "View=\u00257B" + strViewGUID + "\u00257D\u0026" + "RootFolder=\u00252FLists\u00252F" + strListName.Replace("_", "\u00255F") + "\u0026" + "CacheControl=1')";
    }

    My main concern on the code is the line that contains the JavaScipt functions.

    If anyone is familiar with this, and can point me in the right direction, I will appreciate it.

    Thanks in advanced.


    Fausto Capellan, Jr - SharePoint Admin

    Friday, September 7, 2012 8:07 PM

Answers

  • Hi Fausto,

    Javascript is running in client side, you have to send the javascript to client to excute it. In asp.net, you can use
    ClientScript.RegisterStartupScript
    to register a script code to client. Here, please try to use the following code:

    ClientScript.RegisterStartupScript(GetType(),"hwa",);"EnsureSSImporter();" + "ExportList('\u002f_vti_bin\u002fowssvr.dll?CS=65001\u0026" + "Using=_layouts\u002fquery.iqy\u0026" + "List=\u00257B" + strListGUID + "\u00257D\u0026" + "View=\u00257B" + strViewGUID + "\u00257D\u0026" + "RootFolder=\u00252FLists\u00252F" + strListName.Replace("_", "\u00255F") + "\u0026" + "CacheControl=1')",true);

    //

    Thanks,
    Lhan Han

    • Edited by Lhan Han Monday, September 10, 2012 4:09 AM
    • Proposed as answer by Hiren.j.Patel Monday, September 10, 2012 1:04 PM
    • Marked as answer by Lhan Han Friday, September 21, 2012 8:25 AM
    Monday, September 10, 2012 4:08 AM

All replies

  • Are you getting any javascript error on click of the button?

    Probably you can check if the strCommand is properly set and there is no missing '' or "".

    Also there might be possibility that the method EnsureSSImporter is having some error and not returning properly. Check that function first with putting alerts.


    Mark it as helpful if so!!! thanks, Mithilesh

    Saturday, September 8, 2012 11:47 AM
  • Hi Fausto,

    Javascript is running in client side, you have to send the javascript to client to excute it. In asp.net, you can use
    ClientScript.RegisterStartupScript
    to register a script code to client. Here, please try to use the following code:

    ClientScript.RegisterStartupScript(GetType(),"hwa",);"EnsureSSImporter();" + "ExportList('\u002f_vti_bin\u002fowssvr.dll?CS=65001\u0026" + "Using=_layouts\u002fquery.iqy\u0026" + "List=\u00257B" + strListGUID + "\u00257D\u0026" + "View=\u00257B" + strViewGUID + "\u00257D\u0026" + "RootFolder=\u00252FLists\u00252F" + strListName.Replace("_", "\u00255F") + "\u0026" + "CacheControl=1')",true);

    //

    Thanks,
    Lhan Han

    • Edited by Lhan Han Monday, September 10, 2012 4:09 AM
    • Proposed as answer by Hiren.j.Patel Monday, September 10, 2012 1:04 PM
    • Marked as answer by Lhan Han Friday, September 21, 2012 8:25 AM
    Monday, September 10, 2012 4:08 AM