2015-09-09

Updating Workflow Definitions Without Terminating Current Instances

I haven't seen anywhere a good tutorial on how to update a workflow definition without terminating currently running instances on SharePoint 2013 Workflow Manager platform so this post is my contribution to the community. The goal of this post is to enable the following series of steps:
  1. Update workflow definition XAMLs (for example from version 1 to version 2).
  2. Allow currently running instances of workflow to finish running the version 1 of the workflow.
  3. New instances of workflow will run version 2 of the workflow. Version 1 of the workflow shouldn't be allowed to run any more.
Let's suppose you've created an empty SharePoint project in Visual Studio and you add a workflow to it. This workflow consists of one SingleTask activity. You run your workflow which consists of single task activity and now your workflow instance is blocking and waiting for this task to end. Now, let's suppose you add WriteToHistory activity to your workflow in Visual Studio. You leave workflow property Deployment Conflict Resolution on Automatic value and deploy your new version of the workflow using Visual Studio. You will see that running instance of the workflow gets terminated. If you change Deployment Conflict Resolution to None workflow definition will not get updated. If you try to deploy your WSP using PowerShell scripts (Add-SPSolution, Install-SPSolution, Enable-SPFeature, ...) the result will be the same, i.e. workflow definition will not get updated.

There are some examples on how to update Workflow Manager workflows on MSDN but these examples are strictly tied to Workflow Manager platform, not Workflow Manager in combination with SharePoint 2013. My solution is based on aforementioned MSDN article but it is adapted to SharePoint 2013.

Prerequisite for my solution is at least one deploy of the workflow using either Visual Studio deploy or using PowerShell scripts. Workflow needs to be deployed as a part of the feature only first time.

