共用方式為


同步處理 Outlook 與 SharePoint 資料夾

此範例示範如何以程序設計方式將 Outlook 與 SharePoint 資料夾連線,以及同步處理資料夾內容。

範例

注意事項

下列程式代碼範例是 Microsoft Office Outlook 2007 程式設計應用程式的摘錄。

在 Outlook 中,您可以將行事曆、聯繫人清單、工作清單、討論區和文檔庫同步處理至 SharePoint 資料夾。 根據同步處理時提供的 URL,Outlook 會建立與 SharePoint 資料夾相同基底類型的新資料夾。 例如,同步處理至 SharePoint 行事曆資料夾會在 Outlook 中建立新的行事曆資料夾。 SharePoint 同步處理資料夾會儲存在自己的 Outlook 個人資料夾 (.pst) 檔案中,位於使用者信箱外部。 您可以使用 NameSpace 物件的 OpenSharedFolder (String、Object、Object、Object) 方法來連線到 SharePoint 資料夾。 請注意,您必須使用 stssync:// URL,以提供有關 SharePoint 伺服器、資料夾路徑,以及 Outlook 開啟 SharePoint 資料夾所需的其他資訊的詳細數據。

以程式設計方式連線到 SharePoint 資料夾時,您必須判斷要用來建立共用關聯性的適當 URL。 因為在資料夾的 SharePoint 使用者介面中未提供 stssync:// URL,請手動將目的地資料夾連結到 Outlook。 然後使用下列程式代碼範例中的第一個程式 DisplaySharePointUrl 來取得正確的 URL。 DisplaySharePointUrl 會使用 Table 物件,在使用中檔案總管視窗的目前資料夾中尋找共用系結資訊。 如果找到一或多個系結內容,將會顯示所有可用共享內容的URL。

現在您有適當的 URL 可建立共用關聯性。 若要同步處理 Outlook 中的新 SharePoint 資料夾,請在第二個程式 AddSpsFolder 中,將 URL 複製並貼到字串變數 calendarUrl 的指派中。 AddSpsFolder 會使用 NameSpace.OpenSharedFolder 方法搭配 stssync:// URL (,將 Outlook 中新 SharePoint 資料夾的同步處理自動化,在此情況下,DisplaySharePointUrl 程式所產生的 URL 會) 。 AddSpsFolderalso 提供自定義資料夾名稱「範例 SPS 行事曆」,並指定 Outlook 使用資料夾的預設存留時間 (TTL) 。 SharePoint 資料夾一律會下載專案附件,因此您不需要在此處指定。

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void DisplaySharePointUrl()
{
    const string PROP_SYNC_URL = 
        "http://schemas.microsoft.com/mapi/id/{00062040-0000-0000-C000-000000000046}/8A24001E";

    Outlook.Folder folder = Application.ActiveExplorer().CurrentFolder as Outlook.Folder;
    Outlook.Table table = folder.GetTable(Type.Missing, Outlook.OlTableContents.olHiddenItems);
    table.Columns.RemoveAll();
    table.Columns.Add("MessageClass");
    table.Columns.Add(PROP_SYNC_URL);

    StringBuilder sb = new StringBuilder();
    while (!table.EndOfTable)
    {
        Outlook.Row row = table.GetNextRow();
        string msgClass, spsUrl;
        msgClass = row["MessageClass"] as string;
        spsUrl = row[PROP_SYNC_URL] as string;

        if (msgClass == "IPM.Sharing.Binding.In")
        {
            sb.Append(spsUrl);
            sb.Append("\r\n");
        }
    }
    if (sb.Length > 0)
    {
        System.Windows.Forms.MessageBox.Show(
            "The following SharePoint Folder URLs were found:\r\n" + sb.ToString());
    }
    else
    {
        System.Windows.Forms.MessageBox.Show("No SharePoint URLs were found in this folder.");
    }
}

private void AddSpsFolder()
{
    string calendarUrl = "stssync://sts/?ver=1.1&type=calendar&cmd=add-folder&base-url=
        https://example.org/calendar&list-url=/Lists/Calendar/calendar.aspx&guid=&site-name=
        Example%20Site&list-name=Calendar";
    string folderName = "Example SPS Calendar";
    bool useDefaultTTL = true;
    Outlook.Folder calendarFolder =
        Application.Session.OpenSharedFolder(calendarUrl, folderName, Type.Missing, useDefaultTTL) 
        as Outlook.Folder;
    Outlook.Explorer exp =
        Application.Explorers.Add(calendarFolder, Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
    exp.Display();
}

另請參閱