public void UploadSingleDocument_Json()
 {
     const int caseId = 10000;
     ArchiveCase archiveCaseController = new ArchiveCase();
     ArchiveCaseFileModel fileDataObj = new ArchiveCaseFileModel
     {
         FileName = "fiddler4setup.json",
         FileData = "{ 'employees':[" +
                    "{ 'firstName':'Peter', 'lastName':'Jones'}" +
                    "]}"
     };
     string extension = Path.GetExtension(fileDataObj.FileName);
     var result = JObject.Parse(archiveCaseController.UploadDocumentInCaseFolder(caseId, fileDataObj, _objArchiveModel));
     Assert.AreEqual("Invalid file Extension " + extension, result["response"]);
 }
 public void UploadSingleDocument_JavaScript()
 {
     const int caseId = 10000;
     ArchiveCase archiveCaseController = new ArchiveCase();
     ArchiveCaseFileModel fileDataObj = new ArchiveCaseFileModel
     {
         FileName = "script.js",
         FileData = "<script>" +
                    "alert('Javascript File');" +
                    "</script>"
     };
     string extension = Path.GetExtension(fileDataObj.FileName);
     var result = JObject.Parse(archiveCaseController.UploadDocumentInCaseFolder(caseId, fileDataObj, _objArchiveModel));
     Assert.AreEqual("Invalid file Extension " + extension, result["response"]);
 }
 public void UploadSingleDocument_Exe()
 {
     const int caseId = 10000;
     ArchiveCase archiveCaseController = new ArchiveCase();
     ArchiveCaseFileModel fileDataObj = new ArchiveCaseFileModel
     {
         FileName = "fiddler4setup.exe",
         FileData = "Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs"
     };
     string extension = Path.GetExtension(fileDataObj.FileName);
     var result = JObject.Parse(archiveCaseController.UploadDocumentInCaseFolder(caseId, fileDataObj, _objArchiveModel));
     Assert.AreEqual("Invalid file Extension " + extension, result["response"]);
 }
 public void UploadSingleDocument_Image()
 {
     const int caseId = 10000;
     ArchiveCase archiveCaseController = new ArchiveCase();
     ArchiveCaseFileModel fileDataObj = new ArchiveCaseFileModel
     {
         FileName = "Desert.jpg",
         FileData = "Excel text Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs"
     };
     archiveCaseController.UploadDocumentInCaseFolder(caseId, fileDataObj, _objArchiveModel);
 }
 public void UploadSingleDocument_PPT()
 {
     const int caseId = 10000;
     ArchiveCase archiveCaseController = new ArchiveCase();
     ArchiveCaseFileModel fileDataObj = new ArchiveCaseFileModel
     {
         FileName = "Powerpoint.pptx",//6.xlsx
         FileData = "Powerpoint text Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs"
     };
     archiveCaseController.UploadDocumentInCaseFolder(caseId, fileDataObj, _objArchiveModel);
 }
 public void UploadSingleDocument_Notepad()
 {
     const int caseId = 10000;
     ArchiveCase archiveCaseController = new ArchiveCase();
     ArchiveCaseFileModel fileDataObj = new ArchiveCaseFileModel
     {
         FileName = "Notepad.txt",
         FileData = "Notepad text Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs Sample Docs"
     };
     archiveCaseController.UploadDocumentInCaseFolder(caseId, fileDataObj, _objArchiveModel);
 }
        public string UploadDocumentInCaseFolder(int caseId, ArchiveCaseFileModel fileDataObj, ArchiveCredentialModel archiveModel)
        {
            string extension = Path.GetExtension(fileDataObj.FileName);
            if (!string.IsNullOrEmpty(extension))
            {
                Match match = Regex.Match(extension, @".*\.(xls|xlsx|jpg|jpeg|gif|png|doc|docx|pdf|pptx|ppt|txt)?$", RegexOptions.IgnoreCase);

                if (match.Success)
                {
                    Uri uri = new Uri(archiveModel.BaseSiteUrl);
                    string relativeUrl;
                    if (!archiveModel.BaseSiteUrl.Contains(KeywordApi))
                    {
                        relativeUrl = uri.AbsolutePath;
                        if (archiveModel.BaseSiteUrl.EndsWith("/", StringComparison.Ordinal))
                            archiveModel.BaseSiteUrl += KeywordApi;
                        else
                            archiveModel.BaseSiteUrl += "/" + KeywordApi;
                    }
                    else
                    {
                        Uri uri1 = new Uri(uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length));
                        relativeUrl = uri1.AbsolutePath;
                    }
                    string formDigestValue, libraryName = string.Empty, caseFolderName = string.Empty, documentStatusValue = string.Empty;
                    var restClient = RestClientObj(archiveModel.BaseSiteUrl, archiveModel.Username, archiveModel.Password, out formDigestValue);

                    int localeId = GetLocaleId(restClient);

                    if (Equals(localeId, NorwegianNoLocaleId))
                    {
                        libraryName = DocumentLibraryNo;
                        caseFolderName = CaseFolderNameNo;
                        documentStatusValue = DocumentStatusDefaultValNo;
                    }
                    else if (Equals(localeId, EnglishUsLocaleId))
                    {
                        libraryName = DocumentLibraryEn;
                        caseFolderName = CaseFolderNameEn;
                        documentStatusValue = DocumentStatusDefaultValEn;
                    }

                    string folderServerRelativeUrl = string.Concat(relativeUrl, libraryName, "/", caseFolderName, "/", caseId, "/Saksdokumenter");

                    //RestRequest request = null;
                    RestRequest request = new RestRequest("web/GetFolderByServerRelativeUrl('" + folderServerRelativeUrl + "')/files/add(url='" + fileDataObj.FileName + "',overwrite='true')", Method.POST)
                    {
                        RequestFormat = DataFormat.Json
                    };
                    request.AddHeader(Accept, AcceptHeaderVal);
                    request.AddHeader(ContentType, AcceptHeaderVal);
                    request.AddHeader(RequestDigest, formDigestValue);
                    request.AddHeader(BinaryStringRequestBody, "true");
                    request.AddParameter(AcceptHeaderVal, fileDataObj.FileData, ParameterType.RequestBody);
                    //IRestResponse response = restClient.Execute(request);
                    restClient.Execute(request);

                    //JObject jobj = JObject.Parse(response.Content);

                    string serverRelativeUrlOfFile = string.Concat(folderServerRelativeUrl, "/", fileDataObj.FileName);
                    ItemDetailModel listItemDetailObj = GetItemDetailsByServerRelativeUrl(serverRelativeUrlOfFile, restClient);

                    var body = string.Concat(
                                "{",
                                "'__metadata':{'type':'" + listItemDetailObj.ListItemEntityTypeFullName + "'},",
                                "'DocumentStatus':'" + documentStatusValue + "'",
                                "}");

                    request = new RestRequest("web/lists/getbytitle('" + libraryName + "')/items(" + listItemDetailObj.Id + ")", Method.POST)
                    {
                        RequestFormat = DataFormat.Json
                    };
                    request.AddHeader(Accept, AcceptHeaderVal);
                    request.AddHeader(ContentType, AcceptHeaderVal);
                    request.AddHeader(HttpMethod, Method.MERGE.ToString());
                    request.AddHeader(IfMatch, listItemDetailObj.ETag);
                    request.AddHeader(RequestDigest, formDigestValue);
                    request.AddParameter(AcceptHeaderVal, body, ParameterType.RequestBody);
                    //IRestResponse updateResponse = restClient.Execute(request);
                    restClient.Execute(request);

                    string file = Path.GetFileNameWithoutExtension(serverRelativeUrlOfFile),
                           documentName = listItemDetailObj.DocumentName,
                           newPath = serverRelativeUrlOfFile.Replace(file, documentName);

                    return "{\"response\":\"Case Document uploaded successfully at: " + newPath + " and to update metadata call /Documents/{documentId} of type PUT\",\"CaseDocumentName\":\"" + documentName + "\"}";
                }
            }
            return "{\"response\":\"Invalid file Extension " + extension + "\"}";
        }