this solution is in SharePoint 2016 On Premise. I have activated content approval on document libraries and the RER's I have in my app is handling these events:
- "ItemAdded"
- "ItemUpdated"
- "ItemAdding"
- "ItemUpdating"
- "ItemCheckingOut"
- "ItemDeleting"
In the "ItemUpdading" event I keep track on when a document has been approved or rather when a document is sent to be approved.
// After = "vti_doclibmodstat" Before = "vti_docstoreversion" Standard = "_ModerationStatus"
// 0 = The list item is approved.
// 1 = The list item has been denied approval.
// 2 = The list item is pending approval.
// 3 = The list item is in the draft or checked out state.
// 4 = The list item is scheduled for automatic approval at a future date.
// 2 = The list item is pending approval.
if (currentModStat == "2")
{
// 0 = The list item is approved.
if (afterModStat == "0")
{
// Make sure the current user is the same as the one specified in the field "DocumentApprover"
if (currentUserLoginName != currentDocApprover.LoginName)
{
result.ErrorMessage = "Du får bara godkänna dokument där du står som \"Godkännare\"";
result.Status = SPRemoteEventServiceStatus.CancelWithError;
return;
}
else
{
// Set User and Date of the approval
var dateStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
result.ChangedItemProperties.Add("ApprovedBy", fuvDocApprover.LookupId);
result.ChangedItemProperties.Add("ApprovedDate", dateStr);
result.Status = SPRemoteEventServiceStatus.Continue;
return;
}
}
}
The solution is working quite well but there's one odd problem when working with newer Office files and declaring them as records through CSOM and manually via GUI.
// Declare item as record
Records.DeclareItemAsRecord(clientContext, listItem);
clientContext.ExecuteQuery();
The fields I have trouble with:
- StaticName="ApprovedBy" DisplayName="Godkänt av"
- StaticName="ApprovedDate" DisplayName="Godkänt datum"
These are custom fields within the list template that I use for document libraries. When a document is in the final stage of approval the "ItemUpdating" event is passing on "ChangedItemProperties" and this works fine with older files but when declaring newer files the values vanishes.
If I pause with debugger in Visual Studion on the last line below
Records.DeclareItemAsRecord(clientContext, listItem);
clientContext.ExecuteQuery();
I can refresh my browser and see that the values are there, however if I then press F5 and execute the query, the "lock" gets applied, item is declared as record but the values disappears.
I think this might have something to do with system language (LCID 1033) as I can still see the values in SharePoint Management Shell when fetching the list item. I have changed the LCID to 1053 in PowerShell before by changing the current threads culture but no luck doing so in C#.
Any help in this matter would be greatly appreciated!