C#Sharepoint拦截文档库中的所有文件下载

时间:2013-04-29 14:53:59

标签: c# sharepoint httphandler

我在使用HttpHandlers之前通过向web.xml添加标记来完成此操作。我不妨在这里复制代码..

private readonly string[] SupportedExtensions = new[] { "*.docx", ".pptx", "xlsx" };
private void AddForSharePoint2007()
    {
        // SP2007 uses IIS6.0 compatibility mode
        SPWebService contentService = SPWebService.ContentService;

        // the Name must be XPATH of Value.. otherwise SP wont remove!
        SPWebConfigModification item = new SPWebConfigModification
        {
            Owner = FeatureId,
            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
            Path = "/configuration/system.web/httpHandlers",
            Name = "add[@verb=\"*\"][@path=\"" + string.Join(",", SupportedExtensions) + "\"][@type=\"MyNamespace.DownloadHandler, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=*****\"]",
            Value = "<add verb=\"*\" path=\"" + string.Join(",", SupportedExtensions) + "\" type=\"MyNamespace.DownloadHandler, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=******\"/>"
        };

        contentService.WebConfigModifications.Add(item);
        contentService.Update();
        contentService.ApplyWebConfigModifications();
    }


    private void AddForSharePoint2010()
    {
        // SP2010 requires IIS7.0
        const string valueTemplate = "<add name=\"DownloadHandler for {0}\" path=\"{1}\" verb=\"*\" type=\"MyNamespace.DownloadHandler, MyProject, Version=4.13.0.0, Culture=neutral, PublicKeyToken=******\"/>";
        const string xpathTemplate = "add[@name=\"DownloadHandler for {0}\"][@path=\"{1}\"][@verb=\"*\"][@type=\"MyNamespace.DownloadHandler, MyProject, Version=4.13.0.0, Culture=neutral, PublicKeyToken=******\"]";
        SPWebService contentService = SPWebService.ContentService;

        foreach (var extension in SupportedExtensions)
        {
            // the Name must be XPATH of Value.. otherwise SP wont remove!
            SPWebConfigModification item = new SPWebConfigModification()
            {
                Owner = FeatureId,
                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                Path = "/configuration/system.webServer/handlers",
                Name = string.Format(xpathTemplate,extension.Replace("*.", string.Empty), extension),
                Value = string.Format(valueTemplate, extension.Replace("*.", string.Empty), extension)
            };

            contentService.WebConfigModifications.Add(item);
        }

        contentService.Update();
        contentService.ApplyWebConfigModifications();
    }

SupportedExtensions列表中可以看出,HTTPHandler是为那些文件扩展名调用的。但这一次,我需要它拦截Sharepoint中文档库的所有文件下载。我可以简单地将SupportedExtensions定义为“*”,但是然后将为Sharepoint上的每个HTTP请求调用处理程序,包括css,js,image,html请求;这将导致巨大的性能问题。这显然是不可接受的。

所以我的问题是:是否可以仅为文档库中的文件请求注册和实现HTTPHandler?或者编写一个非常有效的处理程序,它可以区分文件请求和Sharepoint自己的文件?

0 个答案:

没有答案