/// <summary> /// Uploads the document to matter library. /// </summary> /// <param name="requestObject">Request Object containing SharePoint App Token</param> /// <param name="sourceUrl">URL of the source document</param> /// <param name="documentStream">Content stream of the document</param> /// <param name="versionInfo">The version information.</param> /// <param name="comments">The comments.</param> /// <param name="retainCheckOut">retain check out option</param> /// <returns>Content Type List for the Request Object containing SharePoint App Token</returns> internal static string UploadtoMatter(RequestObject requestObject, string sourceUrl, Stream documentStream, int versionInfo, string comments, bool retainCheckOut) { string status = ConstantStrings.FALSE; string result = ConstantStrings.FALSE; try { if (null != requestObject) { ClientContext clientContext; if (!string.IsNullOrWhiteSpace(sourceUrl) && null != documentStream) { string[] sourceUrlParts = sourceUrl.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture)); if (2 == sourceUrlParts.Length) { using (clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(sourceUrlParts[0]), requestObject.RefreshToken)) { Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl(sourceUrlParts[1]); string documentLibraryName = BriefcaseHelperFunction.getLibraryName(clientContext, file); string contentType = string.Empty; contentType = BriefcaseContentTypeHelperFunctions.ContentTypeByName(requestObject, clientContext, file.Name, sourceUrl, 1, contentType, documentLibraryName); string listContentType = BriefcaseContentTypeHelperFunctions.GetContentTypeList(requestObject, clientContext, sourceUrl, contentType, 1, documentLibraryName); status = BriefcaseContentTypeHelperFunctions.GetContentTypeList(requestObject, clientContext, sourceUrl, contentType, 2, documentLibraryName); FileSaveBinaryInformation fileSaveBinaryInformation = new FileSaveBinaryInformation(); fileSaveBinaryInformation.ContentStream = documentStream; file.SaveBinary(fileSaveBinaryInformation); // Check if file is already checked out if (file.CheckOutType == CheckOutType.None) { file.CheckOut(); } // Check the type of Check in to be performed switch (versionInfo) { case 0: file.CheckIn(comments, CheckinType.MinorCheckIn); break; case 1: file.CheckIn(comments, CheckinType.MajorCheckIn); break; case 2: file.CheckIn(comments, CheckinType.OverwriteCheckIn); break; } // Load the Stream data for the file clientContext.ExecuteQuery(); status = BriefcaseContentTypeHelperFunctions.GetContentTypeList(requestObject, clientContext, sourceUrl, listContentType, 2, documentLibraryName); // Check whether we need to retain checkout if (retainCheckOut) { file.CheckOut(); clientContext.ExecuteQuery(); } status = string.Concat(ConstantStrings.TRUE, ConstantStrings.Comma, ConstantStrings.Space, file.ServerRelativeUrl); } } result = status; } else { status = string.Concat(ConstantStrings.FALSE, ConstantStrings.Comma, ConstantStrings.Space, TextConstants.MissingParametersMessage); result = status; } } } catch (Exception exception) { status = string.Concat(ConstantStrings.FALSE, ConstantStrings.Comma, ConstantStrings.Space, ServiceUtility.RemoveEscapeCharacter(exception.Message)); result = status; } return(result); }
/// <summary> /// Uploads the document to OneDrive (Users MySite document library). /// </summary> /// <param name="requestObject">Request Object containing SharePoint App Token</param> /// <param name="collectionOfAttachments">Collection of documents with data for each document</param> /// <param name="collectionOfOriginalAttachments">Collection of documents with path of source document</param> /// <param name="usersMySite">User's My site URL</param> /// <param name="allAttachmentUrl">Attachment URL.</param> /// <param name="isOverwrite">Overwrite check</param> /// <returns>JSON string specifying path of OneDrive as success and false as failure</returns> internal static string UploadtoBriefcase(RequestObject requestObject, Dictionary <string, Stream> collectionOfAttachments, Dictionary <string, string> collectionOfOriginalAttachments, string usersMySite, string[] allAttachmentUrl, int isOverwrite) { string status = ConstantStrings.FALSE; try { using (ClientContext clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(usersMySite), requestObject.RefreshToken)) { Microsoft.SharePoint.Client.Web web = clientContext.Web; string contentType = ServiceConstantStrings.OneDriveContentTypeName; // Get Content Type string contentTypegroup = ServiceConstantStrings.OneDriveContentTypeGroup; // Get Group of Content Type List <string> siteColumns = new List <string>(new string[] { ServiceConstantStrings.OneDriveSiteColumn }); bool isSiteColumnCreated = BriefcaseContentTypeHelperFunctions.CreateSiteColumn(clientContext, web, siteColumns); if (isSiteColumnCreated) { bool isContentTypeCreated = BriefcaseContentTypeHelperFunctions.IsContentTypePresentCheck(clientContext, web, contentType, contentTypegroup, siteColumns); if (isContentTypeCreated) { List list = web.Lists.GetByTitle(ServiceConstantStrings.OneDriveDocumentLibraryTitle); string briefcaseFolderQuery = string.Format(CultureInfo.InvariantCulture, ServiceConstantStrings.BriefcaseFolderQuery, ServiceConstantStrings.LegalBriefcaseFolder); ListItemCollection listItems = Lists.GetData(clientContext, ServiceConstantStrings.OneDriveDocumentLibraryTitle, briefcaseFolderQuery); BriefcaseContentTypeHelperFunctions.SetContentType(clientContext, list, usersMySite); string defaultContentTypeId = BriefcaseContentTypeHelperFunctions.SetOneDriveDefaultContentType(clientContext, list, contentType); if (ConstantStrings.FALSE != defaultContentTypeId) { web.Update(); if (0 == isOverwrite) { MailAttachmentDetails.CheckoutFailedPosition = 0; status = BriefcaseUtilityHelperFunctions.NewDocumentToOneDrive(clientContext, collectionOfAttachments, collectionOfOriginalAttachments, allAttachmentUrl, web, usersMySite, defaultContentTypeId); //// Undo check out in case of failure //// undo checkout all documents from position MailAttachmentDetails.checkOutFailedPosition BriefcaseHelperFunction.DiscardDocumentCheckout(requestObject, allAttachmentUrl); } else { MailAttachmentDetails.CheckoutFailedPosition = 0; status = BriefcaseUtilityHelperFunctions.OverWriteDocument(collectionOfAttachments, collectionOfOriginalAttachments, usersMySite, status, clientContext, web, listItems, defaultContentTypeId); string[] returnedStatus = status.Split(Convert.ToChar(ConstantStrings.DOLLAR, CultureInfo.InvariantCulture)); if (ConstantStrings.TRUE == returnedStatus[0]) { BriefcaseHelperFunction.DiscardDocumentCheckout(requestObject, allAttachmentUrl); } status = returnedStatus[1]; } } else { MatterCenterException customException = new MatterCenterException(TextConstants.ErrorCodeAssignDefaultContentType, TextConstants.ErrorMessageAssignDefaultContentType); throw customException; // Throw will direct to current function's catch block (if present). If not present then it will direct to parent catch block. Parent will be the calling function } } else { MatterCenterException customException = new MatterCenterException(TextConstants.ErrorCodeCreateSiteContentType, TextConstants.ErrorMessageCreateSiteContentType); throw customException; // Throw will direct to current function's catch block (if present). If not present then it will direct to parent catch block. Parent will be the calling function } } else { MatterCenterException customException = new MatterCenterException(TextConstants.ErrorCodeCreateSiteColumn, TextConstants.ErrorMessageCreateSiteColumn); throw customException; // Throw will direct to current function's catch block (if present). If not present then it will direct to parent catch block. Parent will be the calling function } } } catch (Exception exception) { Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName); status += ConstantStrings.Semicolon + usersMySite; } return(status); }