using static MFiles.Grpc.Samples.MFilesValues; namespace MFiles.Grpc.Samples.Recipes; /// /// Recipe "update-properties": change properties of a document two ways: in one call with an /// explicit version, and with check out, set, check in. /// Arguments: [--keep] to leave the test document in the vault (it is deleted otherwise). /// public static class UpdateProperties { public static async Task RunAsync(MFilesSettings settings, string[] args) { await using var connection = await MFilesConnection.ConnectAsync(settings, RecipeHelpers.ClientName); var objects = new IRPCObjectOperations.IRPCObjectOperationsClient(connection.CallInvoker); var created = await RecipeHelpers.CreateTestDocumentAsync(connection, "update-properties"); var objId = created.ObjectInfo.ObjId; int version = created.VersionInfo.Version.InternalVersion; Console.WriteLine($"Created {RecipeHelpers.Describe(created)}"); try { // [START update-properties] // One call: the server checks the object out, sets the values and checks it in. // Name the version you read; with fail_if_newer_version_exists the call fails instead // of overwriting a change someone else made after that version. var updated = await objects.SetPropertiesAsync(new SetPropertiesRequest { ObjVer = ObjVer(objId.Type, objId.ItemId.InternalId, version), AllowModifyingCheckedInObject = true, FailIfNewerVersionExists = true, // Other properties stay as they are (remove_unspecified_properties is false). Properties = { Property(1004, Text("grpc, sample")) }, // Keywords }); version = updated.ObjectInfo.LatestVersion.ObjectVersion.VersionInfo.Version.InternalVersion; Console.WriteLine($"Set Keywords: now version {version}"); // [END update-properties] // [START check-out-in] // Check out, set, check in: several changes become one new version, and nobody else // can change the object meanwhile. var checkedOut = await objects.CheckOutAsync(new CheckOutRequest { ObjId = objId }); var workingVersion = checkedOut.ObjectInfo.ObjectVersion.VersionInfo.Version; Console.WriteLine($"Checked out: working version {workingVersion.InternalVersion}"); try { var working = ObjVer(objId.Type, objId.ItemId.InternalId, workingVersion.InternalVersion); await objects.SetPropertiesAsync(new SetPropertiesRequest { ObjVer = working, Properties = { Property(1004, Text("grpc, sample, checked out")), // Keywords Property(1002, Date(DateOnly.FromDateTime(DateTime.UtcNow))), // Document date }, }); var checkedIn = await objects.CheckInAsync(new CheckInRequest { ObjVer = working }); version = checkedIn.ObjectInfo.LatestVersion.ObjectVersion.VersionInfo.Version.InternalVersion; Console.WriteLine($"Checked in: now version {version}"); } catch { // Do not leave the object checked out. await objects.UndoCheckoutAsync(new UndoCheckoutRequest { ObjVer = ObjVer(objId.Type, objId.ItemId.InternalId, workingVersion.InternalVersion), }); throw; } // [END check-out-in] // Show the result. var result = await objects.GetObjectInfoAndPropertiesAsync(new GetObjectInfoAndPropertiesRequest { ObjId = objId, LatestVersion = true, }); foreach (var property in result.PropertyArray.Where(p => p.PropertyDef is 1004 or 1002)) Console.WriteLine($" Property {property.PropertyDef}: {RecipeHelpers.FormatValue(property.Value)}"); } finally { if (!RecipeHelpers.Keep(args)) await RecipeHelpers.DeleteAsync(connection, objId); } } }