Exemplo n.º 1
0
        /// <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);
        }