locked
EventReceiver and ExcelServices RRS feed

  • Question

  • Hi All,

    I explain you first the scenario in order to see the better picture:
    I want to create an event receiver (ItemAdded) when a new document is added to a Document Library. If the new element is an Excel file then I will read the content using ExcelServices and store it in a database.

    Is it possible to implement all together in the same EventReceiver.cs file?, are the EventReceiver and working with ExcelServices compatible?. I tried to read an Excel file with Excel Services but either the code wasn't executed or I got an exception.

    Thanks a lot for your help,

    Jordi

    Monday, December 13, 2010 2:23 PM

Answers

  • Hi Jordi,

     

    Thanks for your post.

    You can call the Excel Service in the event handler.

    Here are the main steps:

    1.       Add the Excel Web Service in your project. The Url of the Web Service is: http://<server>/_vti_bin/excelservice.asmx?WSDL

    2.       Call the Web Service in your code, here is some sample code:

    public override void ItemAdded(SPItemEventProperties properties)

    {

       WSExcel.ExcelService ES = new WSExcel.ExcelService();

       ES.Credentials = System.Net.CredentialCache.DefaultCredentials;

      

       string targetExcelPath = properties.WebUrl+"/" +properties.ListItem.File.Url;

       string sheetName = "MyData"; 

     

       Status[] outStatus;

       string sessionId = string.Empty;

       string cellValue = string.Empty;

       try

       {

                       sessionId = ES.OpenWorkbook(targetExcelPath,

                                                    "en-US", "en-US",

                                                    out outStatus);

                       //cellValue = ES.GetCellA1(sessionId, sheetName,

                                                    "BookPrice", false, out outStatus).ToString();

       }

       catch(SoapException ex)

       {

                       //

       }

       finally

       {

                       if (ES != null)

                       {

                                       ES.CloseWorkbook(sessionId);

                       }

       }          

    }

    Note: It’s only  a sample code above. You should avoid the “Loop-Back SOAP Calls” in your code.

    More information: http://msdn.microsoft.com/en-us/library/ms565591.aspx

    Wednesday, December 15, 2010 5:18 AM