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

推荐订阅源

G
GRAHAM CLULEY
V
V2EX
WordPress大学
WordPress大学
博客园 - Franky
Last Week in AI
Last Week in AI
博客园 - 司徒正美
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
A
Arctic Wolf
量子位
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
雷峰网
雷峰网
博客园_首页
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Hacker News: Front Page
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
IT之家
IT之家
The Cloudflare Blog
爱范儿
爱范儿
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 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.