Coded UI非常好,我开始还在想,怎么样能让一个通过SharePoint Designer创建的Workflow publish三百五十次?想不到一个好的方法,也不知道SharePoint Designer里那个publish按钮调用的是什么方法,想来想去还是用Coded UI尝试了一下,发现也挺好,在资源允许的情况下,就这样完成了SPD Workflow的自动化创建:

1. 首先,你得有个SPD Workflow

SharePoint自动化系列——通过Coded UI录制脚本自动化创建SharePoint Designer Reusable Workflow_SharePoint

2. 通过我第一次录制的脚本发现,SharePoint本身有个bug,就是如果你在Publish完这个Reusable Workflow之后如果不去SharePoint List的Workflow Settings页面刷新一下的话,这个Publish的版本根本就没有创建成功,于是我第二次录制的脚本中有多了一步,就是在Publish Workflow之后要去往Workflow Settings页面刷新一下,这样就能确保Workflow每次Publish都是成功的:

SharePoint自动化系列——通过Coded UI录制脚本自动化创建SharePoint Designer Reusable Workflow_自动化测试_02

 

具体的脚本流程如下(Coded UI具体用法详见我之前发表的一篇文章里有介绍),其实和你操作SharePoint Designer Publish Workflow的步骤是一样的,只不过在最后两步——Publish Workflow和刷新Workflow Settings页面这里我加了一个循环,以达到自动化创建欲达到的次数,就是这样(过程详见我注释行括号中加的汉语):

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左右,半个小时左右弄完。

不过这是通过最接近用户操作的方法实现的,却不是通过直接调用相关API实现的。

还希望有大神能给出直接调用相关API实现Publish SPD Workflow的方法,共同学习,共同进步。