上帝大脸 发表于 2015-9-27 14:08:49

SharePoint State Machine Workflow +ASP.NET ASPX Form step by step(2)-创建Associati

  在workflow中,有四种表单,分别是Association,Instantiation,Modification,TaskEdit.这篇我就讲讲如何自定义一个Association表单。
  但什么是Association表单?当我们为一个Library、list加上一个workflow的时候,会出现下图,当我们点击Next之后,如果我们自定义了一个Association表单的话,那么你就会跳转到相应的你自己定义的那个表单页面。而总的来说,这个页面是对你的这个workflow做一些定义,你可以为这个workflow加上一些缺省的值,这些值是List/Library级别的,在每个不同Item的workflow中都可以使用。例如,我们可以在Association表单中定义缺省的审批人,然后在后面的Instantiation表单中显现(前提是你有自定义这个表单),在这个表单中你可以直接就使用Association表单中已经定义的审批者,也可以对他们进行修改。从我的理解来说,Association表单就是让你能够在List/Library级别上做些缺省设置,然后在接下来的步骤中,你可以选择使用这些,也可以进行修改用你自己的定义的值。
  
   
  首先,我们要定义一个APSX页面。我的做法是先创建一个Class Library的工程,把原先的文件删了,自己创建一个ASPX文件,和一个CLass Library作为aspx文件的后台代码。
  
  
   
  你可以先将这个工程编译一下,生成一个dll,那么你就可以将这个Assmebly加到你的aspx文件头中。然后加上一些指令,你可以到layouts文件下找一个application页面,将一些公用的指令代码贴过来就行了。这些部分都是公用的,除最开头的Assembly和Inherits是不同的,其它的都可以用的。
  

<%@ Assembly Name="MyASPXPage, Version=1.0.0.0, Culture=neutral, PublicKeyToken=91dbd359a1533843" %>
<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" EnableSessionState="true" ValidateRequest="False" AutoEventWireup="true" Inherits="MyASPXPage.MyAss" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>   
<%@ Register TagPrefix="wssuc" TagName="LinksTable" src="/_controltemplates/LinksTable.ascx" %> <%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="/_controltemplates/InputFormSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="/_controltemplates/InputFormControl.ascx" %> <%@ Register TagPrefix="wssuc" TagName="LinkSection" src="/_controltemplates/LinkSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" src="/_controltemplates/ButtonSection.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ActionBar" src="/_controltemplates/ActionBar.ascx" %> <%@ Register TagPrefix="wssuc" TagName="ToolBar" src="/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBarButton" src="/_controltemplates/ToolBarButton.ascx" %> <%@ Register TagPrefix="wssuc" TagName="Welcome" src="/_controltemplates/Welcome.ascx" %>
<%@ Register Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Workflow" Namespace="Microsoft.SharePoint.Workflow" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  
  在master页面中有很多的PlaceHolder,但对于我们来说只有PlaceHolderMain是最重要的,我们的控件都是放在这里,你要是对其它的PlaceHolder感兴趣,你可以自己加上相应的东西。
  