For part 1 of this post I will demonstrate how to publish new versions of workflow using console application. I also presume you are developing on development box with Visual Studio installed. In part 2 I will focus on packaging the deployment in feature receiver.
  1. Create new console application (.NET 4.5) project
  2. Add the following references to the project (not all are needed, but I didn't filter out unneeded references):
  3. Add WorkflowManagementClientExtensions class to the project:
    //------------------------------------------------------------
    // Copyright (c) Microsoft Corporation.  All rights reserved.
    //------------------------------------------------------------
    
    using Microsoft.Activities;
    using Microsoft.Activities.Messaging;
    using Microsoft.Workflow.Client;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Threading;
    
    namespace Microsoft.Workflow.Samples.Common
    {
        public static class WorkflowManagementClientExtensions
        {
            public static void PublishActivity(this WorkflowManagementClient client, string name, string xamlFilePath)
            {
                client.Activities.Publish(
                    new ActivityDescription(WorkflowUtils.Translate(xamlFilePath))
                    {
                        Name = name
                    });
            }
    
            public static void PublishWorkflow(this WorkflowManagementClient client, string workflowName, string xamlFilePath, SubscriptionFilter activationFilter = null)
            {
                PublishWorkflow(client, workflowName, xamlFilePath, null, null, activationFilter);
            }
    
            public static void PublishWorkflow(this WorkflowManagementClient client, string workflowName, string xamlFilePath, Collection<ExternalVariable> externalVariables, SubscriptionFilter activationFilter = null)
            {
                PublishWorkflow(client, workflowName, xamlFilePath, externalVariables, null, activationFilter);
            }
    
            public static void PublishWorkflow(this WorkflowManagementClient client, string workflowName, string xamlFilePath, IDictionary<string, string> configValues, SubscriptionFilter activationFilter = null)
            {
                PublishWorkflow(client, workflowName, xamlFilePath, null, configValues, activationFilter);
            }
    
            public static void PublishWorkflow(this WorkflowManagementClient client, string workflowName, string xamlFilePath, Collection<ExternalVariable> externalVariables, IDictionary<string, string> configValues, SubscriptionFilter activationFilter = null)
            {
                // publish the activity description related with the workflow
                client.Activities.Publish(
                    new ActivityDescription(WorkflowUtils.Translate(xamlFilePath)) { Name = workflowName });
    
                // now, publish the workflow description
                WorkflowDescription description = new WorkflowDescription
                {
                    Name = workflowName,
                    ActivityPath = workflowName,
                };
    
                // add external variables
                if (externalVariables != null)
                {
                    externalVariables
                        .ToList()
                        .ForEach(ev => description.ExternalVariables.Add(ev));
                }
    
                // add config
                if (configValues != null)
                {
                    description.Configuration = new WorkflowConfiguration();
                    configValues
                        .ToList()
                        .ForEach(c => description.Configuration.AppSettings.Add(c));
                }
    
                // add activation filter
                if (activationFilter != null)
                {
                    description.ActivationDescription = new SubscriptionActivationDescription
                    {
                        Filter = activationFilter
                    };
                }
    
                // publish!
                client.Workflows.Publish(description);
            }
    
            public static void CleanUp(this WorkflowManagementClient client)
            {
                client.CurrentScope.Delete();
            }
    
            public static string WaitForWorkflowCompletion(this WorkflowManagementClient client, string workflowName, string instanceId, int pollingInterval = 0)
            {
                string currentStatus = string.Empty;
                string lastStatus = string.Empty;
    
                WorkflowInstanceInfo instanceInfo = client.Instances.Get(workflowName, instanceId);
    
                while (true)
                {
                    instanceInfo = client.Instances.Get(workflowName, instanceId);
    
                    currentStatus = instanceInfo.UserStatus;
    
                    if (currentStatus != lastStatus && !string.IsNullOrWhiteSpace(currentStatus))
                    {
                        Console.Write("   Current Status: ");
                        WorkflowUtils.Print(currentStatus, ConsoleColor.Cyan);
                        lastStatus = currentStatus;
                    }
    
                    if (instanceInfo.WorkflowStatus == WorkflowInstanceStatus.Started || instanceInfo.WorkflowStatus == WorkflowInstanceStatus.NotStarted)
                    {
                        Thread.Sleep(pollingInterval);
                        continue;
                    }
    
                    if (instanceInfo.WorkflowStatus == WorkflowInstanceStatus.Completed)
                    {
                        Console.WriteLine("\nWorkflow instance completed");
                    }
    
                    break;
                }
    
                return instanceInfo.UserStatus;
            }
        }
    } 
  4. Add WorkflowUtils class to the project. Notice that Translate method is loading assembly from Visual Studio PublicAssemblies folder:
    //------------------------------------------------------------
    // Copyright (c) Microsoft Corporation.  All rights reserved.
    //------------------------------------------------------------
    
    using System;
    using System.Text;
    using System.Xaml;
    using System.Xml;
    using System.Xml.Linq;
    using Microsoft.Activities.Design.ExpressionTranslation;
    using Microsoft.Workflow.Client;
    using System.Reflection;
    
    namespace Microsoft.Workflow.Samples.Common
    {
        public class WorkflowUtils
        {
            public static WorkflowManagementClient CreateForSample(string rootScope, string scopeName)
            {
                var rootClient = new WorkflowManagementClient(new Uri(rootScope));
    
                return rootClient.CurrentScope.PublishChildScope(scopeName,
                    new ScopeDescription()
                    {
                        UserComments = string.Format("For {0} sample only", scopeName)
                    });
            }
    
            public static XElement Translate(string xamlFile)
            {
                string translatedWorkflowString = null;
    
                Assembly wfAssembly = Assembly.LoadFile(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PublicAssemblies\Microsoft.SharePoint.DesignTime.Activities.dll");
                XamlXmlReaderSettings settings = new XamlXmlReaderSettings();
                settings.LocalAssembly = wfAssembly;
    
                using (XamlReader xamlReader = new XamlXmlReader(xamlFile, settings))
                {
                    TranslationResults result = ExpressionTranslator.Translate(xamlReader);
                    if (result.Errors.Count == 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        using (XmlWriter xmlWriter = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
                        {
                            using (XamlXmlWriter writer = new XamlXmlWriter(xmlWriter, result.Output.SchemaContext))
                            {
                                XamlServices.Transform(result.Output, writer);
                            }
                        }
                        translatedWorkflowString = sb.ToString();
                    }
                    else
                    {
                        throw new InvalidOperationException("Translation errors");
                    }
                }
    
                return XElement.Parse(translatedWorkflowString);
            }
    
            public static void PrintDone()
            {
                Print("[Done]", ConsoleColor.Green);
            }
    
            public static void Print(string message, ConsoleColor color)
            {
                ConsoleColor currentColor = Console.ForegroundColor;
                Console.ForegroundColor = color;
                Console.WriteLine(message);
                Console.ForegroundColor = currentColor;
            }
        }
    }
  5. Now you need to find out the GUID of the module which was used in deploying workflow (prerequisite). This GUID is located in the Module element, Url attribute in Elements.xml file in workflow folder. For example, in Elements.xml file:
    <?xml version="1.0" encoding="utf-8" ?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="Workflow1" Url="wfsvc/86c7e42d4fe14b839d55fcbb6dde7b9c">
        <File Url="Workflow.xaml" Type="GhostableInLibrary" Path="Workflow1\Workflow.xaml" DoGUIDFixUp="TRUE">
          <Property Name="ContentType" Value="WorkflowServiceDefinition" />
          <Property Name="isReusable" Value="true" />
          <Property Name="RequiresInitiationForm" Value="False" />
          <Property Name="RequiresAssociationForm" Value="False" />
          <Property Name="WSPublishState" Value="3" />
          <Property Name="WSDisplayName" Value="Workflow1" />
          <Property Name="WSDescription" Value="My 'Workflow1' Workflow" />
          <!-- If you change the name or Url of your custom initiation or association form, 
               remember to update the corresponding property value (InitiationUrl or AssociationUrl) to match the new web relative url.
          -->
          <Property Name="RestrictToType" Value="List" />
          <Property Name="RestrictToScope" Value="{$ListId:Shared Documents;}" />
        </File>
        <File Url="WorkflowStartAssociation" Path="Workflow1\WorkflowStartAssociation" Type="GhostableInLibrary">
          <Property Name="WSDisplayName" Value="Workflow1 - Workflow Start" />
          <Property Name="ContentType" Value="WorkflowServiceSubscription" />
          <Property Name="WSPublishState" Value="3" />
          <Property Name="WSEventType" Value="WorkflowStart" />
          <Property Name="WSEnabled" Value="true" />
          <Property Name="WSGUID" Value="19cbf8a6-4af8-462c-b014-c8d7c0e327c1" />
          <Property Name="WSEventSourceGUID" Value="{$ListId:Shared Documents;}" />
          <Property Name="Microsoft.SharePoint.ActivationProperties.ListId" Value="{$ListId:Shared Documents;}" />
          <Property Name="HistoryListId" Value="{$ListId:Lists/WorkflowHistoryList;}" />
          <Property Name="TaskListId" Value="{$ListId:Lists/WorkflowTaskList;}" />
        </File>
      </Module>
      <ListInstance FeatureId="{2c63df2b-ceab-42c6-aeff-b3968162d4b1}"
                    TemplateType="4501"
                    Title="wfsvc"
                    Description="This list instance is used by SharePoint to keep track of workflows. Do not modify."
                    Url="wfsvc"
                    RootWebOnly="FALSE" />
    </Elements>

    this GUID is 86c7e42d4fe14b839d55fcbb6dde7b9c. Remember this GUID, you will need it in the following step.
  6. Now you need to query workflow resource management database to find out the scope path of your workflow. If you left database name with default value during configuration of workflow farm, this query should get you the scope path (replace the GUID in query with GUID from the previous step):
    SELECT [Path]
    FROM [WFResourceManagementDB].[dbo].[Scopes] s
        join [WFResourceManagementDB].[dbo].[Activities] a on s.ScopeId = a.ScopeId
    where a.Name = 'WorkflowXaml_86c7e42d_4fe1_4b83_9d55_fcbb6dde7b9c'
  7. Now it's time to change x:Class attribute in the workflow.xaml file. Last token in the attribute should contain GUID from step 5. For example: x:Class="In2.Ilas.Sp.Ecase.PukWf.WorkflowXaml_ed00d3bd_4796_41ba_b288_35ce2226f89a"
  8. Finally, console application should have the following code (replace the path from step 6 and workflow name from step 5):
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.WorkflowServices;
    using Microsoft.Workflow.Client;
    using Microsoft.Workflow.Samples.Common;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                WorkflowManagementClient client = new WorkflowManagementClient("http://zg-sp2013-02:12291/SharePoint/default/030e8465-c66c-4f9f-83af-e64d5a4a799b/61aa1fff-103e-49c8-bc56-395c27ca7f81");
                client.Activities.Publish(new ActivityDescription(WorkflowUtils.Translate("Workflow.xaml")) { Name = "WorkflowXaml_ed00d3bd_4796_41ba_b288_35ce2226f89a" });
            }
        }
    } 
  9. Copy Workflow.xaml file to console application's folder.
  10. Run the application.
