随笔 - 234, 文章 - 12, 评论 - 1671, 阅读 - 74万
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

使用SharePoint 2007 Web Service上传文件到文档库

Posted on   生鱼片  阅读(3312)  评论(5编辑  收藏  举报

SharePoint 2010中有了全新的客户端模型,给我们在客户端操作SharePoint对象提供了很大的方便,但是在SharePoint 2007中我们可以使用的方式就比较有限,Web Service是我们最常用的一种方式,SharePoint本身提供了很多web Service,比如Lists.asmx如下图:

image

 

我们下面就接触sharepoint提供的web service来实现上传文件

1. 我们要上传的文件如下图:

clip_image002

clip_image004

2. 下图为要上传的文档库:

clip_image006

3. 实现代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MOSSUploadDemo1.*****;
using System.IO;
using System.Net;

namespace MOSSUploadDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFilePath = "c:\\cpu.txt";
            string wsUrl="http://************/_vti_bin/copy.asmx";
            string desPath="http://*******/DocLib1/filename4.txt";
            string userName="****";
            string password="*****";
            string domain="*****";
            
            byte[] filebyte = StreamFile(sourceFilePath);
            UploadFile(filebyte,wsUrl,desPath,userName,password,domain);
        }

        private static void UploadFile(byte[] fileData,string wsUrl,string desPath,string userName,string password,string domain)
        {
            var copy = new ****.Copy
            {
                Url = wsUrl,
                Credentials = new NetworkCredential(userName, password, domain)
               
            };

            string destinationUrl = desPath;
            string[] destinationUrls = { destinationUrl };
            var info1 = new FieldInformation
            {
                DisplayName = "Title",
                InternalName = "Title",
                Type = FieldType.Text,
                Value = "New Title"
            };
            FieldInformation[] info = { info1 };
            var copyResult = new CopyResult();
            CopyResult[] copyResults = { copyResult };
            copy.CopyIntoItems(destinationUrl, destinationUrls, info, fileData, out copyResults);
        }

        private static byte[] StreamFile(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);          
            byte[] ImageData = new byte[fs.Length];           
            fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));           
            fs.Close();
            return ImageData;
          
        }
    }
}

4. 程序远行后,结果如下图:

clip_image008

clip_image010

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示