SharePoint 2010开发实例精选——通过客户端对象模型上传下载文件

为了使用ClientContext,我们需要添加两个dll引用到我们的项目中。Microsoft.SharePoint.Client.dll和Microsoft.SharePoint.Client.Runtime.dll。在本博文中,我们将学习如何:

  • 从SharePoint文档库中通过CAML获取ListItemCollection
  • 上载一个文档到SharePoint 文档库
  • 从SharePoint文档库下载一个文档

从SharePoint文档库中通过CAML获取ListItemCollection

我们可以像下面这样获取ListItemCollection:

1
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1);

GetListItemCollectionFormSP方法可以用来获取列表项,其中第一个参数Name - 为FieldRef的名称,第二个参数value=FieldRef的值,第三个参数type - 是值的类型,最后一个参数rowLimit - 是返回最多多少条记录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private static ListItemCollection GetListItemCollectionFromSP(string name, string value, string type, int rowLimit)
{
    ListItemCollection listItems = null;
    using (ClientContext clientContext = new ClientContext(siteURL))
    {
        List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
        CamlQuery camlQuery = new CamlQuery(); ;
        camlQuery.ViewXml =
        @"<View>
        <Query>
        <Where>
        <Eq>
        <FieldRef Name='" + name + @"'/>
        <Value Type='" + type + "'>" + value + @"</Value>
        </Eq>
        </Where>                   
        <RowLimit>" + rowLimit.ToString() + @"</RowLimit>
        </Query>
        </View>";
        listItems = documentsList.GetItems(camlQuery);
        clientContext.Load(documentsList);
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();
    }
    return listItems;
}

上载一个文档到SharePoint 文档库

本例中我们需要上载文档到SharePoint文档库,同时更新该文档的元数据信息。比如通过ClientContext将一个说明字段设置为“核心内容”。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void UploadDocument(string siteURL, string documentListName,string documentListURL, string documentName, byte[] documentStream)
{
    using (ClientContext clientContext = new ClientContext(siteURL))
    {
        //获取文档库
        List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
        var fileCreationInformation = new FileCreationInformation();
        //指定内容 byte[]数组,这里是 documentStream
        fileCreationInformation.Content = documentStream;
        //允许文档覆盖
        fileCreationInformation.Overwrite = true;
        //上载 URL地址
        fileCreationInformation.Url = siteURL + documentListURL + documentName;
        Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
        //更新元数据信息,这里是一个显示名为“描述”的字段,其字段名为“Description0”
        uploadFile.ListItemAllFields["Description0"] = "核心内容";
        uploadFile.ListItemAllFields.Update();
        clientContext.ExecuteQuery();
    }
}

从SharePoint文档库下载一个文档

我们可以用如下代码下载一个文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static Stream DownloadDocument(string siteURL, string documentName)
{
    ListItem item = GetDocumentFromSP(documentName);
    if (item != null)
    {
        using (ClientContext clientContext = new ClientContext(siteURL))
        {
            FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());
            return fInfo.Stream;
        }
    }
    return null;
}
private static ListItem GetDocumentFromSP(string documentName)
{
    //这个方法上面讨论过了,用于从SharePoint文档库获取列表项集合
    ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1);
    return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}

调用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
static string siteURL = "http://sp2010u/it";
static string documentListName = "共享文档";
 
static void Main(string[] args)
    UploadTest();
    DownloadTest();
    Console.ReadLine();
}
private static void DownloadTest()
{
    string downloadDocumentName = "Lesson 2.pptx";
    Stream s = DownloadDocument(siteURL, downloadDocumentName);
 
    string saveTo = @"C:\" + downloadDocumentName;
    // 创建一个写入流
    FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
    // 写入到该流
    ReadWriteStream(s, writeStream);
    Console.WriteLine("下载完成!");
}
private static void UploadTest()
{
    string uploadDocumentName = "Lesson 1.pptx";
    string openFrom =@"C:\"+uploadDocumentName;
    if (!System.IO.File.Exists(openFrom))
    {
        throw new ArgumentException(String.Format("{0} 不存在",openFrom), "openFrom");
    }
 
    FileStream fStream = System.IO.File.OpenRead(openFrom);
    byte[] contents = new byte[fStream.Length];
    fStream.Read(contents, 0, (int)fStream.Length);
    //上载到“共享文档”文档库下的“销售计划”文档集中
    UploadDocument(siteURL, documentListName, "/Shared%20Documents/%E9%94%80%E5%94%AE%E8%AE%A1%E5%88%92/", uploadDocumentName, contents);
    Console.WriteLine("上载完成!");
    fStream.Close();
 
}
// readStream 是你需要读取的流
// writeStream 是你需要写入的流
private static void ReadWriteStream(Stream readStream, Stream writeStream)
{
    int Length = 256;
    Byte[] buffer = new Byte[Length];
    int bytesRead = readStream.Read(buffer, 0, Length);
    // 写入要求的字节
    while (bytesRead > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
        bytesRead = readStream.Read(buffer, 0, Length);
    }
    readStream.Close();
    writeStream.Close();
}

上载到SharePoint中的效果如下:

希望对你有帮助!

 

参考资料

How to upload/download a document in SharePoint 2010 using Client Context Object Model

Microsoft.SharePoint.Client.ListItem Class

Microsoft.SharePoint.Client.File Class

Programmatically Upload document using Client object model – SharePoint 2010

posted @   Sunmoonfire  阅读(4623)  评论(3编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示