C# 从Sharepoint获取文件列表并下载最新文件

C# 从Sharepoint获取文件列表并下载最新文件,c#,visual-studio-2013,sharepoint-2010,sharepoint-2013,C#,Visual Studio 2013,Sharepoint 2010,Sharepoint 2013,我正在尝试从SharePoint位置获取文件列表,然后使用C#获取最新(按创建时间)的文件 可用的资源涉及SPWeb/SPSite,但Visual Studio 2013上没有直接提供这些资源 SharePoint网站:https://sharepoint.amr..com/sites/OrganizationName/Group/Shared 文档/报告/区域/ 到目前为止,我还不能这样做 任何人都可以提供一些有用的资源或示例吗?如果您没有在SharePoint服务器上运行代码,则需要使用客户

我正在尝试从SharePoint位置获取文件列表,然后使用C#获取最新(按创建时间)的文件

可用的资源涉及SPWeb/SPSite,但Visual Studio 2013上没有直接提供这些资源

SharePoint网站:
https://sharepoint.amr..com/sites/OrganizationName/Group/Shared 文档/报告/区域/

到目前为止,我还不能这样做


任何人都可以提供一些有用的资源或示例吗?

如果您没有在SharePoint服务器上运行代码,则需要使用客户端对象模型

我认为,只要您有权访问该url,就应该针对SharePoint运行此操作。我在我的Office 365上运行了这一功能,这很有效。 我必须对Office 365进行身份验证,但如果您的AD用户具有访问库的权限,您可能可以跳过该验证

以及保存从中获取的流的扩展名


如果您没有在SharePoint服务器上运行代码,则需要使用客户端对象模型

我认为,只要您有权访问该url,就应该针对SharePoint运行此操作。我在我的Office 365上运行了这一功能,这很有效。 我必须对Office 365进行身份验证,但如果您的AD用户具有访问库的权限,您可能可以跳过该验证

以及保存从中获取的流的扩展名


您是要刮取该网站,还是使用sharepoint api来执行此操作?因为从技术上讲,sharepoint只是一个网站,当我尝试将其用作WebClient时,我收到了以下错误:远程服务器返回了一个错误:(401)未经授权。这就是为什么要尝试使用不同的方法。您是要删除该网站,还是使用sharepoint api来执行此操作?因为从技术上讲,sharepoint只是一个网站,当我尝试将其用作WebClient时,我收到了以下错误:远程服务器返回了一个错误:(401)未经授权。这就是为什么尝试使用不同的方法。谢谢你的回复,但是你是如何使用GetList和GetItems方法的?这些似乎不是Microsoft.SharePoint.Client的一部分;您能告诉我如何使用这些或使用哪种参考资料吗?显然,这些应该是不存在的Microsoft.Sharepoint的一部分获取库var list=ctx.Web.GetList(“/Shared%20Documents/”);//获取所有项目的空查询var listItems=list.GetItems(new CamlQuery())@Kevin为了使GetItems方法有效,您必须安装Microsoft.SharePoint.Client.Runtime.dll,对于GetList,它似乎已被弃用或类似的东西,您可以使用它来代替clientContext.Web.Lists.GetByTitle(“yourTitle”)Ref:谢谢您的答复,但是您如何使用GetList和GetItems方法?这些似乎不是Microsoft.SharePoint.Client的一部分;您能告诉我如何使用这些或使用哪种参考资料吗?显然,这些应该是不存在的Microsoft.Sharepoint的一部分获取库var list=ctx.Web.GetList(“/Shared%20Documents/”);//获取所有项目的空查询var listItems=list.GetItems(new CamlQuery())@Kevin为了使GetItems方法起作用,您必须安装Microsoft.SharePoint.Client.Runtime.dll,对于GetList,它似乎已被弃用或类似的内容,您可以使用它代替clientContext.Web.Lists.GetByTitle(“yourTitle”)参考:
// Url to site
string url = "https://your.sharepoint.com";
// get context for that Url
var ctx = new ClientContext(url);

// Provide credentials 
// (Might be able to skip this if the server is on prem and your 
// AD user has permissions to access the library)
var password = new SecureString();
foreach (var c in "your_password".ToCharArray())
    password.AppendChar(c);
ctx.Credentials = 
    new SharePointOnlineCredentials("login@your.onmicrosoft.com", password);

// get the library
var list = ctx.Web.GetList("/Shared%20Documents/");

// Empty query to get all items
var listItems = list.GetItems(new CamlQuery());

// Load all items and use Include to specify what properties
// we want to be able to access
ctx.Load(listItems, 
    items => items.Include(
        item => item["Created"], 
        item => item.File));
// Execute the query
ctx.ExecuteQuery();

// Just to show you we have all the items now
foreach (var item in listItems)
{
    Console.WriteLine("{0} - {1}", 
            item["Created"], 
            item.File.ServerRelativeUrl);
}

// Orderby something and take one
var fileInfo = listItems
    .OrderBy(x => x.File.Name)
    .Take(1)
    .FirstOrDefault();
if (fileInfo != null)
{
    // Open file
    var fileInformation = 
        File.OpenBinaryDirect(ctx, fileInfo.File.ServerRelativeUrl);

    // Save File to c:\temp
    using (var fileStream = 
        new FileStream(@"c:\temp\" + fileInfo.File.Name, FileMode.Create))
        fileInformation.Stream.CopyTo(fileStream);
}
// Extension to save stream
public static void CopyTo(this System.IO.Stream src, System.IO.Stream dest)
{
    if (src == null)
        throw new System.ArgumentNullException("src");
    if (dest == null)
        throw new System.ArgumentNullException("dest");

    System.Diagnostics.Debug.Assert(src.CanRead, "src.CanRead");
    System.Diagnostics.Debug.Assert(dest.CanWrite, "dest.CanWrite");

    int readCount;
    var buffer = new byte[8192];
    while ((readCount = src.Read(buffer, 0, buffer.Length)) != 0)
        dest.Write(buffer, 0, readCount);
}