sharepoint 2007 文档库编程
来源:互联网 发布:网络暴力经典案例 编辑:程序博客网 时间:2024/04/29 14:26
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using Microsoft.SharePoint;
- using System.IO;
- namespace StatutesPublication
- {
- public class DocLib
- {
- private string siteUri;//网站集
- private string webUri;//网站
- private string docLibUri;//文档库
- SPList list = null;
- public DocLib(string siteUri,string webUri, string docLibUri)
- {
- this.siteUri = siteUri;
- this.webUri = webUri;
- this.docLibUri = docLibUri;
- }
- //打开文档库并返回其引用
- public SPList Open()
- {
- SPSite site = null;
- SPWeb web = null;
- SPList list = null;
- //打开网站集
- try
- {
- site = new SPSite(siteUri);
- }
- catch (Exception ex)
- {
- throw new Exception("网站集错误:" + ex.Message);
- }
- //打开文档库所在网站
- try
- {
- web = site.OpenWeb(webUri);
- }
- catch (Exception ex)
- {
- throw new Exception("网站错误:" + ex.Message);
- }
- //打开文档库
- try
- {
- web.Lists.IncludeRootFolder = true;//SPList.RootFolder的访问默认没有开启
- list = web.Lists[docLibUri];
- }
- catch (Exception ex)
- {
- throw new Exception("文档库错误:" + ex.Message);
- }
- return list;
- }
- public string Upload(string filePath,string dirName,string fileName)
- {
- try
- {
- list = this.Open();
- }
- catch
- {
- return "文档库打开错误!";
- }
- SPFolder rootFolder = list.RootFolder;
- if (dirName == "root")//直接保存在根目录
- {
- if(FileExists("root",fileName))
- rootFolder.Files.Delete(rootFolder.Url + "/" + fileName);
- FileStream fs = new FileStream(filePath, FileMode.Open);
- byte[] content = newbyte[fs.Length];
- fs.Read(content, 0, (int)fs.Length);
- rootFolder.Files.Add(fileName, content);
- fs.Close();
- }
- else
- {
- SPFolder subFolder = null;
- subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
- subFolder = rootFolder.SubFolders[dirName];
- if(FileExists(dirName,fileName))
- subFolder.Files.Delete(subFolder.Url + "/" + fileName);
- FileStream fs = new FileStream(filePath, FileMode.Open);
- byte[] content = new byte[fs.Length];
- fs.Read(content, 0, (int)fs.Length);
- subFolder.Files.Add(fileName, content);
- fs.Close();
- }
- return "Success";
- }
- public bool FileExists(string dirName,string fileName)
- {
- try
- {
- list = this.Open();
- }
- catch
- {
- return false;
- }
- SPFolder rootFolder = list.RootFolder;
- if (dirName == "root")
- {
- foreach (SPFile file in rootFolder.Files)
- {
- if (file.Name == fileName)
- return true;
- }
- }
- else
- {
- SPFolder subFolder = null;
- subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
- subFolder = rootFolder.SubFolders[dirName];
- foreach (SPFile file in subFolder.Files)
- {
- if (file.Name == fileName)
- return true;
- }
- }
- return false;
- }
- public void FileRemove(string dirName,string fileName)
- {
- try
- {
- list = this.Open();
- }
- catch
- {
- return;
- }
- SPFolder rootFolder = list.RootFolder;
- if (dirName == "root")
- {
- foreach (SPFile file in rootFolder.Files)
- {
- if (file.Name == fileName)
- file.Delete();
- }
- }
- else
- {
- SPFolder subFolder = null;
- subFolder = rootFolder.SubFolders.Add(rootFolder.Url + "/" + dirName);
- subFolder = rootFolder.SubFolders[dirName];
- foreach (SPFile file in subFolder.Files)
- {
- if (file.Name == fileName)
- file.Delete();
- }
- }
- }
- //读取用户
- public string GetUser()
- {
- SPSite site = null;
- SPWeb web = null;
- //打开网站集
- try
- {
- site = new SPSite(siteUri);
- }
- catch (Exception ex)
- {
- //throw new Exception("网站集错误:" + ex.Message);
- return String.Empty;
- }
- //打开文档库所在网站
- try
- {
- web = site.OpenWeb(webUri);
- }
- catch (Exception ex)
- {
- //throw new Exception("网站错误:" + ex.Message);
- return String.Empty;
- }
- string spUser = web.CurrentUser.Name;
- return spUser;
- }
- }
- }
- <pre>
1.上传文件到文档库
2.删除文档库中的文件
3.读取当前登录用户
功能比较简单且还在完善中,希望大家多交流
- sharepoint 2007 文档库编程
- Sharepoint文档库操作
- SharePoint 2007 文档库中的文档添加评论功能
- [SharePoint]文档库树形展示
- SharePoint Online 创建文档库
- sharepoint 2007文档库,文件批理下载
- Sharepoint 2007 获取文档库history version设置信息
- Sharepoint 2007 为文档库设置历史版本数
- SharePoint使用说明(3)--文档库
- sharepoint 2010 修改文档库列排序
- sharepoint 2013 文档库eventhandle权限控制
- SharePoint 2013 操作文档库ECB菜单
- sharepoint cmdlet(4)文档库管理
- SharePoint 2016 文档库的新功能简介
- SharePoint 2013 文档库“样式”变了
- C#将文档上传到sharepoint文档库
- 利用客户端向SharePoint文档库上传文档
- SharePoint 更新文档库文档标题(Title)字段
- 如何遍历结构体中的所有元属
- 解决错误ORA-01779: cannot modify a column which maps to a non-key-preserved table
- perror学习
- 简单字符设备应用
- C++ string
- sharepoint 2007 文档库编程
- vc常见问题
- Problems with Multiple QApplications(同时运行两个qt embedded程序)
- 获取Spinner的选中项的值
- 从虚拟地址转为物理地址
- 黑马程序员---类的属性和构造方法
- 抄一个汉字转汉语拼音缩写的存储过程
- C#结构体和字节数组的转换
- 官方NotePad实例学习--上下文菜单ContextMenu的使用