/// <summary> /// View Report is a viewer for the user. /// </summary> /// <param name="reportModel"></param> /// <param name="userId"></param> /// <returns></returns> public static ReportViewerModel View(ReportModel reportModel, string userId) { if (reportModel != null) { var reportviewerModel = new ReportViewerModel { Report = reportModel, ReportPath = "/ReportService/" + reportModel.SSRSReportName.Replace(".rdl", ""), Parameters = null, ServerUrl = reportServerPathViewer, UserName = userName, Password = password }; BaseModelContext <ReportUserActivityLogModel> reportUserActivityLogDb = new BaseModelContext <ReportUserActivityLogModel>(); ReportUserActivityLogModel reportUserActivityModel = new ReportUserActivityLogModel() { ReportId = reportModel.Id, UserId = userId, Activity = ReportUserActivityLogModel.ViewActivity }; reportUserActivityLogDb.Models.Add(reportUserActivityModel); reportUserActivityLogDb.SaveChanges(userId); return(reportviewerModel); } return(null); }
/// <summary> /// Run reports is used for scheduled reports. /// </summary> /// <param name="reportModel"></param> /// <param name="userId"></param> /// <returns></returns> public static FileHelper Run(ReportModel reportModel, string userId) { string URL = reportServerPathRunner + reportModel.SSRSReportName.Replace(".rdl", ""); string Command = "Render"; string Query = ""; FileHelper file = new FileHelper((!string.IsNullOrWhiteSpace(reportModel.FileName) ? reportModel.FileName : reportModel.SSRSReportName.Replace(".rdl", "")), userId); //Replace filename parameters. foreach (ReportParameterModel reportParameter in reportModel.ReportParameters) { //Setup the query if (!string.IsNullOrWhiteSpace(reportParameter.DefaultValue)) { Query += $"&{reportParameter.Name}={reportParameter.DefaultValue}"; } //Replace any parameter in the name. if (file.FileName.Contains("{" + reportParameter.Name + "}")) { string defaultValue = reportParameter.DefaultValue; //If the parameter is a date make sure we format it so its fiel friendly. if (!string.IsNullOrWhiteSpace(reportParameter.DataType) && reportParameter.DataType.Equals(ReportParameterModel.dateTimeType)) { DateTime dateTimeValue; DateTime.TryParse(reportParameter.DefaultValue, out dateTimeValue); defaultValue = (dateTimeValue != null ? dateTimeValue.ToString("yyyyMMdd") : ""); } file.FileName = file.FileName.Replace("{" + reportParameter.Name + "}", defaultValue); } } file.FileName = Regex.Replace(file.FileName, "-{2,}", "-"); file.FileName = file.FileName + "." + ReportModel.Extensions[reportModel.Format]; //Delete existing file if (file.Delete()) { URL = URL + "&rs:Command=" + Command + "&rs:Format=" + reportModel.Format + Query; System.Net.HttpWebRequest Req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL); NetworkCredential credential = new NetworkCredential(userName, password, domain); Req.Credentials = credential; Req.Method = "GET"; //Specify the path for saving. //Do File Service here and return FileService fileService = new FileService(); //file.FilePathAndName = string.Format("{0}{1}{2}", file.FilePath, file.FileName, @".pdf"); System.Net.WebResponse objResponse = Req.GetResponse(); System.IO.FileStream fs = new System.IO.FileStream(file.FilePathAndName, System.IO.FileMode.Create); System.IO.Stream stream = objResponse.GetResponseStream(); byte[] buf = new byte[1024]; int len = stream.Read(buf, 0, 1024); while (len > 0) { fs.Write(buf, 0, len); len = stream.Read(buf, 0, 1024); } stream.Close(); fs.Close(); BaseModelContext <ReportUserActivityLogModel> reportUserActivityLogDb = new BaseModelContext <ReportUserActivityLogModel>(); ReportUserActivityLogModel reportUserActivityModel = new ReportUserActivityLogModel() { ReportId = reportModel.Id, UserId = userId, Activity = ReportUserActivityLogModel.RunActivity, Parameters = Query }; reportUserActivityLogDb.Models.Add(reportUserActivityModel); reportUserActivityLogDb.SaveChanges(userId); BaseModelContext <ReportModel> reportDb = new BaseModelContext <ReportModel>(); reportModel.LastRun = DateTime.Now; reportDb.Entry(reportModel); reportDb.SaveChanges(userId); } return(file); }