Search This Blog

Wednesday, May 8, 2013

Timer Job Upload Document Lib

// -----------------------------------------------------------------------
// <copyright file="ImportPDF.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace AutomationImport
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint.Administration;
    using System.IO;
    using Microsoft.SharePoint;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ImportPDF : SPJobDefinition
    {
        public ImportPDF()

            : base()
        {

        }

        public ImportPDF(string jobName, SPService service, SPServer server, SPJobLockType targetType)

            : base(jobName, service, server, targetType)
        {

        }

        public ImportPDF(string jobName, SPWebApplication webApplication)

            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {

            this.Title = "Automated Import PDF Service";

        }
        public override void Execute(Guid contentDbId)
        {
            var localPath = "D:\\Code";
            var directory = new DirectoryInfo(localPath);
            if (directory.Exists)
            {
                foreach (FileInfo f in directory.GetFiles())
                {
                    using (SPSite site = this.WebApplication.Sites[0])
                    {
                        using (SPWeb currWeb = site.OpenWeb())
                        {
                            currWeb.AllowUnsafeUpdates = true;
                            //Shared is the document library
                            SPList docLibrary = currWeb.Lists["Shared Documents"];
                            SPListItemCollection ItemsColl = docLibrary.Items;
                            foreach (SPListItem item in ItemsColl)
                            {
                                item.File.CheckOut();
                                item.File.Delete();
                                item.Delete();
                            }
                            using (FileStream fs = File.OpenRead(string.Concat(localPath, "\\", f.Name)))
                            {
                                //Name is the name of the file
                                SPFile spfile = currWeb.Files.Add(currWeb.Url.ToString() + "/" + docLibrary.Title.ToString()
                                                                         + "/" + f.Name, fs);
                                spfile.Update();
                            }
                            docLibrary.Update();
                            currWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
        }
    }
}

 -----------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace AutomationImport.Features.AutomatedPDFService
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("5abb348f-ab2d-4bc9-9769-dd4616d1dc13")]
    public class AutomatedPDFServiceEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
           
          foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
          {

                if (job.Name == "AutomatedImportPDFService")

                    job.Delete();

            }

            // install the job

          ImportPDF customTimerJob = new ImportPDF("AutomatedImportPDFService", site.WebApplication);

            // Create a schedule for timer job
            SPMinuteSchedule customSchedule = new SPMinuteSchedule();  // You can create Hourly, Daily,  Weekly schedules as well
            customSchedule.BeginSecond = 0;
            customSchedule.EndSecond = 59;
            customSchedule.Interval = 10;  //Run every 2 minutes

            // assign the schedule to the timer job
            customTimerJob.Schedule = customSchedule;
            customTimerJob.Update();

            //SPDailySchedule schedule = new SPDailySchedule();
            //schedule.BeginHour = 23;
            //schedule.BeginMinute = 5;
            //schedule.BeginSecond = 0;
            //schedule.EndSecond = 15;
            //schedule.EndMinute = 5;
            //schedule.EndHour = 23;
            //customTimerJob.Schedule = schedule;
            //customTimerJob.Update();
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            SPSite site = properties.Feature.Parent as SPSite;
            // delete the job
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {

                if (job.Name == "AutomatedImportPDFService")
                    job.Delete();
            }
        }

        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}






No comments:

Post a Comment