/// <summary> /// Method to fetch all the versions metadata /// </summary> /// <param name="searchDocumentRequest">Request model for Search Operation</param> /// <param name="LoggerId">Message Id used for logging information</param> /// <returns></returns> public SearchDocumentAllMetaDataVersions DDMSSearchAllOldVersions(SearchDocumentRequest searchDocumentRequest, string LoggerId) { SearchDocumentAllMetaDataVersions searchDocumentAllMetaDataVersions = new SearchDocumentAllMetaDataVersions(); //create a list of search response List <SearchDocumentAllVersionsResponse> listSearchDocumentResponse = new List <SearchDocumentAllVersionsResponse>(); SecureString secureString = null; try { Log.DebugFormat("In DDMSSearchAllOldVersions method for MessageId - {0}", LoggerId); using (ClientContext clientContext = new ClientContext(ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOSiteURL))) { //Get SPO credentials secureString = new NetworkCredential("", CommonHelper.Decrypt(ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOPassword), ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOPasswordKey), ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOPasswordIv))).SecurePassword; //Decrypt the user name and password information String username = CommonHelper.Decrypt(ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOUserName), ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOUserNameKey), ConfigurationManager.AppSettings.Get(ConfigurationConstants.SPOUserNameIv)); clientContext.Credentials = new SharePointOnlineCredentials(username, secureString); //retrieve file based on DocumentId Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileById(searchDocumentRequest.DocumentId); //load list item fields ListItem listItem = file.ListItemAllFields; //load all the list item versions ListItemVersionCollection versions = listItem.Versions; clientContext.Load(file); //fetch the specific metadata clientContext.Load(listItem, item => item[SpoConstants.Name], item => item[SpoConstants.DealerNumber], item => item[SpoConstants.RequestUser], item => item[SpoConstants.Version]); clientContext.Load(versions); clientContext.ExecuteQueryWithRetry(Convert.ToInt32(ConfigurationManager.AppSettings.Get(ExecuteQueryConstants.RetryCount)), Convert.ToInt32(ConfigurationManager.AppSettings.Get(ExecuteQueryConstants.RetryDelayTime)), LoggerId); Log.DebugFormat("In DDMSSearchAllOldVersions method after ExecuteQueryWithRetry for MessageId - {0}", LoggerId); //loop through all versions and fetch metadata foreach (var version in versions) { //assign the metadata of each version to search document response model SearchDocumentAllVersionsResponse searchDocumentResponse = new SearchDocumentAllVersionsResponse(); foreach (var item in version.FieldValues) { if (item.Key == SpoConstants.Name && item.Value != null) { searchDocumentResponse.DocumentName = item.Value.ToString(); } if (item.Key == SpoConstants.DealerNumber && item.Value != null) { searchDocumentResponse.DealerNumber = item.Value.ToString(); } if (item.Key == SpoConstants.RequestUser && item.Value != null) { searchDocumentResponse.RequestUser = item.Value.ToString(); } if (item.Key == SpoConstants.Version && item.Value != null) { searchDocumentResponse.Version = item.Value.ToString(); } } listSearchDocumentResponse.Add(searchDocumentResponse); } } Log.DebugFormat("Out of DDMSSearchAllOldVersions method for MessageId - {0}", LoggerId); searchDocumentAllMetaDataVersions.SearchMetadata = listSearchDocumentResponse; } catch (WebException e) when(e.Status == WebExceptionStatus.NameResolutionFailure) { Log.ErrorFormat("WebException in DDMSSearchAllOldVersions method for MessageId - {0} :{1}", LoggerId, e.Message); searchDocumentAllMetaDataVersions.ErrorMessage = ErrorMessage.RemoteName; } catch (ServerException ex) { Log.ErrorFormat("ServerException in DDMSSearchAllOldVersions method for MessageId - {0} :{1}", LoggerId, ex.Message); if (ex.ServerErrorTypeName == ErrorException.SystemIoFileNotFound) { searchDocumentAllMetaDataVersions.ErrorMessage = ErrorMessage.FileNotFound; } else { searchDocumentAllMetaDataVersions.ErrorMessage = ex.Message; } } catch (Exception ex) { Log.ErrorFormat("Exception in DDMSSearchAllOldVersions method for MessageId - {0} :{1}", LoggerId, ex.Message); searchDocumentAllMetaDataVersions.ErrorMessage = ex.Message; } return(searchDocumentAllMetaDataVersions); }
public static void ProcessChanges(string listID, ChangeToken lastChangeToken, DurableOrchestrationClient client, ILogger log) { using (var cc = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(ConfigurationManager.AppSettings["siteUrl"], ConfigurationManager.AppSettings["clientId"], ConfigurationManager.AppSettings["clientSecret"])) { ChangeQuery changeQuery = new ChangeQuery(false, false); changeQuery.Item = true; changeQuery.Update = true; // could handle deletes too. Just need to know if approve or reject is assumed changeQuery.ChangeTokenStart = lastChangeToken; List changedList = cc.Web.GetListById(new Guid(listID)); var changes = changedList.GetChanges(changeQuery); cc.Load(changes); cc.ExecuteQuery(); foreach (Change change in changes) { if (change is ChangeItem) { ListItem task = changedList.GetItemById((change as ChangeItem).ItemId); ListItemVersionCollection taskVersions = task.Versions; cc.Load(taskVersions); cc.Load(task); cc.ExecuteQuery(); if (taskVersions.Count < 2) { return; } var currentStatus = (string)taskVersions[0]["Status"]; var priorStatus = (string)taskVersions[1]["Status"]; string wfid = (string)task["workflowId"]; Console.WriteLine($"Item # ${task.Id} current status is ${currentStatus} Prior status is ${priorStatus}"); switch ((string)task["Action"]) { case "DocOwnerApproval": if (currentStatus != priorStatus) { if (currentStatus == "Approve") { log.LogInformation("Sending event DocOwnerApproved"); client.RaiseEventAsync(wfid, "DocOwnerApproved"); } else { log.LogInformation("Sending event DocOwnerRejected"); client.RaiseEventAsync(wfid, "DocOwnerRejected"); } } break; case "StakeHolderApproval": if (currentStatus != priorStatus) { if (currentStatus == "Approve") { var eventName = "StakeHolderApproval:" + ((FieldUserValue)task["AssignedTo"]).LookupId; log.LogInformation($"Sending event '${eventName}'"); client.RaiseEventAsync(wfid, eventName, true); } else { log.LogInformation($"Sending event 'StakeHolderRejection'"); client.RaiseEventAsync(wfid, "StakeHolderRejection"); } } break; } } } }; }