Tuesday, 14 February 2012

Me and workflow - "Start a new workflow after this Workflow end"

Hi!

Here's the thing.... i just realize that SPD2010 don't have the workflow called "Start another WF"..before, when using SPD2007, i knw how to custom made this WF.  Now, i'm using SPD2010, more of the functions are not easy to customized. It's easy for beginners but such a prison for people who wants it to be more flexible. :((

So, i thought of finding some ways to create these custom WF. All of the things that i find in the net regarding this, i'll will compile it here. So, this entry will be a LONG~~ entry. :D ]

Hope i have time to try it one by one..hehehehe >_< 

1)From : http://www.anmolrehan-sharepointconsultant.com/2011/09/workflow-activity-to-trigger-another.html

SharePoint 2010: Workflow Activity to trigger another Workflow

While creating a workflow in SharePoint Designer, You may find a situation in which you may require to create a Step to trigger another workflow on same list. By default its not possible in Sharepoint Designer. So to achieve this we can create a custom workflow activity.

Steps to acheive this
  • Create Empty SharePoint Project.
  • Add Sharepoint Mapped Folder
    "{SharePointRoot}\Template\1033\Workflow"
  • Create new file "SPDStartWorkflow.actions" in mapped folder. And paste below code in this
    <?xml version="1.0" encoding="utf-8" ?>
    <WorkflowInfo>
      <Actions Sequential="then" Parallel="and">
        <Action Name="Start Another Workflow"
         ClassName="WorkflowStarter.StartAnotherWorkflow"
         Assembly="WorkflowStarter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3b0e31fdf8deb757"
         AppliesTo="all"
         Category="Custom Actions">
          <RuleDesigner Sentence="Start another workflow on %2, named %1">
            <FieldBind Field="WorkflowIdentifier" Text="Call Self" Id="1" DesignerType="TextArea" />
            <FieldBind Field="ListId,ListItem" Text="this item" Id="2" DesignerType="ChooseListItem" />
          </RuleDesigner>
          <Parameters>
            <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext" Direction="In" />
            <Parameter Name="WorkflowIdentifier" Type="System.String, mscorlib" Direction="Optional" />
            <Parameter Name="ListId" Type="System.String, mscorlib" Direction="In" />
            <Parameter Name="ListItem" Type="System.Int32, mscorlib" Direction="In" />
          </Parameters>
        </Action>
      </Actions>
    </WorkflowInfo>

  •  Add new class "StartAnotherWorkflow" and inherit it from "Activity". Add below code in file.
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context",
                                                                                       typeof(WorkflowContext),
                                                                                       typeof(StartAnotherWorkflow));


        public static DependencyProperty ListIdProperty = DependencyProperty.Register("ListId", typeof(string),
                                                                                      typeof(StartAnotherWorkflow));

       
        public static DependencyProperty ListItemProperty = DependencyProperty.Register("ListItem", typeof(int),
                                                                                        typeof(StartAnotherWorkflow));

       
       public static DependencyProperty WorkflowIdentifierProperty = DependencyProperty.Register("WorkflowIdentifier",
                                                                                                  typeof(string),
                                                                                                  typeof(
                                                                                                      StartAnotherWorkflow
                                                                                                      ), new PropertyMetadata(""));


        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get { return ((WorkflowContext)(base.GetValue(__ContextProperty))); }
            set { base.SetValue(__ContextProperty, value); }
        }

       
        [Description("ID of the list we are working with")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ListId
        {
            get { return ((string)(base.GetValue(ListIdProperty))); }
            set { base.SetValue(ListIdProperty, value); }
        }

       
        [Description("ID of the list item we are working with")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int ListItem
        {
            get { return ((int)(base.GetValue(ListItemProperty))); }
            set { base.SetValue(ListItemProperty, value); }
        }

       
        [Description("Workflow name or template base id")]
        [ValidationOption(ValidationOption.None)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string WorkflowIdentifier
        {
            get { return ((string)(base.GetValue(WorkflowIdentifierProperty))); }
            set { base.SetValue(WorkflowIdentifierProperty, value); }
        }

       
         protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                //need to run under SHAREPOINT\system account because workflow owner might not have start workflow permissions on the target list
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite site = new SPSite(__Context.Site.ID))
                    {
                        using (SPWeb web = site.AllWebs[__Context.Web.ID])
                        {
                            SPList list = web.Lists[new Guid(ListId)];

                           
                            SPListItem listItem =
                                list.Items.GetItemById(ListItem);

                           
                             //resolve any lookup parameters
                            string wkId =
                                Common.ProcessStringField(executionContext,
                                                          WorkflowIdentifier);

                           
                            //find workflow association by name
                            SPWorkflowAssociation myWorkflowAssoc =
                                list.WorkflowAssociations.GetAssociationByName(
                                    wkId, Thread.CurrentThread.CurrentCulture);
                           
                            if (myWorkflowAssoc != null)
                            {
                                //start the workflow
                                site.WorkflowManager.StartWorkflow(listItem,
                                                                   myWorkflowAssoc,
                                                                   myWorkflowAssoc.AssociationData);
                            }
                        }
                    }
                });
            }
            catch (Exception e)
            {
              
            }

            return ActivityExecutionStatus.Closed;
        }
 2) From : http://msmvps.com/blogs/sundar_narasiman/archive/2010/12/26/develop-custom-workflow-activity-for-sharepoint-2010-workflow.aspx