Code
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">   
   
      <wssuc:InputFormSection Title="Default Workflow Start Values" Description="My Association Form"
            runat="server">
            <template_inputformcontrols>
                <wssuc:InputFormControl LabelText="Reviewers:" runat="server">
                  <Template_Control>
                        <wssawc:PeopleEditor
                                    AllowEmpty="true"
                                    ValidatorEnabled="true"
                                    id="Reviewers"
                                    runat="server"
                                    SelectionSet="User,SecGroup,SPGroup"
                                    width='300px' MultiSelect="true"/>
                        </Template_Control>
                </wssuc:InputFormControl>
                <wssuc:InputFormControl LabelText="Type a message to include with your request:" runat="server">
                        <Template_Control>
                            <wssawc:InputFormTextBox Title="Task Description" class="ms-input" ID="Description" runat="server" TextMode="MultiLine" Columns="40" Rows="5"/>
                        </Template_Control>
                </wssuc:InputFormControl>
            </template_inputformcontrols>
      </wssuc:InputFormSection>
      <wssuc:ButtonSection runat="server">
            <template_buttons>
            <asp:PlaceHolder runat="server">
               
                <asp:Button runat="server" class="ms-ButtonHeightWidth" OnClick="btnOK_Click" Text="OK" id="btnOK"/>
            </asp:PlaceHolder>
      </template_buttons>
      </wssuc:ButtonSection>
               
      <input type="hidden" name="WorkflowDefinition" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["WorkflowDefinition"]),Response.Output); %>' />
      <input type="hidden" name="WorkflowName" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["WorkflowName"]),Response.Output); %>'/>
      <input type="hidden" name="AddToStatusMenu" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["AddToStatusMenu"]),Response.Output); %>'/>
      <input type="hidden" name="AllowManual" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["AllowManual"]),Response.Output); %>'/>
      <input type="hidden" name="RoleSelect" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["RoleSelect"]),Response.Output); %>'/>
      <input type="hidden" name="GuidAssoc" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["GuidAssoc"]),Response.Output); %>'/>
      <input type="hidden" name="SetDefault" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["SetDefault"]),Response.Output); %>'/>
      <input type="hidden" name="HistoryList" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["HistoryList"]),Response.Output); %>'/>
      <input type="hidden" name="TaskList" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["TaskList"]),Response.Output); %>'/>
      <input type="hidden" name="UpdateLists" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["UpdateLists"]),Response.Output); %>' />      
      <input type="hidden" name="AutoStartCreate" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["AutoStartCreate"]),Response.Output); %>' />
      <input type="hidden" name="AutoStartChange" value='<% SPHttpUtility.NoEncode(SPHttpUtility.HtmlEncode(Request.Form["AutoStartChange"]),Response.Output); %>' />

</asp:Content>  
  以上就是ASPX文件的所有内容,后面我们就要着手我们的后台代码了。首先我们要定义一大堆的变量,这些变量是用来保存例如ListId,ItemId,WorkflowId,TaskList等等之类的东西。你还要定义相应的控件,变量名要和你aspx文件控件id一样。
  

Code
private SPWeb _myTeamSite;            

      Paramater variables received from the Workflow Attachment screen#region Paramater variables received from the Workflow Attachment screen

      string _paramWorkflowName = string.Empty;               
      string _paramWorkflowGuid = string.Empty;               
      string _paramSPListGuid = string.Empty;               
      string _paramTaskList = string.Empty;                  
      string _paramHistoryList = string.Empty;                     
      bool _paramAutoStartWF_OnItemChange;                  
      bool _paramAllowManualStart;                           
      string _paramWorkflowAssociationGuid = string.Empty;   

      #endregion

      Workflow List, Template and Association Variables#region Workflow List, Template and Association Variables

      bool _worfkflowAssociationExists = false;         
      SPList _sharePointListAttachedTo;                  
      Guid _workflowAssociationGuid;                     
      SPWorkflowAssociation _workflowAssociationObject;   
      Guid _workflowTemplateGuid;                        
      SPWorkflowTemplate _workflowTemplateObject;         
      #endregion

      Task List and History List variables#region Task List and History List variables

      SPList _workflowtTaskList;                        
      SPList _workflowHistoryList;                        
      #endregion  你先是重载OnPreInit函数。
  
  protected override void OnPreInit(EventArgs e)
      {
          this._myTeamSite = SPControl.GetContextWeb(Context);
         }
  然后为前先无数的变量设置值。因为我们自定义Association Form,原先我们点击了Next之后,相应的workflow就与相应的List联系起来,现在,这些工作要我们自己来做。代码就不贴了,看附件吧(其实代码借用了很多前人的代码)。大家一看就能明白了。下面也没有什么需要嘱咐的地方,就有个地方,在我们的Association Form上有个按钮,你不需要对控件加上一个EventHandler,你只需要在代码中实现一个和页面控件OnClick属性值同名的一个函数,函数原型就和一般的EventHandler是一样的,但记得要设为Public。
  将生成的dll放到GAC中。(要强签名),把ASPX文件放到layouts文件夹下,在你生成的workflow的workflow.xml中做加上 AssociationUrl="_layouts/MyAss.aspx"
  

Code
<Workflow
   Name="TestWorkflow"
   Description="My SharePoint Workflow"
   Id="e0d38698-f244-492a-ae32-4ca3ebcb1b03"
   CodeBesideClass="TestWorkflow.Workflow1"
   CodeBesideAssembly="TestWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cd996aa41f54bf80"
   AssociationUrl="_layouts/MyAss.aspx" />  
  我的页面的样子
  
   希望大家对提意见。
  Association Form
  
页: [1]
查看完整版本: SharePoint State Machine Workflow +ASP.NET ASPX Form step by step(2)-创建Associati