代码操作Sharepoint文档库(创建、上传附件)
做项目中遇到客户需求,根据Infopath表单生成Excel并上传到Sharepoint指定文档库中
创建文档库
SPSite site = new SPSite(siteurl); SPListItem item = site.AllWebs["网站名"].Lists["列表名"].Folders.Add("文档库URL", SPFileSystemObjectType.Folder); item["Name"] = your folder name; site.RootWeb.AllowUnsafeUpdates = true; item.Update(); site.RootWeb.AllowUnsafeUpdates = false;
上传文件到指定文档库
FileStream fs = new FileStream(direct, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); Byte[] filecontents = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); SPFolder folder = site.RootWeb.GetFolder("文档库url"); string foldername = 上传上去的文档URL; site.RootWeb.AllowUnsafeUpdates = true;
SPFile sofle = folder.Files.Add(foldername, filecontents, true);
site.RootWeb.AllowUnsafeUpdates = false;