SharePoint 2010 自定义 WCF 服务返回 400 - "bad request"与 OpenXML



我正在开发自定义SharePoint 2010服务以使用Excel文件。我在本地工作站上使用VS2015。

服务可以正常工作,调试可以使SPFILE,阅读其属性并将其转换为流。但是,一旦我包含了使用电子表格document.open()创建电子表格图表的代码,它甚至不再调试,而只是重新响应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 Page上使用以下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('Successn' + JSON.stringify(data));
                    $resultEl.html('<pre>' + JSON.stringify(data) + '</pre>');
                },
                error: function (jqXHR, status, error) {
                    $resultEl.html('');
                    alert('Errorn' + jqXHR.responseText + 'nstatus: ' + status);
                    //alert('Errorn' + jqXHR.responseText + 'nheader: ' + jqXHR.getResponseHeader() + 'nerror: ' + error);
                }
            });

有什么想法吗?

谢谢

找出了问题。

我需要在我的SharePoint软件包中添加documentformat.openxml.dll。

  1. open/package/package.package from Solution Explorer
  2. 高级选项卡
  3. 添加 ->添加现有汇编
  4. 输入了Document Format.openxml.dll
  5. 的源路径
  6. 选定的部署目标= GlobalAssemblyCache
  7. 好的

和下一个测试成功。

最新更新