Esempio n. 1
0
        static SPFileInfo GetOrCreateWebPartPage(ClientContext context, string listTitle, string pageTitle)
        {
            Logs.LogToFile(LogLevel.Info, DateTime.Now, "Start to get the page information. Page name: " + pageTitle);
            var fileInfo = new SPFileInfo();
            var file     = GetSPOFile(context, listTitle, pageTitle);

            if (file == null)
            {
                Logs.LogToFile(LogLevel.Info, DateTime.Now, "Start to create the page. Page name: " + pageTitle);
                var web = context.Web;
                context.Load(web, x => x.Lists);
                context.ExecuteQuery();
                var pageLib = web.Lists.GetByTitle(listTitle);
                context.Load(pageLib, x => x.RootFolder);
                context.ExecuteQuery();
                var fileUrl = pageLib.RootFolder.ServerRelativeUrl + "/" + pageTitle;
                file = pageLib.RootFolder.Files.AddTemplateFile(pageLib.RootFolder.ServerRelativeUrl + "/" + pageTitle, TemplateFileType.StandardPage);
                context.Load(file);
                context.ExecuteQuery();
                Logs.LogToFile(LogLevel.Info, DateTime.Now, "The page has been created. Page name: " + pageTitle);

                fileInfo.Title        = file.Title;
                fileInfo.Path         = file.ServerRelativeUrl;
                fileInfo.IsNewCreated = true;
            }
            else
            {
                fileInfo.Path         = file.ServerRelativeUrl;
                fileInfo.Title        = file.Title;
                fileInfo.IsNewCreated = false;
            }
            return(fileInfo);
        }
Esempio n. 2
0
        private static SPFileInfo GetorUploadLinkFile(ClientContext context, string listTitle, string filename = "link.html")
        {
            Logs.LogToFile(LogLevel.Info, DateTime.Now, "Start to get the WebPart link file information. File name: " + filename);
            var fileInfo = new SPFileInfo();
            var file     = GetSPOFile(context, listTitle, filename);

            if (file == null)
            {
                Logs.LogToFile(LogLevel.Info, DateTime.Now, "Start to upload the WebPart link file. File name:  " + filename);
                var web = context.Web;
                context.Load(web, x => x.Lists);
                context.ExecuteQuery();
                var pageLib = web.Lists.GetByTitle(listTitle);
                context.Load(pageLib, x => x.RootFolder);
                context.ExecuteQuery();

                var fileUrl  = pageLib.RootFolder.ServerRelativeUrl + "/" + filename;
                var fci      = new FileCreationInformation();
                var filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
                fci.Content = System.IO.File.ReadAllBytes(filePath);
                fci.Url     = fileUrl;
                file        = pageLib.RootFolder.Files.Add(fci);
                context.Load(file);
                context.ExecuteQuery();
                Logs.LogToFile(LogLevel.Info, DateTime.Now, "The file has been uploaded. File name: " + filename);

                fileInfo.Path         = file.ServerRelativeUrl;
                fileInfo.Title        = file.Title;
                fileInfo.IsNewCreated = true;
            }
            else
            {
                fileInfo.Path         = file.ServerRelativeUrl;
                fileInfo.Title        = file.Title;
                fileInfo.IsNewCreated = false;
            }
            return(fileInfo);
        }