There is some error in the codes though.

Develop Custom Workflow Activity for SharePoint 2010 Workflow

In this article, I’d be discussing the steps to create a custom workflow activity for SharePoint 2010. The need for custom workflow activity arises, when the out-of-box workflow activities do not suffice our requirement. The functionality of developing a custom activity for SharePoint 2010 Workflow is not a new feature in SharePoint 2010. This was available in SharePoint 2007, which is now continued to SharePoint 2010. Once the custom workflow activity is created, it can be made available for use inside SharePoint Designer 2010.

File ---> New Project
Select -----> Visual C# ---> SharePoint | 2010 ----> Empty Project
custom activity 1
custom activity2


Click Finish
File ---> Add –> New Project
Select the Visual C# | Workflow | Workflow Activity Library and set the project name as ‘CreateActivityDemo’.
custom activity3[5]
Right click ‘CreateActivityDemo’ project and add a reference to Microsoft.SharePoint.dll and Microsoft.SharePoint.Workflow.Actions.dll
Add a new workflow activity called ‘CreateSurveyLibrary.cs’
Switch to view code of ‘CreateSurveyLibrary.cs’
Import the following namespaces
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Workflow Actions;
 
Add a dependency property property by name ‘SiteUrlProperty’
public static DependencyProperty SiteUrlProperty = DependencyProperty.Register("SiteUrl", 

typeof(string), typeof(CreateSurveyList), new PropertyMetadata(""));
[DescriptionAttribute("Url of site where survey is to be created")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
[ValidationOption(ValidationOption.Optional)]
public string SiteUrl
{
get
{
return ((string)(base.GetValue(CreateSurveyList.SiteUrlProperty)));
}
set
{
base.SetValue(CreateSurveyList.SiteUrlProperty, value);
}
}
The SiteUrlProperty is used to capture the url of the site, in which the survey list needs to be created. The convention for creating the dependency property is to have the keyword ‘Property’ appended (at the end) to the name of the actual property. In this case, the name of the regular C# property is SiteUrl, the dependency property is created with convention of ‘SiteUrlProperty’. If this naming convention is not followed, we’ll get a compilation error.

Add another dependency property called ‘SurveyListNameProperty’. This is used to assign the name for Survey List during the list creation.

public static DependencyProperty SurveyListNameProperty = DependencyProperty.Register("SurveyListName", 

typeof(string), typeof(CreateSurveyList), new PropertyMetadata(""));
[DescriptionAttribute("Name for survey list")]
[BrowsableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
[ValidationOption(ValidationOption.Optional)]
public string SurveyListName
{
get
{
return ((string)(base.GetValue(CreateSurveyList.SurveyListNameProperty)));
}
set
{
base.SetValue(CreateSurveyList.SurveyListNameProperty, value);
}
}
Again, follow the same naming convention for creating this dependency property ‘SurveyListName’, discussed above.
The next logical step in creating custom workflow activity for SharePoint is to override the Execute method of the workflow activity. Then add the relevant logic to the Execute method for creating a Survey List in the specified site with the specified survey list name.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
CreateSurveyLibrary();
return ActivityExecutionStatus.Closed;
}
 
 
private void CreateSurveyLibrary()
{
using (SPSite oSPSite = new SPSite(SiteUrl))
{
using (SPWeb oSPWeb = oSPSite.RootWeb)
{
Guid ID = oSPWeb.Lists.Add(SurveyListName, SurveyListName + System.DateTime.Now.ToString(),
SPListTemplateType.Survey);
 
SPList oSPList = oSPWeb.Lists[ID];
oSPList.OnQuickLaunch = true;
oSPList.Update();
}
}
}
Create a strong name for the assembly ‘CreateActivityDemo’.
Right click on the CustomWorkflowActivityDemo project and Add ---> SharePoint Mapped Folder
Navigate to Template1033Workflow and Select it.
custom activity4



Create an XML file called ‘CreateActivityDemo.Actions’ under the workflow folder. Complete the definition of ‘CreateActivityDemo.Actions’.The definition of ‘.Actions’ file is responsible for making the custom workflow activity appear in the SharePoint Designer 2010.
<WorkflowInfo>
  <Actions Sequential="then" Parallel="and">
    <Action Name="Create Survey List"
        ClassName="CreateActivityDemo.CreateSurveyList"
        Assembly="CreateActivityDemo, Version=1.0.0.0,
           Culture=neutral, PublicKeyToken=38b1d60938e39f46"
        AppliesTo="all"
        Category="Sundar Activity">
      <RuleDesigner Sentence="Survey List Name %1 to site %2.">
        <FieldBind Field="SurveyListName" Text="Survey List Name"
           DesignerType="TextArea" Id="1"/>
        <FieldBind Field="SiteUrl" Text="Url of base site" Id="2"
           DesignerType="TextArea"/>
      </RuleDesigner>
      <Parameters>
        <Parameter Name="SurveyListName" Type="System.String, mscorlib"
      Direction="In" />
        <Parameter Name="SiteUrl" Type="System.String, mscorlib"
      Direction="In" />
      </Parameters>
    </Action>
  </Actions>
</WorkflowInfo>

Next, we need to add the assembly for custom workflow activity in the Package.
Double click on the Package.package
custom activity5
Click ‘Advanced’ in Package Designer.
Add a Safe Control for Workflow Activity Assembly ‘CreateActivityDemo’.
custom activity6
 
Create an authorized type entry for the CreateActivityDemo assembly (custom workflow activity assembly).
<authorizedType Assembly="CreateActivityDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=38b1d60938e39f46" Namespace="CreateActivityDemo"
 TypeName="*" Authorized="True" />
Deploy the Solution. Now we’ll see the custom activity ‘Create Survey List’ appearing in the SharePoint Designer 2010 under actions.

custom activity7

 

 

The custom workflow activity  ‘Create Survey List’ is ready to be tested.

 

Create a re-usable workflow and drop the ‘Create Survey List’ activity.

 

Set the Values for SiteUrl and SurveyListName.

custom activity8[5]

 

Start the Workflow.

 

custom activity 9

 

Now the Survey is created in the site with defined name.

 

custom activity 10

This completes the development of custom workflow activity.

No comments:

Post a Comment