2011-09-21 89 views
1

我想獲取Web應用程序中的SharePoint文檔。這是我的代碼。在Web應用程序中獲取Sharepoint服務器文檔

SPSite site = new SPSite(sharePointURL); 

的錯誤信息是:

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; 
    } 

我在這一行收到錯誤。
「在[URL] Web應用程序無法找到驗證是否正確輸入了網址。如果網址應該提供現有的內容,系統管理員可能需要添加一個新的請求URL映射到預期的應用程序。「

請讓我知道如何解決它。

謝謝。

回答

3

試試這個片斷(它適應您的情況):

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... 
} 

來源:Uploading, Deleting, and Downloading a File from SharePoint 2003 Document Library

+0

它將引發我對這個網站的SPSite錯誤=新的SPSite(sharePointURL);.我檢查了我的網址是否正常。錯誤是:找不到[URL]處的Web應用程序。驗證您是否正確輸入了網址。如果網址應該提供現有的內容,系統管理員可能需要添加一個新的請求URL映射到預期的應用程序。 – Sami

+0

你的sharePointUrl的價值是什麼? –

+0

string sharePointUrl =「http:// :8090」; – Sami

相關問題