using static MFiles.Grpc.Samples.MFilesValues; namespace MFiles.Grpc.Samples.Recipes; /// /// Recipe "workflow-state": read a document's workflow states and transitions, then move it to /// another state. /// Arguments: [workflow ID, default 107] [initial state ID, default 137] [target state ID, default 141] /// [--keep] to leave the test document in the vault (it is deleted otherwise). /// public static class WorkflowState { public static async Task RunAsync(MFilesSettings settings, string[] args) { int workflowId = RecipeHelpers.IntArg(args, 0, 107); // "Contract Approval Workflow" int initialState = RecipeHelpers.IntArg(args, 1, 137); // "Draft" int targetState = RecipeHelpers.IntArg(args, 2, 141); // "Rejected" await using var connection = await MFilesConnection.ConnectAsync(settings, RecipeHelpers.ClientName); var objects = new IRPCObjectOperations.IRPCObjectOperationsClient(connection.CallInvoker); // A test document in the workflow: property 38 "Workflow" (value list 7) and // property 39 "Workflow State" (value list 8). var created = await RecipeHelpers.CreateTestDocumentAsync(connection, "workflow-state", Property(38, Lookup(7, workflowId)), Property(39, Lookup(8, initialState))); var objId = created.ObjectInfo.ObjId; Console.WriteLine($"Created {RecipeHelpers.Describe(created)}"); try { // [START workflow-state] // The states and transitions of the workflow, with what this user may select for // this object in its current state. var objVer = ObjVer(objId.Type, objId.ItemId.InternalId, created.VersionInfo.Version.InternalVersion); var workflow = await objects.GetWorkflowStatesAndTransitionsAsync(new GetWorkflowStatesAndTransitionsRequest { WorkflowId = workflowId, CurrentState = Lookup(8, initialState), ObjVer = objVer, }); foreach (var state in workflow.States) Console.WriteLine($" State {state.Id} {state.Name}: selectable {state.CanCurrentUserSelect}"); foreach (var transition in workflow.Transitions) { Console.WriteLine($" Transition {transition.Id} {transition.Name}: {transition.FromStateId} -> " + $"{transition.ToStateId}, selectable {transition.CanCurrentUserSelect}"); } // Change the state by setting property 39 "Workflow State" on the version just read. var updated = await objects.SetPropertiesAsync(new SetPropertiesRequest { ObjVer = objVer, AllowModifyingCheckedInObject = true, FailIfNewerVersionExists = true, Properties = { Property(39, Lookup(8, targetState)) }, }); // [END workflow-state] var state39 = updated.ObjectInfo.LatestVersion.Properties.Single(p => p.PropertyDef == 39); Console.WriteLine($"State is now {RecipeHelpers.FormatValue(state39.Value)}, version " + updated.ObjectInfo.LatestVersion.ObjectVersion.VersionInfo.Version.InternalVersion); } finally { if (!RecipeHelpers.Keep(args)) await RecipeHelpers.DeleteAsync(connection, objId); } } }