Now, old and already running instances will continue using old workflow definition while new instances will run using new workflow definition.

Complete solution is available for download.

List of types in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\PublicAssemblies\Microsoft.SharePoint.DesignTime.Activities.dll assembly:

namespace Microsoft.SharePoint.DesignTime.Activities
{
    [ComVisible(false)]
    public class AppOnlySequence : Activity, ISupportInitialize

    [ComVisible(false)]
    public class AtomicTaskItemUpdatedHelper : Activity, ISupportInitialize

    [ComVisible(false)]
    public class BuildSPListItemWebLink : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class BuildSPUri : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class Calc : Activity, ISupportInitialize

    [ComVisible(false)]
    public class CallHTTPWebService : Activity, ISupportInitialize

    [ComVisible(false)]
    public class CheckInItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class CheckOutItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class Comment : Activity, ISupportInitialize

    [ComVisible(false)]
    public class CompositeTask : Activity, ISupportInitialize

    [ComVisible(false)]
    public class CompositeTaskHelper : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class ConvertPropertiesForSPListItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class ConvertTimeZoneFromSPLocalToUtc : Activity<DateTime>, ISupportInitialize

    [ComVisible(false)]
    public class ConvertTimeZoneFromUtcToSPLocal : Activity<DateTime>, ISupportInitialize

