/// <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 IHttpActionResult SearchDocument(Guid documentId, string version = null) { Guid messageId; Dictionary <string, dynamic> keyValuePairs; //Retrieve the header parameters from request object RetrieveHeaders(out keyValuePairs, out messageId); SearchDocumentRequest searchDocumentRequest = new SearchDocumentRequest(); SearchDocumentResponse searchDocumentResponse = new SearchDocumentResponse(); try { Log.DebugFormat("In api/SearchDocument method for MessageId - {0}", messageId.ToString()); searchDocumentRequest.DocumentId = documentId; searchDocumentRequest.Version = version; if (searchDocumentRequest.DocumentId != Guid.Empty) { //If documentid is not empty and version number is not passed retrieve the document metadata if ((string.IsNullOrEmpty(searchDocumentRequest.Version) || string.IsNullOrWhiteSpace(searchDocumentRequest.Version))) { //Create a new SearchDocumentAllMetadataVersions model object SearchDocumentAllMetaDataVersions searchDocumentAllMetaDataVersions = new SearchDocumentAllMetaDataVersions(); try { Log.DebugFormat("In api/SearchDocument, before calling DDMSSearchAllOldVersions method for MessageId - {0} Document ID :{1}", messageId.ToString(), searchDocumentRequest.DocumentId.ToString()); //To fetch document metadata for all versions searchDocumentAllMetaDataVersions = ddmsSearchDocument.DDMSSearchAllOldVersions(searchDocumentRequest, messageId.ToString()); Log.DebugFormat("In api/SearchDocument, after calling DDMSSearchAllOldVersions method for MessageId - {0} Document ID :{1}", messageId.ToString(), searchDocumentRequest.DocumentId.ToString()); if (string.IsNullOrEmpty(searchDocumentAllMetaDataVersions.ErrorMessage)) { //Add headers to response message keyValuePairs.Add(HeaderConstants.ErrorCode, (int)HttpStatusCode.OK); keyValuePairs.Add(HeaderConstants.Status, HeaderValueConstants.Success); foreach (var field in keyValuePairs) { if (!string.IsNullOrEmpty(field.Value.ToString()) && !string.IsNullOrWhiteSpace(field.Value.ToString())) { HttpContext.Current.Response.Headers.Add(field.Key, field.Value.ToString()); } } return(Content(HttpStatusCode.OK, searchDocumentAllMetaDataVersions)); } //Search based on documentid is not successful - file not found if (searchDocumentAllMetaDataVersions.ErrorMessage == ErrorMessage.FileNotFound) { AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.NotFound, ErrorMessage.FileNotFound); return(Content(HttpStatusCode.NotFound, searchDocumentAllMetaDataVersions)); } else { //Any other failures/exceptions in search functionality AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.InternalServerError, searchDocumentAllMetaDataVersions.ErrorMessage); return(Content(HttpStatusCode.InternalServerError, searchDocumentAllMetaDataVersions)); } } catch (Exception e) { Log.ErrorFormat("Error in api/SearchDocument DDMSSearch method for MessageId - {0} :{ 1}", messageId.ToString(), e.Message); searchDocumentAllMetaDataVersions.ErrorMessage = e.Message; AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.InternalServerError, searchDocumentAllMetaDataVersions.ErrorMessage); return(Content(HttpStatusCode.InternalServerError, searchDocumentAllMetaDataVersions)); } } try { Log.DebugFormat("In api/SearchDocument, before calling DDMSSearch method for MessageId - {0} - Document ID :{1} Version :{2}", messageId.ToString(), searchDocumentRequest.DocumentId.ToString(), searchDocumentRequest.Version); //Fetch the document based on documentid and version number searchDocumentResponse = ddmsSearchDocument.DDMSSearch(searchDocumentRequest, messageId.ToString()); Log.DebugFormat("In api/SearchDocument, after calling DDMSSearch method for MessageId - {0} - Document ID :{1} Version :{2}", messageId.ToString(), searchDocumentRequest.DocumentId.ToString(), searchDocumentRequest.Version); if (string.IsNullOrEmpty(searchDocumentResponse.ErrorMessage)) { keyValuePairs.Add(HeaderConstants.ErrorCode, (int)HttpStatusCode.OK); keyValuePairs.Add(HeaderConstants.Status, HeaderValueConstants.Success); foreach (var field in keyValuePairs) { if (!string.IsNullOrEmpty(field.Value.ToString()) && !string.IsNullOrWhiteSpace(field.Value.ToString())) { HttpContext.Current.Response.Headers.Add(field.Key, field.Value.ToString()); } } return(Content(HttpStatusCode.OK, searchDocumentResponse)); } //Add error response file not found if (searchDocumentResponse.ErrorMessage == ErrorMessage.FileNotFound) { AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.NotFound, ErrorMessage.FileNotFound); return(Content(HttpStatusCode.NotFound, searchDocumentResponse)); } else { AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.InternalServerError, searchDocumentResponse.ErrorMessage); return(Content(HttpStatusCode.InternalServerError, searchDocumentResponse)); } } catch (Exception ex) { Log.ErrorFormat("Error in api/SearchDocument DDMSSearch method for MessageId - {0} :{1}", messageId.ToString(), ex.Message); searchDocumentResponse.ErrorMessage = ex.Message; AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.InternalServerError, searchDocumentResponse.ErrorMessage); return(Content(HttpStatusCode.InternalServerError, searchDocumentResponse)); } } else { Log.DebugFormat("In api/SearchDocument, DocumentId is empty for MessageId - {0} - Document ID :{1}", messageId.ToString(), searchDocumentRequest.DocumentId.ToString()); AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.InternalServerError, string.Format(ErrorMessage.ValueEmpty, "documentId")); return(Content(HttpStatusCode.BadRequest, string.Format(ErrorMessage.ValueEmpty, "documentId"))); } } catch (Exception ex) { Log.ErrorFormat("Error in api/SearchDocument for MessageId - {0} :{1}", messageId.ToString(), ex.Message); AddErrorResponseHeaders(keyValuePairs, (int)HttpStatusCode.InternalServerError, ex.Message); return(Content(HttpStatusCode.InternalServerError, ex.Message)); } }