Получить документы сервера Sharepoint в веб-приложении - PullRequest
1 голос
/ 21 сентября 2011

Я хочу получить документы sharepoint в веб-приложении. Это мой код.

try
    {
        string sharePointURL = "http://<ipAddress>:<port>/<websiteName>/default.aspx";
        SPSite site = new SPSite(sharePointURL); //Error on this line.....

        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;

        string strContentType = "";
        // docLib is the name of document library
        SPFolder folder = web.GetFolder("docLib");

        SPFileCollection files = folder.Files;

        string fileName = "temp.xls";
        //"docLib" is name of document library and testFile.doc is the name of file
        string url = sharePointURL + "/" + "docLib" + "/" + fileName;

        SPFile tempFile = web.GetFile(url);

        //Get the extension of File.
        string[] fext = fileName.Split('.');
        byte[] obj = (byte[])tempFile.OpenBinary();
        // Get the extension of File to determine the file type
        string casestring = "";
        if (fext.Length > 1)
        {
            casestring = fext[fext.Length - 1];
        }
        //set the content type of file according to extension
        switch (casestring)
        {
            case "txt":
                strContentType = "text/plain";
                break;
            case "htm": strContentType = "text/html";
                break;
            case "html": strContentType = "text/html";
                break;
            case "rtf": strContentType = "text/richtext";
                break;
            case "jpg": strContentType = "image/jpeg";
                break;
            case "jpeg": strContentType = "image/jpeg";
                break;
            case "gif": strContentType = "image/gif";
                break;
            case "bmp": strContentType = "image/bmp";
                break;
            case "mpg": strContentType = "video/mpeg";
                break;
            case "mpeg": strContentType = "video/mpeg";
                break;
            case "avi": strContentType = "video/avi";
                break;
            case "pdf": strContentType = "application/pdf";
                break;
            case "doc": strContentType = "application/msword";
                break;
            case "dot": strContentType = "application/msword";
                break;
            case "csv": strContentType = "application/vnd.msexcel";
                break;
            case ".xls": strContentType = "application/vnd.msexcel";
                break;
            case ".xlt": strContentType = "application/vnd.msexcel";
                break;
            default: strContentType = "application/octet-stream";
                break;
        }
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AppendHeader("Content-Disposition",
                 "attachment; filename= " + fileName);
        Response.ContentType = strContentType;
        //Check that the client is connected and has 
        //not closed the connection after the request
        if (Response.IsClientConnected)
            Response.BinaryWrite(obj);
        Response.Flush();
        Response.Close();
    }
    catch (Exception ex)
    {
       string exMessage = ex.Message;
    }

Я получаю сообщение об ошибке:

SPSite site = new SPSite(sharePointURL);

Сообщение об ошибке:
«Не удалось найти веб-приложение по адресу [URL]. Убедитесь, что вы правильно ввели URL-адрес. Если URL-адрес должен обслуживать существующее содержимое, системному администратору может потребоваться добавить новое сопоставление URL-адреса запроса в предполагаемое приложение».

Пожалуйста, дайте мне знать, как решить эту проблему.

Спасибо.

1 Ответ

3 голосов
/ 21 сентября 2011

попробуйте этот фрагмент (адаптируйте его к вашему случаю):

using Microsoft.SharePoint; 
using Microsoft.SharePoint.Utilities; 
using Microsoft.SharePoint.WebPartPages; 
using Microsoft.SharePoint.WebControls;

try
{
    int flag=0;
    SPSite site = new SPSite(sharePointURL); 
    SPWeb web = site.OpenWeb(); 
    web.AllowUnsafeUpdates=true;

    string strContentType=""; 
    // docLib is the name of document library
    SPFolder folder = web.GetFolder("docLib");

    SPFileCollection files=folder.Files;
    //"docLib" is name of document library and testFile.doc is the name of file
    string url=sharePointURL+"/"+"docLib"+"/"+"testFile.doc"

    SPFile tempFile = web.GetFile(url);

    //Get the extension of File.
    string []fext=this.filename[0].Split('.');
    byte []obj=(byte[])tempFile.OpenBinary();

    // Get the extension of File to determine the file type
    string casestring="";
    if(fext.Length>1)
    {
        casestring= fext[fext.Length-1];
    }
    //set the content type of file according to extension
    switch(casestring)
    {
        case "txt": 
            strContentType = "text/plain";
            break;
        case "htm" : strContentType = "text/html";
            break;
        case "html" : strContentType = "text/html";
            break;
        case "rtf" : strContentType = "text/richtext";
            break;
        case "jpg" : strContentType = "image/jpeg";
            break;
        case "jpeg": strContentType = "image/jpeg";
            break;
        case "gif" : strContentType = "image/gif";
            break;
        case "bmp" : strContentType = "image/bmp";
            break;
        case "mpg" : strContentType = "video/mpeg";
            break;
        case "mpeg": strContentType = "video/mpeg";
            break;
        case "avi" : strContentType = "video/avi";
            break;
        case "pdf" : strContentType = "application/pdf";
            break;
        case "doc" : strContentType = "application/msword";
            break;
        case "dot": strContentType = "application/msword";
            break;
        case "csv" : strContentType = "application/vnd.msexcel";
            break;
        case ".xls": strContentType = "application/vnd.msexcel";
            break;
        case ".xlt": strContentType = "application/vnd.msexcel";
            break;
        default : strContentType = "application/octet-stream";
            break;
    }
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AppendHeader("Content-Disposition", 
             "attachment; filename= "+filename[0]);
    Response.ContentType = strContentType;
    //Check that the client is connected and has 
    //not closed the connection after the request
    if(Response.IsClientConnected)
        Response.BinaryWrite(obj);
    Response.Flush();
    Response.Close();
}
catch(Exception ex)
{
  //... do some logging here...
}

источник: Загрузка, удаление и загрузка файла из библиотеки документов SharePoint 2003

...