    [ComVisible(false)]
    public class CopyItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class CreatedBy : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class CreatedInRange : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class CreateListItem : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class DateInterval : Activity<double>, ISupportInitialize

    [ComVisible(false)]
    public class DefaultRetryPolicy : Activity, ISupportInitialize

    [ComVisible(false)]
    public class DelayFor : Activity, ISupportInitialize

    [ComVisible(false)]
    public class DelayUntil : Activity, ISupportInitialize

    [ComVisible(false)]
    public class DeleteListItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class Email : Activity, ISupportInitialize

    [ComVisible(false)]
    public class ExpandGroupToUsers : Activity<Collection<string>>, ISupportInitialize

    [ComVisible(false)]
    public class ExpandInitFormUsers : Activity<Collection<string>>, ISupportInitialize

    [ComVisible(false)]
    public class ExtractSubstringFromEnd : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class ExtractSubstringFromIndex : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class ExtractSubstringFromIndexLength : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class ExtractSubstringFromStart : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class GenerateEmailDynamicValue : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class GetCurrentItemGuid : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class GetCurrentItemId : Activity<int>, ISupportInitialize

    [ComVisible(false)]
    public class GetCurrentListId : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class GetHistoryListId : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class GetItemIdInCache : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class GetTaskListId : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class IsNull : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsSPListItemPropertyLookupToDocsField : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsValidUser : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class JoinChoicesFromSPFieldMultiChoice : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class JoinIdOrValueFromSPFieldLookupMulti : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class JoinSiteUserInfoListPropertyFromSPFieldUserMulti : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class JoinSPPrincipalPropertyFromInitFormParamUserMulti : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPChoiceFieldIndex : Activity<int>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPField : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPFields : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPGroup : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPGroupMembers : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPList : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItem : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemBooleanProperty : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemDateTimeProperty : Activity<DateTime>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemDoubleProperty : Activity<double>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemDynamicValueProperty : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemGuid : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemId : Activity<int>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemInt32Property : Activity<int>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemProperty : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemPropertyNameInREST : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemSPFieldLookupMultiProperty : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemSPFieldLookupProperty : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListItemStringProperty : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPListProperty : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPPrincipal : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPPrincipalId : Activity<int>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPPrincipalProperty : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPTaskAssignedToDisplayName : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPTaskListItemAssignedTo : Activity<Collection<string>>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPUser : Activity<DynamicValue>, ISupportInitialize

