从 Sharepoint 获取文件列表并下载最新文件 [英] Get a list of files from Sharepoint and download the latest file

查看:115
本文介绍了从 Sharepoint 获取文件列表并下载最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get a list of files from a SharePoint location and then get the latest (by creation time) file using C#.

可用资源讨论 SPWeb/SPSite,但这些资源不能直接在 Visual Studio 2013 上使用;

The resources available talk about SPWeb/SPSite but these are not available directly on Visual Studio 2013;

SharePoint 站点:https://sharepoint.amr..com/sites/OrganizationName/Group/Shared Documents/Reports/Area/

The SharePoint site: https://sharepoint.amr.<Website>.com/sites/OrganizationName/Group/Shared Documents/Reports/Area/

到目前为止,我一直无法这样做.

So far I have been unable to do so.

谁能提供一些有用的资源或示例?

Could anyone please provide some useful resource or examples?

推荐答案

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

If you're not running your code on the SharePoint server then you'll need to use the Client Object Model.

使用 SharePoint 2013 客户端库代码完成基本操作

我相信只要您有权访问该 url,这应该会针对 SharePoint 运行.我在我的 Office 365 上运行了这个,并且有效.我必须再次对 Office 365 进行身份验证,但如果您的 AD 用户有权访问该库,您可能可以跳过该步骤.

I believe this should run against SharePoint as long as you have access to the url. I ran this against my Office 365 and that works. I have to authenticate agains Office 365 but you might be able to skip that if your AD user has permissions to access the library.

SharePoint 2013:验证 .NET 客户端对象模型在 Office 365 中

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

这篇关于从 Sharepoint 获取文件列表并下载最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