SharePoint自動化系列——經過Coded UI錄製腳本自動化建立SharePoint Designer Reusable Workflow

Coded UI很是好,我開始還在想,怎麼樣能讓一個經過SharePoint Designer建立的Workflow publish三百五十次?想不到一個好的方法,也不知道SharePoint Designer裏那個publish按鈕調用的是什麼方法,想來想去仍是用Coded UI嘗試了一下,發現也挺好,在資源容許的狀況下,就這樣完成了SPD Workflow的自動化建立:web

1. 首先,你得有個SPD Workflow瀏覽器

2. 經過我第一次錄製的腳本發現,SharePoint自己有個bug,就是若是你在Publish完這個Reusable Workflow以後若是不去SharePoint List的Workflow Settings頁面刷新一下的話,這個Publish的版本根本就沒有建立成功,因而我第二次錄製的腳本中有多了一步,就是在Publish Workflow以後要去往Workflow Settings頁面刷新一下,這樣就能確保Workflow每次Publish都是成功的:學習

 

具體的腳本流程以下(Coded UI具體用法詳見我以前發表的一篇文章裏有介紹),其實和你操做SharePoint Designer Publish Workflow的步驟是同樣的,只不過在最後兩步——Publish Workflow和刷新Workflow Settings頁面這裏我加了一個循環,以達到自動化建立欲達到的次數,就是這樣(過程詳見我註釋行括號中加的漢語):this

public void RecordedMethod()
        {
            #region Variable Declarations
            WinButton uIItem30828CI1Button = this.UIHttpmgluca10000sitesWindow.UIItem30828CI1ListItem.UIItem30828CI1Button;
            WinButton uIWorkflowsButton = this.UIHttpmgluca10000sitesWindow.UIWorkflowsListItem.UIWorkflowsButton;
            WinListItem uIRe10ListItem = this.UIHttpmgluca10000sitesWindow.UIItemWindow1.UIItemList.UIRe10ListItem;
            WinButton uIPublishButton = this.UIHttpmgluca10000sitesWindow.UIItemWindow.UISaveToolBar.UIPublishButton;
            WinEdit uIAddressEdit = this.UIMsnWindowsInternetExWindow.UIItemWindow.UIAddressBarClient.UIAddressEdit;
            BrowserWindow uIMsnWindowsInternetExWindow = this.UIMsnWindowsInternetExWindow;
            HtmlHyperlink uIShareHyperlink = this.UIMsnWindowsInternetExWindow.UIWorkflowSettingsDocument.UIShareHyperlink;
            HtmlDocument uIWorkflowSettingsDocument = this.UIMsnWindowsInternetExWindow.UIWorkflowSettingsDocument;
            #endregion

            // Launch '%ProgramW6432%\Microsoft Office\Office15\SPDESIGN.EXE'(打開SPD)
            ApplicationUnderTest sPDESIGNApplication = ApplicationUnderTest.Launch(this.RecordedMethod4Params.ExePath, this.RecordedMethod4Params.AlternateExePath);

            // Click '30828CI1' button(點擊相應的站點)
            Mouse.Click(uIItem30828CI1Button, new Point(48, 10));

            // Click 'Workflows' button(點擊Workflows)
            Mouse.Click(uIWorkflowsButton, new Point(54, 7));

            // Click 're10' list item(點擊相應的Workflow)
            Mouse.Click(uIRe10ListItem, new Point(35, 13));

            // Click 'Publish' button(點擊Publish按鈕)
            Mouse.Click(uIPublishButton, new Point(13, 31));

            // Go to web page 'http://go.microsoft.com/fwlink/p/?LinkId=255141' using new browser instance(打開瀏覽器)
            this.UIMsnWindowsInternetExWindow.LaunchUrl(new System.Uri(this.RecordedMethod4Params.UIMsnWindowsInternetExWindowUrl));

            // Click 'Address' text box(點擊瀏覽器地址欄)
            Mouse.Click(uIAddressEdit, new Point(216, 2));

            // Go to web page 'http://mglu-ca:10000/sites/30828CI1/_layouts/15/WrkSetng.aspx?List={C9B1EDD4-8B24-4115-9753-3113D02BE3C1}'(輸入Workflow Settings頁面的地址)
            uIMsnWindowsInternetExWindow.NavigateToUrl(new System.Uri(this.RecordedMethod4Params.UIMsnWindowsInternetExWindowUrl1));

            // Set flag to allow play back to continue if non-essential actions fail. (For example, if a mouse hover action fails.)
            Playback.PlaybackSettings.ContinueOnError = true;

            // Mouse hover 'Share' link at (8, 3)
            Mouse.Hover(uIShareHyperlink, new Point(8, 3));

            // Reset flag to ensure that play back stops if there is an error.
            Playback.PlaybackSettings.ContinueOnError = false;

            int i=0;
            while (i < 350)
            {
                // Click 'Publish' button(點擊Publish按鈕)
                Mouse.Click(uIPublishButton, new Point(23, 31));

                // Type '{F5}' in 'Workflow Settings' document(刷新Workflow Settings頁面)
                Keyboard.SendKeys(uIWorkflowSettingsDocument, this.RecordedMethod4Params.UIWorkflowSettingsDocumentSendKeys, ModifierKeys.None);
            }
        }

就這樣,一個自動化建立SPD Workflow的任務就完成了,還挺快的,一分鐘差很少建立10個SPD Workflow左右,半個小時左右弄完。spa

不過這是經過最接近用戶操做的方法實現的,卻不是經過直接調用相關API實現的。code

還但願有大神能給出直接調用相關API實現Publish SPD Workflow的方法,共同窗習,共同進步。blog

相關文章
相關標籤/搜索