65.9K
CodeProject 正在变化。 阅读更多。
Home

全面的 SharePoint C# 监视文件夹

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2016年3月27日

CPOL

3分钟阅读

viewsIcon

19014

downloadIcon

192

这是一个全面的 Windows C# 应用程序,它监视特定文件夹并将其中条目上传到 SharePoint 列表或文档库。

引言

这是一个完整的 Windows 应用程序,可以与 Microsoft SharePoint 协同工作。 F2S 是一个监视文件夹,用于监视特定文件夹。 它将其名称添加到 SharePoint 列表或将其上传到 SharePoint 文档库。

F2S 基于“Watchers”工作。在应用程序中,您可以创建多个 Watchers,每个 watcher 获取以下属性并将其保存到 MS-SQL Server 数据库:SharePoint 站点 URL、SharePoint 应用程序、列表名称和必须监视的路径。

此应用程序采用多线程设计,因此当在特定文件夹中找到新文件时,例程不会中断。 此外,F2S 使用自定义 DLL 文件 (SPWebServiceLink) 连接到 SharePoint,这是一个非常专业的类库,它通过 SharePoint 的 Web 服务连接到 SharePoint 并模拟一个 Dataset 以在访客应用程序中使用。 我将在我的下一篇文章中与您分享“SPWebServiceLink”的源代码。

背景

自 2005 年以来,我一直在使用 SharePoint。 我为一家媒体公司创建了这个应用程序,该公司拥有用于保存其输入剪辑的文件服务器(每小时超过 1000 个剪辑)。 该公司已将其数据服务器切换到 SharePoint,并且他们的一项重要要求是将剪辑的名称输入到 SharePoint 列表中。

我为他们开发了这个应用程序。 该应用程序解决了他们的问题,很高兴得知该应用程序从 2008 年至今一直运行,没有任何调试。

Using the Code

这是一个完整的应用程序,我无法描述它的所有细节,但我尝试与您分享一些最重要的问题。 如果您决定将该应用程序用作您公司的解决方案,我也可以为您提供帮助。

  1. mainForm 是此应用程序的核心。 这是此应用程序的主窗体,所有其他用户控件都在 mainForm 的主面板“pnlMain”中打开。 主窗体的左侧栏中有一个自定义控件,名为“OpenFormTree”。 您也可以在此应用程序上看到它的代码。 此控件保存打开窗体的信息,并为应用程序提供窗体浏览功能。
  2. 主窗体有一个 Watchers’ 属性,它是一个 ArrayList,用于保存所有监视器控件的信息。 正如我上面所描述的,在此应用程序中,您可以拥有一个或多个监视器。 每个监视器监视一个指定的文件夹,并将其文件名上传到 Sharepoint 列表或将文件上传到 SharePoint 文档库。
  3. 当应用程序运行时,将调用主窗体的 Load 处理程序。 此方法还会调用另一个方法“RestartWatching()”,此方法从数据库读取所有监视器信息并将其加载到 Watchers 属性中。