    [ComVisible(false)]
    public class LookupSPUserProperty : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LookupWorkflowContextProperty : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class LoopNTimes : Activity, ISupportInitialize

    [ComVisible(false)]
    public class ModifiedBy : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class ModifiedInRange : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class RegisterForSharePointEvent : Activity, ISupportInitialize

    [ComVisible(false)]
    public class ReplaceEmailTokens : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class RetryForDurationPolicy : Activity, ISupportInitialize

    [ComVisible(false)]
    public class SetField : Activity, ISupportInitialize

    [ComVisible(false)]
    public class SetItemIdInCache : Activity, ISupportInitialize

    [ComVisible(false)]
    public class SetRelatedItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class SetTimeField : Activity<DateTime>, ISupportInitialize

    [ComVisible(false)]
    public class SetWorkflowStatus : Activity, ISupportInitialize

    [ComVisible(false)]
    public class SingleTask : Activity, ISupportInitialize

    [ComVisible(false)]
    public class TranslateDocument : Activity, ISupportInitialize

    [ComVisible(false)]
    public class UndoCheckOutItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class UpdateListItem : Activity, ISupportInitialize

    [ComVisible(false)]
    public class WaitForCustomEvent : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class WaitForFieldChange : Activity, ISupportInitialize

    [ComVisible(false)]
    public class WaitForItemEvent : Activity, ISupportInitialize

    [ComVisible(false)]
    public class WebUri : Activity<string>, ISupportInitialize

    [ComVisible(false)]
    public class WordsInTitle : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class WorkflowInterop : Activity<Guid>, ISupportInitialize

    [ComVisible(false)]
    public class WriteToHistory : Activity, ISupportInitialize
}

namespace Microsoft.SharePoint.DesignTime.Activities.Expressions
{
    [ComVisible(false)]
    public class ContainsStringIgnoreCase : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsEqualDate : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsEqualDateTime : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsEqualDynamicValue : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsEqualStringIgnoreCase : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsEqualUser : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsGreaterThanDateTime : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsGreaterThanOrEqualDateTime : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsLessThanDateTime : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class IsLessThanOrEqualDateTime : Activity<bool>, ISupportInitialize

    [ComVisible(false)]
    public class MatchesString : Activity<bool>, ISupportInitialize
}

namespace XamlStaticHelperNamespace
{
    [GeneratedCode("XamlBuildTask", "4.0.0.0")]
    internal class _XamlStaticHelper
}



2015-08-19

Failed to load receiver assembly, System.IO.FileNotFoundException: Could not load file or assembly or one of its dependencies on Feature Receiver in SharePoint 2013

After breaking the deploy process by pressing CTRL+BREAK during Visual Studio 2012 deploy of SharePoint 2013 WSP I wasn't able to deploy my package any more. Deploy was failing with the message:

Failed to load receiver assembly...

After fiddling with Process Monitor and Fusion Log for a while I couldn't pinpoint why assembly couldn't be loaded. Assembly exists at appropriate location in GAC but my concern is that Visual Studio tries to access it few seconds before it is actually copied to GAC during deploy process. Aforementioned exception happens during feature activation phase of deploy process.

My solution to the problem was to change assembly version from 1.0.0.0 to 1.0.0.1 and build the SharePoint project. After that I I deleted feature receiver and added feature receiver again so that feature manifest XML file gets updated with correct version of feature receiver assembly.

2014-12-11

Maximum Number of Variables per Activity

This issue has been raised on SharePoint StackExchange.

I am currently developing a Workflow 2013 in VS 2012 as a part of Full trust solution. I added a variable to the workflow and tried to deploy my solution but VS threw an error:
Error occurred in deployment step 'Activate Features': Microsoft.Workflow.Client.ActivityNotFoundException: The activity named 'WorkflowXaml_ed00d3bd_4796_41ba_b288_35ce2226f89a' from scope '/SharePoint/default/248fadea-e82b-4d15-bf03-d00b7947ca36/a6b8142a-8120-4823-a855-f400460ec143' was not found. HTTP headers received from the server - ActivityId: 56a026d5-261b-443e-bada-30ceb3417263. NodeId: ZG-SP2013-02. Scope: /SharePoint/default/248fadea-e82b-4d15-bf03-d00b7947ca36/a6b8142a-8120-4823-a855-f400460ec143. Client ActivityId : f86d411a-425b-42ab-8
After fiddling around with this issue for a few hours, I was able to locate more detailed exception message in ULS. ULS records have to have filter Level=Exception:
Workflow XAML failed validation due to the following errors:
Activity 'Sequence' has 51 variables, which exceeds the maximum number of variables per activity (50).
 HTTP headers received from the server - ActivityId: 2bb3ba9e-28d3-4820-91a7-f03ebdb98fdf. NodeId: ZG-SP2013-02. Scope: /SharePoint/default/248fadea-e82b-4d15-bf03-d00b7947ca36/a6b8142a-8120-4823-a855-f400460ec143. Client ActivityId : e87640d8-8819-4981-bbdb-c46c02eb6d6f.
