C#代码实现上传文件到SharePoint文档库

    SharePoint的文档库是根据数据库虚拟出来的,以HTTP形式呈现,因要创建一个页面单独实现上传功能,故对于其存储和呈现机制进行了学习和研究,不过网络上相关资料还真是很少。SharePoint个人觉得还是比较适合不需要进行复杂逻辑功能的二次开发的网站构建,即适合一般基于office组件的功能门户,能够极大提高效率。

以下代码能够实现往文档库下层目录上传文件的功能,主要还是参考网络上其他文章

using  System.IO;
using  Microsoft.SharePoint;
using  System.Web;

namespace  ConsoleApplication1
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
string fileName = @"C:Documents and SettingsAdministrator桌面word_test.docx";
            System.IO.FileInfo myfile 
= new System.IO.FileInfo(fileName);               
            
byte[] fileContents = new byte[int.Parse(myfile.Length.ToString())];
            FileStream fs
=File.OpenRead(fileName);
            
int n = fs.Read(fileContents, 0int.Parse(myfile.Length.ToString()));
            
string result = Program.UploadDocument("word_test.docx", fileContents, @"http://zpkxv7t0p3xxqyg:47024/DocLib1/New_Test");           
        }


        
public static string UploadDocument(string fileName, byte[] fileContents, string pathFolder)
        
{
            
if (fileContents == null)
            
{
                
return "Null Attachment";
            }

            
try
            
{
                
                
int iStartIndex = pathFolder.LastIndexOf("/");               
                
string sitePath = pathFolder.Remove(iStartIndex);
                
string folderName = string.Empty;

                
if (sitePath.LastIndexOf("/"> 6)
                
{
                    
int sec_iStartIndex = sitePath.LastIndexOf("/");
                    
// System.Console.WriteLine(sec_iStartIndex);
                    sitePath = pathFolder.Remove(sitePath.LastIndexOf("/"));
                    folderName 
= pathFolder.Substring(sec_iStartIndex + 1);
                  
//  System.Console.WriteLine(folderName);
                    
// System.Console.WriteLine(folderName);
                    
// System.Console.WriteLine(sitePath);
                }

                
//   string folderName = pathFolder.Substring(iStartIndex + 1);
                
//   System.Console.WriteLine(folderName);
                else
                
{
                    folderName 
= pathFolder.Substring(iStartIndex + 1);
                   
// System.Console.WriteLine(folderName);
                }

                SPSite site 
= new SPSite(sitePath);
                SPWeb web 
= site.OpenWeb();
                
               
                SPFolder folder 
= web.GetFolder(folderName);

                
string fileURL = fileName;
                
                folder.Files.Add(fileURL, fileContents);

                
if (folder.Files[fileURL].CheckedOutBy.Name != "")
                
{
                    folder.Files[fileURL].CheckIn(
"File Checked In");
                }

               
                
return "File added successfully!";
                
               
            }

            
catch (System.Exception ex)
            
{
                
return ex.Source + ":" + ex.Message;
            }

        }

    }

}


你可能感兴趣的:(SharePoint)