private void RestartWatching()
{
    foreach (TreeNode t in this.openFormTree1.Nodes)
    {
        if (t.Text=="Watching Status...")
        {
            MessageBox.Show("You have to close SSP Watching now & Run again.", 
                 "Restart Watching", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.Exit();

            return;
        }
    }

    _Watchers = new System.Collections.ArrayList();
    SSPWatchFolderDatatsetTableAdapters.SSPWatchFolder_FoldersTableAdapter adp = 
      new F2S_WS.SSPWatchFolderDatatsetTableAdapters.SSPWatchFolder_FoldersTableAdapter();
    SSPWatchFolderDatatset dst = new SSPWatchFolderDatatset();
    adp.Fill(dst.SSPWatchFolder_Folders);

    foreach (SSPWatchFolderDatatset.SSPWatchFolder_FoldersRow r in dst.SSPWatchFolder_Folders.Rows)
    {
        try
        {
            SPWebServiceLink.WS_SPList li = new SPWebServiceLink.WS_SPList(r.Folder_SiteURL, _NC);

            string rootFolder = li.GetRootFolder(r.Folder_ListID);

            Uri u = new Uri(r.Folder_SiteURL);
            string rootUrl = u.Scheme + "//" + u.Host + ":" + u.Port.ToString();

            F2S_WS.Controls.WatcherController _watcher = new F2S_WS.Controls.WatcherController();
            _watcher.WatcherLastCheck = r.Folder_LastCheck;
            _watcher.WatcherListID = r.Folder_ListID;
            _watcher.WatcherName = rootFolder;
            _watcher.rootURL = rootUrl;
            _watcher.WatcherPath = r.Folder_Path;
            _watcher.WatcherSiteTimeDif = r.Folder_TimeDif;
            _watcher.WatcherSiteURL = r.Folder_SiteURL;
            _watcher.WatcherMode = r.Folder_Mode;
            _watcher._NC = _NC;

            _watcher.FSWStart();

            _Watchers.Add(_watcher);
        }
        catch
        {

        }
    }
...

watcher 控件具有以下属性,包括所有必需的数据

public string WatcherPath = "-";
public DateTime WatcherLastCheck = new DateTime();
public string WatcherName = "-";
public string WatcherListID = "-";
public string WatcherListName = "-";
public string WatcherWebID = "-";
public string WatcherWebName = "-";
public string WatcherSiteURL = "-";
public long WatcherSiteTimeDif = 0;
public string WatcherMode = "List";
public bool Started = false;
public string rootURL;
public FileSystemWatcher fsw;
public System.Net.NetworkCredential _NC;
private SPWebServiceLink.WS_SPList _SPList;
private SPWebServiceLink.WS_DocumentWorkSpace _DocumentWorkSpace;
private SPWebServiceLink.WS_Copy _Copy;
private  DataSet listDataset;

Watcher 控件基于“FileSystemWathcer”工作,该控件在 FSWStart() 方法中配置。 正如您在 FSWStart() 方法中的以下代码中看到的,所有配置和设置都已完成,并且它开始监视。

public void FSWStart()
{
    fsw = new FileSystemWatcher();
    try
    {
        fsw.Path = this.WatcherPath;

        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
        | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        fsw.Filter = "*.*";

        fsw.Created += new FileSystemEventHandler(OnChanged);

        fsw.IncludeSubdirectories = true;

        fsw.EnableRaisingEvents = true;
        fsw.SynchronizingObject = txtStatus;
        WriteNewLineLog("Watcher Started ...");

        _SPList = new SPWebServiceLink.WS_SPList(WatcherSiteURL, _NC);
        _DocumentWorkSpace = new SPWebServiceLink.WS_DocumentWorkSpace(WatcherSiteURL, _NC);
        _Copy = new SPWebServiceLink.WS_Copy(WatcherSiteURL, _NC);

        listDataset = _SPList.GetListSchema(WatcherListID);

        this.Started = true;
        timer1.Enabled = true;
    }
    catch (System.Exception ex)
    {
        this.Started = false;
        fsw.EnableRaisingEvents = false;
        WriteNewLineLog("Watcher can not be started ...");
        WriteNewLineLog("F2S ERRROR - er453#e - " + ex.Message);                
    }    
}

每个 Watcher 都使用自己的 FileSystemWatcher,因此每个 Watcher 都有两个处理程序来处理 FileSystemWatcher 事件。

public void OnChanged(object source, FileSystemEventArgs e)
{
    try
    {
        WriteNewLineLog("Watcher found new file, Named: " + e.FullPath);        

        if (WatcherMode == "List")
        {
            string[] sp1 = { "\\" };
            string[] folders = e.Name.Split(sp1, StringSplitOptions.RemoveEmptyEntries);

            CreateListTodayFolder();

            if (System.IO.File.Exists(e.FullPath))
            {
                UploadListItem(e.FullPath,"");
            }
            else
            {
                UpLoadDirectoryContentsToList(e.FullPath, 1);
            }

            //string colName = "";
            //for (int i = 0; i < dd.Tables[0].Columns.Count; i++)
            //{
            //    colName += dd.Tables[0].Columns[i].ColumnName + "-->" + 
            //    dd.Tables[0].Rows[1][dd.Tables[0].Columns[i].ColumnName].ToString() + "\r\n";
            //}
            //System.IO.File.WriteAllText("c:\\t.txt", colName, System.Text.Encoding.UTF8);

            WriteNewLineLog("Watcher create new item in Link List");
        }
        else
        {
            string[] sp1 = { "\\" };
            string[] folders = e.Name.Split(sp1, StringSplitOptions.RemoveEmptyEntries);

            CreateTodayFolder();
            if (System.IO.File.Exists(e.FullPath))
            {
                UpLoadFile(e.FullPath,"");
            }
            else
            {
                UpLoadDirectoryContents(e.FullPath, 1);
            }
        }
    }
    catch (Exception ex)
    {
        string ss = ex.Message;
        WriteNewLineLog("F2S ERROR - 1f56");
    }

此外,在 UC 文件夹上还有 2 个用户控件和一个基类

  1. SSPW_FoldersList

    此用户控件使用户能够管理必须监视和控制的文件夹

  2. SSP_WatcherControllerList

    此用户控件包含 Watcher 数组列表中的所有活动监视控件。

如果您有任何问题或建议,使本文更有用,请随时通过下面的评论部分与我联系。

仅允许在与作者协调的情况下商业使用此应用程序。

© . All rights reserved.