/// <summary>
 /// Detaches the operation.
 /// </summary>
 /// <param name="commonResponse">Common response object</param>
 /// <param name="clientContext">Client context object</param>
 /// <param name="list">List object</param>
 /// <param name="listItems">List item collection object</param>
 /// <returns>Returns Common response object</returns>
 internal static CommonResponse DetachOperation(CommonResponse commonResponse, ClientContext clientContext, List list, ListItemCollection listItems)
 {
     CommonResponse detachOperationResponse = new CommonResponse();
     try
     {
         ContentType targetDocumentSetContentType = BriefcaseContentTypeHelperFunctions.GetContentType(list, clientContext);
         if (null != targetDocumentSetContentType)
         {
             foreach (var listItem in listItems)
             {
                 listItem[ServiceConstantStrings.OneDriveSiteColumn] = string.Empty;
                 listItem[ConstantStrings.OneDriveContentTypeProperty] = Convert.ToString(targetDocumentSetContentType.Id, CultureInfo.InvariantCulture);
                 listItem.Update();
                 clientContext.ExecuteQuery();
                 commonResponse.Status.Add(true);
             }
             detachOperationResponse = commonResponse;
         }
     }
     catch (Exception exception)
     {
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
     }
     return detachOperationResponse;
 }
 /// <summary>
 /// Updates the operation status.
 /// </summary>
 /// <param name="commonResponse">Common response object</param>
 /// <param name="status">Status of the operation</param>
 /// <returns>Returns Common response object</returns>
 internal static CommonResponse UpdateOperationStatus(CommonResponse commonResponse, string status)
 {
     CommonResponse result = null;
     try
     {
         Dictionary<string, object> responseJson = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(status);
         int code = Convert.ToInt32(responseJson[ConstantStrings.ResponseCode], CultureInfo.InvariantCulture);
         if (code == 0)
         {
             commonResponse.Status.Add(true);
         }
         else
         {
             commonResponse.Status.Add(false);
             commonResponse.ErrorMessage += Convert.ToString(responseJson[ConstantStrings.ResponseValue], CultureInfo.InvariantCulture);
         }
         result = commonResponse;
     }
     catch (Exception exception)
     {
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
         CommonResponse UpdateOperationStatusError = new CommonResponse();
         result = UpdateOperationStatusError;
     }
     return result;
 }
 /// <summary>
 /// Returns the checkout operation status.
 /// </summary>
 /// <param name="commonResponse">Common response object</param>
 /// <param name="status">Status of the operation</param>
 /// <returns>Returns Common response object</returns>
 internal static CommonResponse CheckoutOperationStatus(CommonResponse commonResponse, string status)
 {
     CommonResponse result = null;
     try
     {
         if (ConstantStrings.TRUE == status.Split(ConstantStrings.Comma[0])[0])
         {
             commonResponse.FileNames.Add(status.Split(ConstantStrings.Comma[0])[1]);
         }
         else
         {
             if (ServiceConstantStrings.AlreadyCheckOut == status.Split(ConstantStrings.Comma[0])[1])
             {
                 commonResponse.FileNames.Add(status.Split(ConstantStrings.Comma[0])[1]);
             }
             else
             {
                 commonResponse.ErrorMessage += status.Split(ConstantStrings.Comma[0])[1];
             }
         }
         result = commonResponse;
     }
     catch (Exception exception)
     {
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
         CommonResponse CheckoutOperationStatusError = new CommonResponse();
         result = CheckoutOperationStatusError;
     }
     return result;
 }
        /// <summary>
        /// Helper function to perform Check Out, Detach, and Update document functionality.
        /// </summary>
        /// <param name="requestObject">The Web request object.</param>
        /// <param name="syncDetails">SyncDetails object</param>
        /// <param name="commonResponse">Holds response for briefcase detach operation</param>
        /// <param name="clientContext">Client context object for connection between SP & client</param>
        /// <param name="status">Sets status for send to briefcase operation</param>
        private void GetContextInList(RequestObject requestObject, SyncDetails syncDetails, ref CommonResponse commonResponse, ClientContext clientContext, ref string status)
        {
            List list = clientContext.Web.Lists.GetById(new Guid(syncDetails.ListId));
            foreach (int itemid in syncDetails.ItemId)
            {
                ListItemCollection listItems = BriefcaseHelperFunction.GetListItemCollection(clientContext, itemid);
                MailAttachmentDetails mailattachmentDetails = new MailAttachmentDetails();
                ServiceConstantStrings.OperationTypes operationType = (ServiceConstantStrings.OperationTypes)syncDetails.Operation;
                if (operationType == ServiceConstantStrings.OperationTypes.Detach)
                {
                    commonResponse = BriefcaseHelperFunction.DetachOperation(commonResponse, clientContext, list, listItems);
                }
                else
                {
                    foreach (var listItem in listItems)
                    {
                        mailattachmentDetails.FullUrl = Convert.ToString(listItem[ServiceConstantStrings.OneDriveSiteColumn], CultureInfo.InvariantCulture);
                        mailattachmentDetails.IsOverwrite = 1;
                    }

                    switch (operationType)
                    {
                        case ServiceConstantStrings.OperationTypes.Update:
                            status = this.SendToBriefcase(requestObject, mailattachmentDetails, false);
                            commonResponse = BriefcaseHelperFunction.UpdateOperationStatus(commonResponse, status);
                            break;
                        case ServiceConstantStrings.OperationTypes.Checkout:
                            status = this.CheckOutDocument(requestObject, mailattachmentDetails);
                            commonResponse = BriefcaseHelperFunction.CheckoutOperationStatus(commonResponse, status);
                            commonResponse.Status.Add(Convert.ToBoolean(status.Split(',')[0], CultureInfo.InvariantCulture));
                            break;
                    }
                }
            }
        }
        public string SyncBriefcase(RequestObject requestObject, SyncDetails syncDetails)
        {
            CommonResponse commonResponse = new CommonResponse();
            try
            {
                if (null != requestObject && null != syncDetails && ValidationHelperFunctions.CheckRequestValidatorToken())
                {
                    string usersMySite = string.Empty;
                    ClientContext clientContext = null;
                    usersMySite = BriefcaseHelperFunction.GetPersonalURL(requestObject);
                    if (!string.IsNullOrEmpty(usersMySite))
                    {
                        using (clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(usersMySite), requestObject.RefreshToken))
                        {
                            string status = string.Empty;
                            List<string> sourceURL = BriefcaseHelperFunction.CheckSourceURL(clientContext, syncDetails, 1);
                            if (sourceURL.Count != 0)
                            {
                                return BriefcaseHelperFunction.GetNotSupportedMessage(sourceURL);
                            }

                            GetContextInList(requestObject, syncDetails, ref commonResponse, clientContext, ref status);
                        }
                    }
                    else
                    {
                        commonResponse.ErrorMessage = ServiceConstantStrings.UserMySiteNotPresent;
                    }
                }
                else
                {
                    commonResponse.Status.Add(false);
                    commonResponse.ErrorMessage = TextConstants.InvalidParametersMessage;
                }
            }
            catch (Exception exception)
            {
                commonResponse.Status.Add(false);
                commonResponse.ErrorMessage = ServiceUtility.RemoveEscapeCharacter(exception.Message);
            }

            return BriefcaseHelperFunction.GetSerializeResponse(commonResponse);
        }
 public string SendToMatter(RequestObject requestObject, BriefcaseDetails[] briefcaseDetails, int versionInfo, string comments, bool retainCheckout, bool convenienceCopy)
 {
     CommonResponse checkInResponse = new CommonResponse();
     string status = string.Empty;
     try
     {
         if (null != requestObject && null != briefcaseDetails && ValidationHelperFunctions.CheckRequestValidatorToken())
         {
             ClientContext clientContext = null;
             string usersMySite = BriefcaseHelperFunction.GetPersonalURL(requestObject);
             if (!string.IsNullOrEmpty(usersMySite))
             {
                 for (int iterator = 0; iterator < briefcaseDetails.Length; iterator++)
                 {
                     if (!string.IsNullOrWhiteSpace(briefcaseDetails[iterator].DocumentUrl) && !string.IsNullOrWhiteSpace(briefcaseDetails[iterator].DocumentId))
                     {
                         using (clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(usersMySite), requestObject.RefreshToken))
                         {
                             ClientResult<System.IO.Stream> data = Lists.GetStreamFromFile(clientContext, briefcaseDetails[iterator].DocumentUrl);
                             string briefcaseItemQuery = string.Format(CultureInfo.InvariantCulture, ServiceConstantStrings.SendToMatterQuery, ServiceConstantStrings.OneDriveSiteColumn, briefcaseDetails[iterator].DocumentId);
                             ListItemCollection listItems = Lists.GetData(clientContext, ServiceConstantStrings.OneDriveDocumentLibraryTitle, briefcaseItemQuery);
                             string sourceUrl = string.Empty;
                             foreach (var listItem in listItems)
                             {
                                 sourceUrl = Convert.ToString(listItem[ServiceConstantStrings.OneDriveSiteColumn], CultureInfo.InvariantCulture);
                             }
                             if (!string.IsNullOrWhiteSpace(sourceUrl))
                             {
                                 status = BriefcaseHelperFunction.UploadtoMatter(requestObject, sourceUrl, data.Value, versionInfo, comments, retainCheckout);
                                 checkInResponse.Status.Add(Convert.ToBoolean(status.Split(ConstantStrings.Comma[0])[0], CultureInfo.InvariantCulture));
                                 if (checkInResponse.Status[iterator])
                                 {
                                     checkInResponse.FileNames.Add(status.Split(ConstantStrings.Comma[0])[1]);
                                     if (!retainCheckout)
                                     {
                                         if (!convenienceCopy)
                                         {
                                             foreach (var listitem in listItems)
                                             {
                                                 listitem.DeleteObject();
                                                 clientContext.ExecuteQuery();
                                             }
                                         }
                                     }
                                 }
                                 else
                                 {
                                     checkInResponse.ErrorMessage += ConstantStrings.Space + status.Split(ConstantStrings.Comma[0])[1];
                                 }
                             }
                         }
                     }
                 }
             }
             else
             {
                 checkInResponse.ErrorMessage = ServiceConstantStrings.UserMySiteNotPresent;
             }
         }
         else
         {
             checkInResponse.Status.Add(false);
             checkInResponse.ErrorMessage += ConstantStrings.Space + TextConstants.MissingParametersMessage;
         }
     }
     catch (Exception exception)
     {
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
         checkInResponse.Status.Add(false);
         checkInResponse.ErrorMessage += ConstantStrings.Space + ServiceUtility.RemoveEscapeCharacter(exception.Message);
     }
     return BriefcaseHelperFunction.GetSerializeResponse(checkInResponse);
 }