Example #1
0
        public File AddTemplateFile(string urlOfFile, TemplateFileType templateFileType)
        {
            ClientRuntimeContext context = base.Context;

            if (base.Context.ValidateOnClient)
            {
                if (urlOfFile == null)
                {
                    throw ClientUtility.CreateArgumentNullException("urlOfFile");
                }
                if (urlOfFile != null && urlOfFile.Length == 0)
                {
                    throw ClientUtility.CreateArgumentException("urlOfFile");
                }
                if (templateFileType != TemplateFileType.StandardPage && templateFileType != TemplateFileType.WikiPage && templateFileType != TemplateFileType.FormPage && templateFileType != TemplateFileType.ClientSidePage)
                {
                    throw ClientUtility.CreateArgumentException("templateFileType");
                }
            }
            File file = new File(context, new ObjectPathMethod(context, base.Path, "AddTemplateFile", new object[]
            {
                urlOfFile,
                templateFileType
            }));

            file.Path.SetPendingReplace();
            ObjectIdentityQuery objectIdentityQuery = new ObjectIdentityQuery(file.Path);

            context.AddQueryIdAndResultObject(objectIdentityQuery.Id, file);
            context.AddQuery(objectIdentityQuery);
            base.AddChild(file);
            return(file);
        }
        internal SPOFile AddTemplateFile(string leafName, TemplateFileType templateType, bool throwIfExists)
        {
            if (string.IsNullOrEmpty(leafName))
            {
                throw new ArgumentNullException("The leafName name cannot be null.");
            }
            if (!leafName.ToLower().EndsWith(".aspx"))
            {
                leafName += ".aspx";
            }

            SPOFile existingFile = null;

            try
            {
                existingFile = GetFile(leafName);
            }
            catch { }
            if (existingFile != null && existingFile.Exists)
            {
                if (throwIfExists)
                {
                    throw new FileExistsException("The specified file already exists.");
                }

                return(existingFile);
            }

            string url  = ServerRelativeUrl.TrimEnd('/') + "/" + leafName;
            var    file = _folder.Files.AddTemplateFile(url, templateType);

            SPOFile.LoadFile(SPOSiteContext.CurrentSiteContext.Context, file);
            return(new SPOFile(file));
        }
 public SPOFile AddTemplateFile(string leafName, TemplateFileType templateType)
 {
     return(AddTemplateFile(leafName, templateType, false));
 }
Example #4
0
 public IFile AddTemplateFile(string serverRelativePageName, TemplateFileType templateFileType)
 {
     return(AddTemplateFileAsync(serverRelativePageName, templateFileType).GetAwaiter().GetResult());
 }
Example #5
0
        public async Task <IFile> AddTemplateFileAsync(string serverRelativePageName, TemplateFileType templateFileType)
        {
            if (string.IsNullOrEmpty(serverRelativePageName))
            {
                throw new ArgumentNullException(nameof(serverRelativePageName));
            }

            // Replace # with - as AddTemplateFile cannot handle an # in the name. This replacement is the same behavior than the SharePoint UI does
            var    encodedServerRelativePageName = WebUtility.UrlEncode(serverRelativePageName.Replace("'", "''").Replace("#", "-")).Replace("+", "%20");
            var    newFile           = CreateNewAndAdd() as File;
            string fileCreateRequest = $"_api/web/getFolderById('{{Parent.Id}}')/files/AddTemplateFile(urlOfFile='{encodedServerRelativePageName}',templateFileType={(int)templateFileType})";
            var    api = new ApiCall(fileCreateRequest, ApiType.SPORest);
            await newFile.RequestAsync(api, HttpMethod.Post).ConfigureAwait(false);

            return(newFile);
        }
