Esempio n. 1
0
        private void UploadAndPublishSingleFile(ClientContext context, Web web, string configRootFolder, ShContentFolder contentFolder, string uploadTargetFolder, Folder rootFolder, List <ShFileProperties> filePropertiesCollection, string filePath)
        {
            var pathToFileFromRootFolder = filePath.Replace(configRootFolder.TrimEnd(new[] { '\\' }) + "\\", "");
            var fileName = Path.GetFileName(pathToFileFromRootFolder);

            pathToFileFromRootFolder = pathToFileFromRootFolder.Replace("\\", "/");

            if (!string.IsNullOrEmpty(contentFolder.PropertiesFile) && contentFolder.PropertiesFile == fileName)
            {
                Log.DebugFormat("Skipping file upload of {0} since it's used as a configuration file", fileName);
                return;
            }

            ShFileProperties fileProperties = null;

            if (filePropertiesCollection != null)
            {
                fileProperties = filePropertiesCollection.SingleOrDefault(f => f.Path == pathToFileFromRootFolder);
            }

            Log.DebugFormat("Uploading file {0} to {1}", fileName, contentFolder.ListUrl);
            var fileUrl = GetFileUrl(uploadTargetFolder, pathToFileFromRootFolder, fileProperties);

            web.CheckOutFile(fileUrl);

            if (fileProperties == null || fileProperties.ReplaceContent)
            {
                var newFile = GetFileCreationInformation(context, fileUrl, filePath, pathToFileFromRootFolder, fileProperties);

                File uploadFile = rootFolder.Files.Add(newFile);

                context.Load(uploadFile);
                context.ExecuteQuery();
            }
            var reloadedFile = web.GetFileByServerRelativeUrl(fileUrl);

            context.Load(reloadedFile);
            context.ExecuteQuery();

            ApplyFileProperties(context, fileProperties, reloadedFile);

            try
            {
                reloadedFile.PublishFileToLevel(FileLevel.Published);
            }
            catch
            {
                Log.Warn("Couldn't publish file " + fileUrl);
            }
        }
        private void ApplyFileProperties(ClientContext context, ShFileProperties fileProperties, File uploadFile)
        {
            if (fileProperties != null)
            {
                var filePropertiesWithTokensReplaced = new Dictionary <string, string>();
                foreach (KeyValuePair <string, string> keyValuePair in fileProperties.Properties)
                {
                    filePropertiesWithTokensReplaced.Add(keyValuePair.Key, ReplaceTokensInText(keyValuePair.Value, context));
                }
                uploadFile.SetFileProperties(filePropertiesWithTokensReplaced);

                if (uploadFile.Name.ToLower().EndsWith(".aspx"))
                {
                    AddWebParts(context, uploadFile, fileProperties.WebParts, fileProperties.ReplaceWebParts);
                }
                context.ExecuteQuery();
            }
        }
        private string GetFileUrl(string uploadTargetFolder, string pathToFileFromRootFolder, ShFileProperties fileProperties)
        {
            var fileUrl = Url.Combine(uploadTargetFolder, pathToFileFromRootFolder);

            if (fileProperties != null)
            {
                fileUrl = Url.Combine(uploadTargetFolder, fileProperties.Url);
            }

            return(fileUrl);
        }
        private FileCreationInformation GetFileCreationInformation(ClientContext context, string fileUrl, string filePath, string pathToFileFromRootFolder, ShFileProperties fileProperties)
        {
            var fileCreationInfo = new FileCreationInformation
            {
                Url       = fileUrl,
                Overwrite = true,
                Content   = System.IO.File.ReadAllBytes(filePath),
            };

            if (fileProperties != null)
            {
                if (fileProperties.ReplaceTokensInTextFile)
                {
                    var fileContents = System.IO.File.ReadAllText(filePath);
                    fileContents = ReplaceTokensInText(fileContents, context);

                    fileCreationInfo.Content = Encoding.UTF8.GetBytes(fileContents);
                }
            }


            return(fileCreationInfo);
        }