I've written some C# User Defined Functions for Excel that I'm trying to run in Excel Services. One of these UDFs is working in my local environment, but failing in the QA environment. Instead of returning actual data, any cells using the UDF are displaying the #VALUE! error. What's particularly interesting is that all of my method code is wrapped in a try catch which should write out an error message to the workbook.

There are other functions in this assembly that run fine, so it's an error with this specific function, but I can't figure out where since none of my error handling code seems to be getting invoked. Here's the method in question.

[UdfMethod(IsVolatile = true, ReturnsPersonalInformation = true)]
    public object[,] getSharepointView(string serverName,
                                       string siteName,
                                       string listName,
                                       string viewName,
                                       bool getTitles)
        object[,] toExcel = null;
        // Impersonate current user viewing Excel Web Access.                

                SPSite site = null;
                    // Get Sharepoint objects.
                    site = new SPSite(serverName);
                    SPList list = site.AllWebs[siteName].Lists[listName];
                    SPView view = list.Views[viewName];

                    // Grab collection of Sharepoint list values.
                    SPListItemCollection values =
                                            list.GetItems(new SPQuery(view));

                    // Add a row for the titles if requested.
                    int titleRow = getTitles ? 1 : 0;
                    int totalRows = values.Count + titleRow;
                    // Avoid two-dimensional arrays with only 
                    // one-dimensional of data 
                    // by adding an empty row if needed.
                    if (totalRows == 1)
                    // Create an object array to return to Excel.
                    toExcel = new object[totalRows, view.ViewFields.Count];
                    // Add column titles to array if needed.
                    if (getTitles)
                        for (int i = 0; i < view.ViewFields.Count; i++)
                            toExcel[0, i] = list.Fields.
                    int j = titleRow;
                    // Iterate through SPListItems in the view.
                    foreach (SPListItem currVal in values)
                        // Add every field value of current item to array.
                        for (int i = 0; i < view.ViewFields.Count; i++)
                            // Get current field.
                            SPField field = currVal.Fields.
                                // Get field value as a text string.
                                toExcel[j, i] = field.GetFieldValueAsText(
                            catch (Exception ex)
                                toExcel[j, i] = "Error loading field";
                catch (System.Exception ex)
                    object[,] error = new object[1, 1];
                    error[0, 0] = "An error has occurred.";
                    toExcel = error;
                    // Dispose of SharePoint objects instead of 
                    // relying on the common language runtime.
                    if (site != null)
        return toExcel;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.