Example #6
0
        public async Task <IFile> AddTemplateFileAsync(string serverRelativePageName, TemplateFileType templateFileType)
        {
            if (string.IsNullOrEmpty(serverRelativePageName))
            {
                throw new ArgumentNullException(nameof(serverRelativePageName));
            }
            var    encodedServerRelativePageName = WebUtility.UrlEncode(serverRelativePageName);
            var    newFile           = CreateNewAndAdd() as File;
            string fileCreateRequest = $"_api/web/getFolderById('{{Parent.Id}}')/files/AddTemplateFile(urlOfFile='{encodedServerRelativePageName}',templateFileType={(int)templateFileType})";
            var    api = new ApiCall(fileCreateRequest, ApiType.SPORest);
            await newFile.RequestAsync(api, HttpMethod.Post).ConfigureAwait(false);

            return(newFile);
        }
        internal SPOFile AddTemplateFile(string leafName, TemplateFileType templateType, bool throwIfExists)
        {
            if (string.IsNullOrEmpty(leafName)) throw new ArgumentNullException("The leafName name cannot be null.");
            if (!leafName.ToLower().EndsWith(".aspx"))
                leafName += ".aspx";

            SPOFile existingFile = null;
            try
            {
                existingFile = GetFile(leafName);
            }
            catch { }
            if (existingFile != null && existingFile.Exists)
            {
                if (throwIfExists)
                    throw new FileExistsException("The specified file already exists.");

                return existingFile;
            }

            string url = ServerRelativeUrl.TrimEnd('/') + "/" + leafName;
            var file = _folder.Files.AddTemplateFile(url, templateType);
            SPOFile.LoadFile(SPOSiteContext.CurrentSiteContext.Context, file);
            return new SPOFile(file);
        }
 public SPOFile AddTemplateFile(string leafName, TemplateFileType templateType)
 {
     return AddTemplateFile(leafName, templateType, false);
 }
Example #9
0
        protected virtual AttachmentInfo GenerateFileReport(HttpContext context, GetFileReportDataHandler handler)
        {
            YZRequest request     = new YZRequest(context);
            string    fileid      = request.GetString("fileid");
            string    template    = request.GetString("template");
            int       tagfolderid = request.GetInt32("tagfolderid");
            string    name        = request.GetString("name");
            string    method      = request.GetString("method");
            string    reportType  = method.Substring(8, method.Length - 14);

            JObject jPost    = request.GetPostData <JObject>();
            JObject jProcess = (JObject)jPost["process"];
            JObject jChart   = (JObject)jPost["chart"];
            Image   chart    = this.GetImage(jChart);

            string           templatePath     = context.Server.MapPath(String.Format("~/YZSoft/BPA/processreport/templates/{0}/{1}", reportType, template));
            TemplateFileType templeteFileType = this.GetTemplateFileType(template);
            string           ext = System.IO.Path.GetExtension(template);

            AttachmentInfo attachment = new AttachmentInfo();

            attachment.Name = name + ext;
            attachment.Ext  = ext;

            DataSet dataset = handler.Invoke(fileid, tagfolderid, jProcess, chart);

            switch (templeteFileType)
            {
            case TemplateFileType.Excel:
                HSSFWorkbook book;
                using (System.IO.FileStream stream = new System.IO.FileStream(templatePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    book = new HSSFWorkbook(stream);
                }

                YZExcelGenerate.Fill(book, null, dataset);
                YZExcelGenerate.PrepareForOutput(book);
                attachment = AttachmentManager.SaveAsAttachment(book, attachment);
                break;

            default:
                XWPFDocument doc;
                using (System.IO.FileStream stream = new System.IO.FileStream(templatePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    doc = new XWPFDocument(stream);
                }
                WordGenerator.Fill(doc, dataset);
                attachment = AttachmentManager.SaveAsAttachment(doc, attachment);
                break;
            }

            YZSoft.FileSystem.File file = new YZSoft.FileSystem.File();
            file.FolderID = tagfolderid;
            file.FileID   = attachment.FileID;
            file.AddBy    = YZAuthHelper.LoginUserAccount;
            file.AddAt    = DateTime.Now;
            file.Flag     = "Generate";

            using (IYZDbProvider provider = YZDbProviderManager.DefaultProvider)
            {
                using (IDbConnection cn = provider.OpenConnection())
                {
                    file.OrderIndex = YZSoft.FileSystem.DirectoryManager.GetFileNextOrderIndex(provider, cn, tagfolderid);
                    FileSystem.DirectoryManager.Insert(provider, cn, file);
                }
            }

            return(attachment);
        }