在 Web 应用程序中获取 Sharepoint 服务器文档

发布于 2024-12-05 11:13:37 字数 3449 浏览 0 评论 0原文

我想获取网络应用程序中的共享点文档。这是我的代码。

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] 处的 Web 应用程序。请验证您是否正确键入了 URL。如果该 URL 应提供现有内容,则系统管理员可能需要添加映射到预期应用程序的新请求 URL。”

请让我知道如何解决它。

谢谢。

I want to get the sharepoint documents in webapplication. This is my code.

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

I am getting error on this line:

SPSite site = new SPSite(sharePointURL);

The error message is:
"The web application at [URL] could not be found. Verify that you have typed the url correctly. If the url should be serving existing content, the system administrator may need to add a new request url mapping to the intended application."

Please let me know how to resolve it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-12-12 11:13:37

尝试此代码片段(根据您的情况进行调整):

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 文档库

try this snippet (adapt it to your case):

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文