Sharepoint 2010自定义WCF服务返回400 - 使用OpenXML

时间:2017-03-02 18:52:01

标签: wcf sharepoint-2010 openxml

我正在开发一个自定义的Sharepoint 2010服务来处理Excel文件。我在我的本地工作站上使用VS2015。

该服务可以正常运行并调试SPFile,读取它的属性并将其转换为流。但是,只要我使用SpreadsheetDocument.Open()包含创建SpreadsheetDocument的代码,它甚至不再调试,只是简单地返回400“错误请求”的响应。

服务代码

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.SharePoint;
using System;
using System.IO;
using System.ServiceModel.Activation;

namespace Lifeco.Corp.Sharepoint
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ExcelItemToSapService : IExcelItemToSapService
    {

        public ServiceResult SubmitSpreadsheet(string documentUrl)
        {
            // Ensure we have the neccessary information.
            if (string.IsNullOrEmpty(documentUrl))
            {
                return new ServiceResult() { Status = "error", Message = "List item url is required as the 'documentUrl' parameter." };
            }


            SPFile doc = SPContext.Current.Web.GetFile(documentUrl);

            if (doc == null || !doc.Exists)
            {
                return new ServiceResult() { Status = "error", Message = string.Format("Document item at '{0}' was not found.", documentUrl) };
            }

            using (Stream dataStream = doc.OpenBinaryStream())
            {

                // As is this works.  Uncommenting the following 'using' block and I receive 400 - Bad Request without even getting to step into the code and debug.
                //using (SpreadsheetDocument document = SpreadsheetDocument.Open(dataStream, false))
                //{
                //    // work with spreadsheet...
                //}
            }

            ServiceResult response = new ServiceResult() { Status = "success" };
            response.Message = string.Format("Title: {0} | Version: {1} | Modified By: {2}", doc.Title, doc.UIVersionLabel, doc.ModifiedBy.Name);

            return response;
        }
    }
}

.SVC

@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="Lifeco.Corp.Sharepoint.ExcelItemToSapService, $SharePoint.Project.AssemblyFullName$" 
    CodeBehind="ExcelItemToSapService.svc.cs" 
    Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

无论是直接在浏览器中调用服务还是在Sharepoint页面上使用以下jquery,都会收到相同的错误

$.ajax({
                type: "GET",
                url: webServerRelativeUrl + '/_vti_bin/ExcelItemToSapService/ExcelItemToSapService.svc/SubmitSpreadsheet',
                contentType: "application/json; charset=utf-8",
                data: { "documentUrl": "http://s040997/Corporate/Insurance Risk Sample 2.xlsm" },
                dataType: 'json',
                success: function (data) {
                    //alert('Success\n' + JSON.stringify(data));
                    $resultEl.html('<pre>' + JSON.stringify(data) + '</pre>');
                },
                error: function (jqXHR, status, error) {
                    $resultEl.html('');
                    alert('Error\n' + jqXHR.responseText + '\nstatus: ' + status);
                    //alert('Error\n' + jqXHR.responseText + '\nheader: ' + jqXHR.getResponseHeader() + '\nerror: ' + error);
                }
            });

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

找出问题。

我需要将DocumentFormat.OpenXml.dll作为附加程序集添加到我的Sharepoint包中。

  1. 从解决方案资源管理器中打开/Package/Package.package
  2. 高级标签
  3. 添加 - &gt;添加现有程序集
  4. 输入DocumentFormat.OpenXml.dll的源路径
  5. 选定的部署目标= GlobalAssemblyCache
  6. 接下来的测试成功了。