/// <summary> /// This method will upload pdf file to media library /// </summary> /// <param name="fullPath"></param> /// <param name="fileName"></param> /// <returns></returns> private Item CreateMediaItem(string fullPath, string fileName) { // Create the options Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions(); // Store the file in the database, not as a file options.FileBased = false; options.Language = Sitecore.Globalization.Language.Parse(Sitecore.Configuration.Settings.DefaultLanguage); // Remove file extension from item name options.IncludeExtensionInItemName = false; // Overwrite any existing file with the same name options.KeepExisting = false; // Do not make a versioned template options.Versioned = false; // Set the database options.Database = Sitecore.Configuration.Factory.GetDatabase("master"); Item mediaItem = null; // set the path string sitecorePath = Constants.SITECORE_PATH_TO_PDF_UPLOAD; Item pdfFolder = MASTERDB.GetItem(sitecorePath); if (pdfFolder != null) { DateTime dt = DateTime.Parse(tbDate.Text); Item yearFolder = pdfFolder.GetChildren().FirstOrDefault(i => i.Name == dt.Year.ToString()); if (yearFolder == null) { using (new SecurityDisabler()) { yearFolder = pdfFolder.Add(dt.Year.ToString(), MediaFolderTemplateItem); itemsToPublish.Add(yearFolder); } } Item monthFolder = null; if (yearFolder != null) { monthFolder = yearFolder.GetChildren().FirstOrDefault(i => i.Name == dt.Month.ToString()); if (monthFolder == null) { using (new SecurityDisabler()) { monthFolder = yearFolder.Add(dt.Month.ToString(), MediaFolderTemplateItem); itemsToPublish.Add(monthFolder); } } } Item dayFolder = null; if (monthFolder != null) { dayFolder = monthFolder.GetChildren().FirstOrDefault(i => i.Name == dt.Day.ToString()); if (dayFolder == null) { using (new SecurityDisabler()) { dayFolder = monthFolder.Add(dt.Day.ToString(), MediaFolderTemplateItem); itemsToPublish.Add(dayFolder); } } } if (dayFolder != null) { options.Destination = string.Format("{0}/{1}/{2}/{3}/{4}", sitecorePath, yearFolder.Name, monthFolder.Name, dayFolder.Name, fileName.Replace(".pdf", "")); } else { options.Destination = string.Format("{0}/{1}", sitecorePath, fileName.Replace(".pdf", "")); } using (new Sitecore.SecurityModel.SecurityDisabler()) { mediaItem = Sitecore.Resources.Media.MediaManager.Creator.CreateFromFile(@fullPath, options); itemsToPublish.Add(mediaItem); } } return(mediaItem); }
/// <summary> /// Create pdf items in globals & fill fields of created item /// </summary> /// <param name="mediaItem"></param> private void CreatePDFItem(Item mediaItem) { if (MASTERDB != null) { // Get Globals folder SiteItem objSite = SiteItem.GetSiteRoot(MASTERDB); if (objSite != null) { PDFRootItem objPDFRoot = objSite.GetGlobalsFolder().GetPDFRootItem(); TemplateItem template = MASTERDB.GetTemplate(ImportantNoticeItem.TemplateId); if (objPDFRoot != null && template != null) { // Sanitize Sitecore item name string sanitizedName = ItemUtil.ProposeValidItemName(tbTitle.Text); string strDate = tbDate.Text.Replace("/", ""); DateTime date = DateTime.ParseExact(strDate, "MMddyyyy", CultureInfo.InvariantCulture); if (date != null && !string.IsNullOrEmpty(sanitizedName)) { using (new Sitecore.SecurityModel.SecurityDisabler()) { // Create item ImportantNoticeItem createdPDFItem = objPDFRoot.InnerItem.Add(sanitizedName, template); if (createdPDFItem != null) { //Begin editing createdPDFItem.InnerItem.Editing.BeginEdit(); // Date createdPDFItem.InnerItem.Fields["Date"].Value = date.ToString("yyyyMMddTHHmmss", CultureInfo.InvariantCulture); // Title createdPDFItem.InnerItem.Fields["Title"].Value = sanitizedName; // Subject createdPDFItem.InnerItem.Fields["Subject"].Value = tbSubject.Text; // Subsidiary Tag + Category tag string tags = ddlSubsidiary.SelectedValue; foreach (ListItem li in lbCategories.Items) { if (li.Selected) { tags += "|" + li.Value; } } createdPDFItem.Tags.GetField("Tag").Value = tags; // Document ID createdPDFItem.InnerItem.Fields["Document ID"].Value = tbNoticeNumber.Text; // Flag createdPDFItem.InnerItem.Fields["Flag"].Value = cbFlag.Checked ? "1" : ""; // PDF File string strPDFFile = String.Format("<file mediaid=\"{0}\" src=\"~/media/{1}.ashx\" />", mediaItem.ID.ToString(), mediaItem.ID.ToShortID().ToString()); createdPDFItem.InnerItem.Fields["PDF File"].Value = strPDFFile; //Close the editing state createdPDFItem.InnerItem.Editing.EndEdit(); itemsToPublish.Add(createdPDFItem.InnerItem.Parent.Parent.Parent); itemsToPublish.Add(createdPDFItem.InnerItem.Parent.Parent); itemsToPublish.Add(createdPDFItem.InnerItem.Parent); itemsToPublish.Add(createdPDFItem); } } } } } } }