예제 #1
0
        public async Task <DocumentUpdateResult> UpdateBookmrkValue(string documentId, IEnumerable <DestinationPoint> destinationPoints, string value)
        {
            DocumentUpdateResult retValue = new DocumentUpdateResult()
            {
                IsSuccess = true
            };

            try
            {
                string authHeader = GetAuthorizationHeader();
                DocumentCheckResult documentResult = await GetDocumentUrlByID(new DocumentCheckResult()
                {
                    DocumentId = documentId
                });

                if (documentResult.IsSuccess)
                {
                    FileContextInfo fileContextInfo = await GetFileContextInfo(authHeader, documentResult.DocumentUrl);

                    var stream = await GetFileStream(authHeader, documentResult.DocumentUrl, fileContextInfo);

                    stream.Seek(0, SeekOrigin.Begin);
                    UpdateStream(destinationPoints, value, stream, retValue);
                    await UploadStream(authHeader, documentResult.DocumentUrl, stream, fileContextInfo);

                    return(retValue);
                }
                else
                {
                    retValue.IsSuccess = false;
                    retValue.Message.Add(documentResult.Message);
                }
            }
            catch (Exception ex)
            {
                retValue.IsSuccess = false;
                retValue.Message.Add(ex.ToString());
            }
            return(retValue);
        }
예제 #2
0
        static private async Task <Stream> GetFileStream(string authHeader, string destinationFileName, FileContextInfo fileContextInfo)
        {
            var filePath = new Uri(destinationFileName).AbsolutePath;
            var stream   = new System.IO.MemoryStream();

            try
            {
                using (var client = new WebClient())
                {
                    client.Headers.Add(HttpRequestHeader.Authorization, authHeader);
                    var fileUri      = new Uri(destinationFileName);
                    var downloadData = await client.DownloadDataTaskAsync($"{fileContextInfo.WebUrl}/_api/web/getfilebyserverrelativeurl('{filePath}')/$value");

                    await stream.WriteAsync(downloadData, 0, downloadData.Length);

                    return(stream);
                }
            }
            catch (Exception ex)
            {
                stream.Dispose();
                throw ex;
            }
        }
예제 #3
0
        static private async Task UploadStream(string authHeader, string destinationFileName, Stream stream, FileContextInfo contextInfo)
        {
            var fileAbsolutePath = new Uri(destinationFileName).AbsolutePath;
            int index            = fileAbsolutePath.LastIndexOf('/');
            var filePath         = fileAbsolutePath.Substring(0, index);
            var fileName         = fileAbsolutePath.Substring(index + 1);

            using (var client = new WebClient())
            {
                client.Headers.Add(HttpRequestHeader.Authorization, authHeader);
                client.Headers.Add(HttpRequestHeader.Accept, "application/json");

                var uploadUri   = new Uri($"{contextInfo.WebUrl}/_api/web/GetFolderByServerRelativeUrl(@filePath)/Files/add(url='{fileName}',overwrite=true)?@filePath='{filePath}'");
                var returnValue = await client.UploadDataTaskAsync(uploadUri, "POST", (stream as MemoryStream).ToArray());
            }
        }