Looking at information at:
and
I am trying to create site workflow for Project Server 2016 - that means it's platform type had to be SharePoint 2013 Workflow - Project Server
Like in question on stackexchange, first i've created sample workflow from spd 2013, saved as template and retrived generated xaml to my xaml string.
ProjectContext psContext = new ProjectContext(Strings.PWA_Url);
var web = psContext.Web;
var siteCollection = psContext.Site;
var tasksList = web.Lists.GetByTitle("Project Server Workflow Tasks");
var historyList = web.Lists.GetByTitle("Project Server Workflow History");
string xaml = "<Activity mc:Ignorable="mwaw" x:Class="Test.MTW" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:local="clr-namespace:Microsoft.Office.Project.Server.WorkflowActivities" xmlns:local1="clr-namespace:Microsoft.SharePoint.WorkflowServices.Activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mwaw="clr-namespace:Microsoft.Web.Authoring.Workflow;assembly=Microsoft.Web.Authoring" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Sequence><Sequence><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="InitBlock">InitBlock-7751C281-B0D1-4336-87B4-83F2198EDE6D</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes></Sequence><Flowchart StartNode="{x:Reference __ReferenceID1}"><FlowStep x:Name="__ReferenceID1"><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String" /></mwaw:SPDesignerXamlWriter.CustomAttributes><Sequence><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="StageAttribute">StageContainer-8EDBFE6D-DA0D-42F6-A806-F5807380DA4D</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes><local:EnterProjectStage StageId="296334d9-1621-e711-80c8-00155d014d18"><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="StageAttribute">StageHeader-7FE15537-DFDB-4198-ABFA-8AF8B9D669AE</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes></local:EnterProjectStage><Sequence DisplayName="Inicjalizacja"><local1:SetWorkflowStatus Disabled="False" Status="Entering stage: Inicjalizacja" /></Sequence><local:ExitProjectStageGate><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="StageAttribute">StageFooter-3A59FA7C-C493-47A1-8F8B-1F481143EB08</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes></local:ExitProjectStageGate></Sequence><FlowStep.Next><FlowStep x:Name="__ReferenceID0"><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="Next">4294967294</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes><Sequence><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="StageAttribute">StageContainer-8EDBFE6D-DA0D-42F6-A806-F5807380DA4D</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes><local:EnterProjectStage StageId="e10db6c9-1721-e711-80c8-00155d014d18"><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="StageAttribute">StageHeader-7FE15537-DFDB-4198-ABFA-8AF8B9D669AE</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes></local:EnterProjectStage><Sequence DisplayName="Akceptacja"><local1:SetWorkflowStatus Disabled="False" Status="Entering stage: Akceptacja" /></Sequence><local:ExitProjectStageGate><mwaw:SPDesignerXamlWriter.CustomAttributes><scg:Dictionary x:TypeArguments="x:String, x:String"><x:String x:Key="StageAttribute">StageFooter-3A59FA7C-C493-47A1-8F8B-1F481143EB08</x:String></scg:Dictionary></mwaw:SPDesignerXamlWriter.CustomAttributes></local:ExitProjectStageGate></Sequence></FlowStep></FlowStep.Next></FlowStep><x:Reference>__ReferenceID0</x:Reference></Flowchart></Sequence></Activity>"
psContext.Load(web);
psContext.Load(siteCollection);
psContext.Load(tasksList);
psContext.Load(historyList);
psContext.ExecuteQuery();
WorkflowServicesManager wfServiceManager = new WorkflowServicesManager(psContext, web);
WorkflowDeploymentService deployService = wfServiceManager.GetWorkflowDeploymentService();
WorkflowSubscriptionService subscriptionService = wfServiceManager.GetWorkflowSubscriptionService();
WorkflowDefinition wfDefinition = new WorkflowDefinition(psContext);
wfDefinition.DisplayName = "TestWorkflow";
wfDefinition.Description = "TestWorkflow";
wfDefinition.Xaml = xaml;
wfDefinition.RestrictToType = "Site";
deployService.SaveDefinition(wfDefinition);
psContext.Load(wfDefinition, i => i.Id);
psContext.ExecuteQuery();
Guid subscriptionID = Guid.NewGuid();
WorkflowSubscription subscription = new WorkflowSubscription(psContext);
subscription.Id = subscriptionID;
subscription.Name = "TestWorkflow";
subscription.DefinitionId = wfDefinition.Id;
subscription.EventSourceId = web.Id;
subscription.EventTypes = new List<string>() { "WorkflowStart" };
subscription.SetProperty("HistoryListId", historyList.Id.ToString("B"));
subscription.SetProperty("TaskListId", tasksList.Id.ToString("B"));
subscriptionService.PublishSubscription(subscription);
psContext.ExecuteQuery();
but this gives me an error on last line of code:
Microsoft.SharePoint.Client.ServerException: 'Microsoft.Workflow.Client.ActivityNotFoundException: The activity named 'WorkflowXaml_a2a3ddfc_8e29_488c_a7fa_110ad2e869fd' from scope '/SharePoint/default/ec283308-a234-4501-9df1-99744cf4a5fe/b0993915-25fa-43f6-9839-8aa683f16ef2' was not found. HTTP headers received from the server - ActivityId: e4085f69-addc-4c3e-8bcc-49c2f04daf7c. NodeId: SERVER. Scope: /SharePoint/default/ec283308-a234-4501-9df1-99744cf4a5fe/b0993915-25fa-43f6-9839-8aa683f16ef2. Client ActivityId : fa0ef59d-8566-1053-095'
no clues from here.
I get this to work by sniffing SharePoint Designer 2013 CSOM communication using Fiddler.
The key to get this working are:
Commented properties are all set by SPD, but are not necessary.
Working code: