惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
Y
Y Combinator Blog
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
博客园_首页
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
宝玉的分享
宝玉的分享

博客园 - vuejs3

More on SQL Server Service Broker [转]Android试验:如果View的ID相同会出现什么效果? 父子关系排序,无确定根节点 Silverlight 操作技巧 [文摘]怎么使用Sticky Footer代码(让页脚紧贴页面底部的方法) Sharepoint development toolbox 通过对象模型上传List Template - vuejs3 - 博客园 在文档库中隐藏多文件上传/Disable the Upload Multiple Document option in Document Library - vuejs3 ASP.NET 2.0 TreeView控件在IE7中断开的连接线 Team Foundation Server讲义 通过CertEnroll在CA上(1创建证书请求2得到证书3安装证书) 如何在WSS中利用KeywordQuery创建搜索查询 SharePoint Web Service的身份验证 SharePoint开发中对ListViewWebPart的几个操作 【转】免费SharePoint资源 Enum操作技巧 LINQ概述-通用和便利的信息查询方式 NetShopForge网上商店程序(VB)源码—讨论-发布 Microsoft AJAX添加自定义智能感知(intellisense)
[转]SharePoint Document Library and List – File Upload
vuejs3 · 2009-08-27 · via 博客园 - vuejs3

There are different ways you can add documents to a doc library or to list item as an attachment. You could use object model, built-in web service, custom web service that uses object model, or HTTP PUT method.

In this post I will be stepping through them in detail. Let us take the object model approach first. For the sake of simplicity I have created a simple windows forms app that will push documents in to the document library and list

Adding documents to a Document Library and updating document attributes :

   1: private void button1_Click(object sender, EventArgs e)
   2:         {
   3:             if (openFileDialog1.ShowDialog() == DialogResult.OK)
   4:             {
   5:                 try
   6:                 {
   7:                     FileStream myStream;
   8:                     string fullName;
   9:                     if ((myStream = (FileStream)openFileDialog1.OpenFile()) != null)
  10:                     {
  11:                         fullName = openFileDialog1.FileName;
  12:                         FileInfo myFileInfo = new FileInfo(fullName);
  13:                         using (myStream)
  14:                         {
  15:                             using (SPSite oSite = new SPSite("http://vslearnmoss/sites/FileUpload"))
  16:                             {
  17:                                 using (SPWeb oWeb = oSite.OpenWeb())
  18:                                 {
  19:                                     SPFolderCollection oFolders = oWeb.GetFolder("MyDocuments").SubFolders;
  20:                                     SPFolder oFolder = oFolders["Folder1"];
  21:                                     SPFileCollection oFileColl = oFolder.Files;
  22:                                     Hashtable oDocAttribs = new Hashtable { { "Field1", "Value1" }, { "Field2", "Value2" } };
  23:                                     SPFile oFile = oFileColl.Add(myFileInfo.Name, myStream, oDocAttribs, true);
  24:                                 }
  25:                             }
  26:  
  27:                         }
  28:                     }
  29:                 }
  30:                 catch (Exception ex)
  31:                 {
  32:                     MessageBox.Show("Error: " + ex.Message);
  33:                 }
  34:             }
  35:  
  36:         }

Adding document attachments to a list item  :

   1: private void button1_Click(object sender, EventArgs e)
   2:         {
   3:             if (openFileDialog1.ShowDialog() == DialogResult.OK)
   4:             {
   5:                 try
   6:                 {                    
   7:                     string fullName;
   8:                     fullName = openFileDialog1.FileName;
   9:                     FileInfo myFileInfo = new FileInfo(fullName);
  10:                     byte[] fileData = System.IO.File.ReadAllBytes(fullName);
  11:  
  12:                     using (SPSite oSite = new SPSite("http://vslearnmoss/sites/FileUpload"))
  13:                     {
  14:                         using (SPWeb oWeb = oSite.OpenWeb())
  15:                         {
  16:                             SPList oList = oWeb.Lists["MyList"];
  17:                             SPQuery oQuery = new SPQuery { Query = "0" };
  18:                             SPListItem oItem = oList.GetItems(oQuery).Add();
  19:                             oItem["Title"] = textBox1.Text;
  20:                             SPAttachmentCollection attachments = oItem.Attachments;
  21:                             attachments.Add(myFileInfo.Name, fileData);
  22:                             oItem.Update();
  23:                         }
  24:                     }
  25:                 }
  26:                 catch (Exception ex)
  27:                 {
  28:                     MessageBox.Show("Error: " + ex.Message);
  29:                 }
  30:  
  31:             }
  32:         }

One thing to note when accessing the list for creating a new list item is to be mindful that when SPList.Items.Add is called it fetches all the items before creating a new item, one way to avoid is to make a blank query that fetches zero results and add a new list item to the collection. [Thanks to Rob Garret]

SPQuery oQuery = new SPQuery { Query = "0" };

SPListItem oItem = oList.GetItems(oQuery).Add();

You can look for more info about adding list item efficiently in this article by Rob Garret.

In the next part of this article, I will show you how to utilize web services or HTTP PUT method to push documents into the SharePoint document library.