I counted variables in my workflow and indeed there are 51 variable defined in the workflow. All of the variables are defined in root Sequence. Unfortunately, this is not documented in Workflow limitations web page on MSDN.

Solution:
Reorganize variables in multiple Sequences and reuse existing variables where possible.

2014-10-29

Visual Studio 2013 Cannot Deploy Content Type Updates

After spending 10+ hours trying to figure out why content types included in my wsp cannot be deployed using Visual Studio 2013 deploy without recreating SP site I must say I am disappointed with my findings. Content types cannot be removed from SP site by using Visual Studio 2013 Retract command. My colleague can successfully deploy (and retract) content types using Visual Studio 2012, so I decided to look inside what is really happening under the hood.

Apparently, both Visual Studio versions, 2012 and 2013, are using Microsoft.VisualStudio.SharePoint.Commands.Implementation.V5.dll to deploy and retract SharePoint solutions to SharePoint sites. But the big difference between the two is that VS 2012 implements Microsoft.VisualStudio.SharePoint.Commands.DeploymentManager.DeactivateFeatures in a way so that it calls SPFeatureCollection.Remove method with force argument set to true which causes a call to stored procedure proc_DeactivateContentTypeInScope with SQL parameter @IsDeactivatingFeature set to 2. When this parameter is set to 2 then stored procedure skips a call to another stored procedure named proc_IsContentTypeInUse. This basically forces the deletion of content type.

VS 2013 implements Microsoft.VisualStudio.SharePoint.Commands.DeploymentManager.DeactivateFeatures with a call to Microsoft.VisualStudio.SharePoint.Commands.DeploymentManager.DeactivateFeature method which makes a call to SPFeatureCollection.Remove with force argument set to false which causes a call to stored procedure proc_DeactivateContentTypeInScope with SQL parameter @IsDeactivatingFeature set to 1. This prevents content type from being deleted.

I am using version of VS 2013 which is Visual Studio Premium 2013 Version 12.0.30723.00 Update 3.

I hope MS provides a fix to this issue soon. Until then I am reverting to VS 2012.

2014-09-22

ECB Missing on Manage User Profiles page in Central Administration

Today I was working on SharePoint 2013 dev machine when I was trying to sync AD accounts with SharePoint. I wanted to set emails for accounts to test workflow tasks but I was surprised I couldn't edit user profiles because ECB menus were not rendered on Manage User Profiles (ProfMngr.aspx) page:

Apparently the problem lies in the fact that CA site was browsed with IE 11. To fix this problem, site has to be added to compatibility view list. In Internet Explorer 11 open Tools menu and choose Compatibility View Settings menu item while browsing CA site. The following window should open:

Click the Add button. Window should now look like this:

Close the window and voila, now ECB should be rendered. Notice how grid lines are added to the list:


2014-08-26

Error occurred in deployment step ‘Activate Features': System.TimeoutException: The HTTP request has timed out after 20000 milliseconds.

I am running SP 2013 dev environment and I recently ran into a problem described in the post title. I tried to deploy a solution with feature activation which included SP 2013 Workflow SPIs. I tried to solve the issue by applying a solution described here but it wasn't helpful in my situation. I already had registry settings set up to extend timeout period for SharePoint deployment.

Visual studio output window might look like this:
  Activating feature 'Feature1' ...
