Ejemplo n.º 1
0
        public async void UploadRemoveFilesTest()
        {
            Random rnd        = new Random(Guid.NewGuid().GetHashCode());
            string name       = "test-name" + rnd.Next() + ".txt";
            string testFolder = "test-folder" + rnd.Next();
            string listTitle  = "Shared Documents";
            string url        = serverAppIdUri + "/cannabisdev/Shared Documents/" + testFolder + "/" + name;

            string contentType = "text/plain";

            string testData = "This is just a test.";

            MemoryStream fileData = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(testData));

            await sharePointFileManager.CreateFolder(listTitle, testFolder);

            await sharePointFileManager.UploadFile(name, "Shared Documents", testFolder, fileData, contentType);

            // now delete it.

            await sharePointFileManager.DeleteFile("Shared Documents", testFolder, name);

            // cleanup the test folder.
            await sharePointFileManager.DeleteFolder("Shared Documents", testFolder);
        }
        public async Task <IActionResult> DeleteFile([FromQuery] string serverRelativeUrl, [FromQuery] string documentType, [FromRoute] string entityId, [FromRoute] string entityName)
        {
            // get the file.
            if (string.IsNullOrEmpty(entityId) || string.IsNullOrEmpty(entityName) || string.IsNullOrEmpty(serverRelativeUrl) || string.IsNullOrEmpty(documentType))
            {
                return(BadRequest());
            }

            ValidateSession();

            var hasAccess = await CanAccessEntityFile(entityName, entityId, documentType, serverRelativeUrl);

            if (!hasAccess)
            {
                return(new NotFoundResult());
            }

            // Update modifiedon to current time
            UpdateEntityModifiedOnDate(entityName, entityId);

            var result = await _sharePointFileManager.DeleteFile(serverRelativeUrl);

            if (result)
            {
                return(new OkResult());
            }

            return(new NotFoundResult());
        }
        public async Task <IActionResult> DeleteFile([FromQuery] string serverRelativeUrl, [FromQuery] string documentType, [FromRoute] string id, [FromRoute] string entityName)
        {
            if (!string.IsNullOrEmpty(id) && Guid.TryParse(id, out Guid entityId) && !string.IsNullOrEmpty(entityName) && !string.IsNullOrEmpty(documentType) && !string.IsNullOrEmpty(serverRelativeUrl))
            {
                ValidateSession();

                var hasAccess = await CanAccessEntityFile(entityName, entityId.ToString(), documentType, serverRelativeUrl);

                if (!hasAccess)
                {
                    return(new NotFoundResult());
                }

                // Update modifiedon to current time
                UpdateEntityModifiedOnDate(entityName, entityId.ToString());

                var result = await _sharePointFileManager.DeleteFile(serverRelativeUrl);

                if (result)
                {
                    return(new OkResult());
                }
                else
                {
                    return(new NotFoundResult());
                }
            }
            else
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 4
0
        public override Task <DeleteFileReply> DeleteFile(DeleteFileRequest request, ServerCallContext context)
        {
            var result = new DeleteFileReply();

            SharePointFileManager _sharePointFileManager = new SharePointFileManager(_configuration);

            try
            {
                bool success = _sharePointFileManager.DeleteFile(request.ServerRelativeUrl).GetAwaiter().GetResult();

                if (success)
                {
                    result.ResultStatus = ResultStatus.Success;
                }
                else
                {
                    result.ResultStatus = ResultStatus.Fail;
                }
            }
            catch (SharePointRestException ex)
            {
                result.ResultStatus = ResultStatus.Fail;
                result.ErrorDetail  = $"ERROR in deleting file {request.ServerRelativeUrl}";
                _logger.LogError(ex, result.ErrorDetail);
            }
            catch (Exception e)
            {
                result.ResultStatus = ResultStatus.Fail;
                result.ErrorDetail  = $"ERROR in deleting file {request.ServerRelativeUrl}";
                _logger.LogError(e, result.ErrorDetail);
            }

            return(Task.FromResult(result));
        }
Ejemplo n.º 5
0
        public void Execute(IConfiguration config, bool doRename)
        {
            // get a connection to Dynamics.
            IDynamicsClient dynamicsClient = DynamicsSetupUtil.SetupDynamics(config);

            // get the list of application files.
            SharePointFileManager sharePoint = new SharePointFileManager(config);

            string[] orderby = { "adoxio_licencenumber" };
            string[] expand  = { "adoxio_licences_SharePointDocumentLocations" };
            //var licences = dynamicsClient.Licenceses.Get(expand: expand).Value;


            var customHeaders = new Dictionary <string, List <string> >();
            var preferHeader  = new List <string>();

            preferHeader.Add($"odata.maxpagesize=5000");

            customHeaders.Add("Prefer", preferHeader);
            var odataVersionHeader = new List <string>();

            odataVersionHeader.Add("4.0");

            customHeaders.Add("OData-Version", odataVersionHeader);
            customHeaders.Add("OData-MaxVersion", odataVersionHeader);
            string odataNextLink = "1";
            bool   firstTime     = true;
            int    totalCount    = 5000;
            int    currentCount  = 0;
            int    renameCount   = 0;
            HttpOperationResponse <MicrosoftDynamicsCRMadoxioLicencesCollection> licencesQuery = new HttpOperationResponse <MicrosoftDynamicsCRMadoxioLicencesCollection>();

            while (odataNextLink != null)
            {
                if (firstTime)
                {
                    firstTime     = false;
                    licencesQuery = dynamicsClient.Licenceses.GetWithHttpMessagesAsync(expand: expand, customHeaders: customHeaders, count: true, orderby: orderby).GetAwaiter().GetResult();
                }
                else
                {
                    odataNextLink = licencesQuery.Body.OdataNextLink;
                    if (odataNextLink != null)
                    {
                        licencesQuery = dynamicsClient.Licenceses.GetNextLink(odataNextLink, customHeaders);

                        totalCount += licencesQuery.Body.Value.Count;
                    }
                    else
                    {
                        licencesQuery = new HttpOperationResponse <MicrosoftDynamicsCRMadoxioLicencesCollection>();
                    }
                }
                Console.Out.WriteLine($"Currently on licence {currentCount} of {totalCount}");
                if (licencesQuery?.Body?.Value != null)
                {
                    var licences = licencesQuery.Body.Value;


                    foreach (var licence in licences)
                    {
                        bool isInRange     = false;
                        int  licenceNumber = -1;
                        int.TryParse(licence.AdoxioLicencenumber, out licenceNumber);

                        if (licenceNumber >= 1114 && licenceNumber <= 18573)
                        {
                            isInRange = true;
                            //Console.Out.WriteLine($"Licence #{licenceNumber}");
                        }
                        currentCount++;
                        string folderName = licence.GetDocumentFolderName();
                        if (licence.AdoxioLicencesSharePointDocumentLocations != null &&
                            licence.AdoxioLicencesSharePointDocumentLocations.Count > 0 &&
                            licence.AdoxioLicencesSharePointDocumentLocations[0].Relativeurl != null)
                        {
                            folderName = licence.AdoxioLicencesSharePointDocumentLocations[0].Relativeurl;
                        }

                        List <FileDetailsList> fileList = null;
                        try
                        {
                            fileList = sharePoint.GetFileDetailsListInFolder(SharePointFileManager.LicenceDocumentUrlTitle,
                                                                             folderName, null)
                                       .GetAwaiter().GetResult();
                        }
                        catch (Exception e)
                        {
                            // Console.WriteLine($"Folder not found [{folderName}]");
                        }

                        if (fileList != null && fileList.Count > 0)
                        {
                            //Console.WriteLine($"Found {fileList.Count} Files.");
                            foreach (var file in fileList)
                            {
                                if (isInRange)
                                {
                                    // Console.Out.WriteLine($"Current filename: {file.Name}");
                                }

                                if (IsSuspect(file.Name))
                                {
                                    string newName = FixName(file.Name);
                                    if (newName != null)
                                    {
                                        Console.Out.WriteLine($"Filename {file.Name} is suspect.");
                                        Console.Out.WriteLine($"New name is {newName}");

                                        string oldFileName =
                                            $"/{SharePointFileManager.LicenceDocumentUrlTitle}/{folderName}/{file.Name}";
                                        string newFileName = $"{SharePointFileManager.LicenceDocumentUrlTitle}/{folderName}/{newName}";
                                        Console.Out.WriteLine($"Rename File {oldFileName} to {newFileName}");
                                        byte[] data = sharePoint.DownloadFile(oldFileName).GetAwaiter().GetResult();
                                        if (data != null)
                                        {
                                            var success = sharePoint.UploadFile(newName,
                                                                                SharePointFileManager.LicenceDocumentUrlTitle, folderName, data, "application/pdf").GetAwaiter().GetResult();
                                            if (success != null)
                                            {
                                                // cleanup the old file.
                                                sharePoint.DeleteFile(oldFileName).GetAwaiter().GetResult();
                                                Console.Out.WriteLine($"Rename File Complete");
                                            }
                                        }

                                        renameCount++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.Out.WriteLine($"Licence count is {totalCount}");
            Console.Out.WriteLine($"Rename count is {renameCount}");
        }