Error occurred in deployment step 'Activate Features': System.TimeoutException: The HTTP request has timed out after 20000 milliseconds. ---> System.Net.WebException: The request was aborted: The request was canceled.
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at Microsoft.Workflow.Client.HttpGetResponseAsyncResult`1.OnGotResponse(IAsyncResult result)
   --- End of inner exception stack trace ---
   at Microsoft.Workflow.Common.AsyncResult.End[TAsyncResult](IAsyncResult result)
   at Microsoft.Workflow.Client.Ht


I noticed that I had Fiddler2 running in the background while trying to deploy my solution. I exited Fiddler2 application and tried to deploy again. This time deploy was successful. It very well might be a cause of error. Just to be sure I tried to reproduce the same behavior again by starting Fiddler2 again and trying to deploy the wsp using Visual Studio 2012. This time VS couldn't delete workflows and workflow associations from the site but it was able to deploy successfully. I turned off Fiddler2 again and tried to deploy, and it went smooth, without any errors. I might be wrong but it seems to me that Fiddler's proxy is interfering with deployment of workflows.

I know there are many reasons causing this error but this solution helped me. Hopefully it helps someone else.

2014-08-20

Implementation of ribbon button availability by calling asynchronous methods

It is known that SharePoint ribbon can be extended by implementing CustomAction Ribbon.Library.Actions.AddAButton. SharePoint allows you to dynamically control whether this ribbon button should be enabled. This is done by implementing Javascript logic in CommandUIHandler's EnabledScript attribute.

The problem is if you want to include asynchronous call in aforementioned attribute because this Javascript block must return boolean value. So, when SharePoint calls asynchronous method it will not get the result immediately. Instead, it will get the result of async call in callback function but it's too late, Javascript block has already returned.

The solution to this problem was very well described by Andrew Connell on his blog post. Since no code snippets were included in this post I decided to provide it here because there is one caveat that is not so obvious from first look at the Andrew's post.

This is the implementation of ribbon button in Elements.xml:

    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Library.Share.Controls._children">
          <Button Id="Ribbon.Library.Share.NewRibbonButton"
                  Command="NewRibbonButtonCommand"
                  Image16by16="/_layouts/15/Style/buttonIcon16.png"
                  Image32by32="/_layouts/15/Style/buttonIcon32.png"
                  LabelText="New case"
                  ToolTipTitle="Get new case."
                  ToolTipDescription="Assigns new case to you."
                  TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="NewRibbonButtonCommand"
          EnabledScript="javascript:IsCurrentUserMemberOfGroup('puk ref');       
       "/>
      </CommandUIHandlers>
    </CommandUIExtension>

Since our project doesn't include customized forms nor master pages we had to include external Javascript file to Elements.xml:

  <CustomAction
   ScriptSrc="/_layouts/15/Scripts/SPRibbonHelperScript.js"
   Location="ScriptLink"
   Sequence="1001">
  </CustomAction>

This makes it easier to debug the solution if Javascript code is included in external file. Prerequisite for this step is to add new Javascript file to layouts mapped folder.

Next piece of code is the actual implementation of enabling/disabling the button based on current user's group membership in SPRibbonHelperScript.js:

var isMemberOfGroup = false;

function IsCurrentUserMemberOfGroup(groupName) {
    var currentContext = new SP.ClientContext.get_current();
    var currentWeb = currentContext.get_web();
    var currentUser = currentContext.get_web().get_currentUser();
    currentContext.load(currentUser);

    var allGroups = currentWeb.get_siteGroups();
    currentContext.load(allGroups);

    var group = allGroups.getByName(groupName);
    currentContext.load(group);

    var groupUsers = group.get_users();
    currentContext.load(groupUsers);
    currentContext.executeQueryAsync(OnSuccess, OnFailure);

    function OnSuccess(sender, args) {
        var userInGroup = false;
        var groupUserEnumerator = groupUsers.getEnumerator();
        while (groupUserEnumerator.moveNext()) {
            var groupUser = groupUserEnumerator.get_current();
            if (groupUser.get_id() == currentUser.get_id()) {
                userInGroup = true;
                break;
            }
        }

        if (isMemberOfGroup == false || isMemberOfGroup == 'undefined') {
            if (userInGroup == true) {
                isMemberOfGroup = userInGroup;
                RefreshCommandUI();
            }
        }
    }

    function OnFailure(sender, args) {
    }

    return isMemberOfGroup;
}