1- Create A Library (FileDumpConfigurationSettings ) To set Timer Job Configuration
2- Create Class
Code :
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Web;
namespace NGOFileDumpProcess
{
class NGOFileDumpTimmerJob: SPJobDefinition
{
public NGOFileDumpTimmerJob()
: base()
{ }
public NGOFileDumpTimmerJob(string jobName, SPService service)
: base(jobName, service, null, SPJobLockType.None)
{
this.Title
= "NGO File Dump Process";
}
public NGOFileDumpTimmerJob(string jobName, SPWebApplication webapp)
: base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title
= " NGO File Dump Process";
}
#region Excute
Method Of Timer Jobs
public override void Execute(Guid targetInstanceId)
{
// Start
Getting All Library Name From List
//document
library name to download
try
{
DownLoadFiles();
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
public void DownLoadFiles()
{
string UrlandDumpLocation
= GetSiteUrlandDumpLocation();
if(UrlandDumpLocation!="")
{
string siteUrl
= UrlandDumpLocation.Split('|')[0].ToString();
string filePath
= UrlandDumpLocation.Split('|')[1].ToString();
string LibraryName
= UrlandDumpLocation.Split('|')[2].ToString();
//
getting Record of Curret month
string Month
= DateTime.Now.ToString("MM");
string Year
= DateTime.Now.ToString("yyyy");
//
using (SPSite site
= new SPSite(siteUrl))
{
using (SPWeb web
= site.OpenWeb())
{
// get
the required document library
SPList docLib
= web.Lists[LibraryName];
// code
to download documents
// loop
through each item or document in the document library
foreach (SPListItem item in docLib.Items)
{
string FolderPathLocation
= "";
// Access
the file
SPFile file
= item.File;
string fileCreateiondateMonth
= Convert.ToDateTime(item["Created"].ToString()).ToString("MM");
string fileCreateiondateYear
= Convert.ToDateTime(item["Created"].ToString()).ToString("yyyy");
if(Month==
fileCreateiondateMonth && Year== fileCreateiondateYear)
{
if (file
!= null)
{
//
retrieve the file as a byte array
try
{
SPFieldLookupValueCollection values
= new SPFieldLookupValueCollection(item["Location"].ToString());
foreach (SPFieldLookupValue value in values)
{
Directory.CreateDirectory(filePath
+ value.LookupValue);
byte[] bArray
= file.OpenBinary();
FolderPathLocation = Path.Combine(filePath + value.LookupValue, file.Name);
//open
the file stream and write the file
using (FileStream fs
= new FileStream(FolderPathLocation, FileMode.Create, FileAccess.ReadWrite))
{
fs.Write(bArray, 0, bArray.Length);
}
}
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
}
}
}
public string GetSiteUrlandDumpLocation()
{
string UrlandDumpLocation
= string.Empty;
try
{
SPWebApplication webApp
= this.Parent as SPWebApplication;
SPWeb web
= webApp.Sites[0].RootWeb;
SPList spList
= null;
web.AllowUnsafeUpdates = true;
try
{
spList = web.Lists.TryGetList("FileDumpConfigurationSettings");
}
catch (Exception ex)
{
string Title
= "Library Name is not found or miss matched";
//ErrorLog(Title,
ex.Message);
}
SPListItemCollection items
= spList.GetItems();
foreach (SPListItem spListItem in items)
{
if (UrlandDumpLocation
== "")
{
UrlandDumpLocation = spListItem["Title"].ToString().Trim()
+ "|" + spListItem["DumpLocationPath"].ToString().Trim()+ "|" +
spListItem["LibraryName"].ToString().Trim();
}
}
}
catch (Exception ex)
{
string Title
= "Function-GetSiteUrlandDumpLocation() error";
//
ErrorLog(Title, ex.Message);
}
return UrlandDumpLocation;
}
}
}
#endregion
3- Add The Feature and Add
Event receiver
Code-
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace NGOFileDumpProcess.Features.NGOFileDumpProcessTimmerJob
{
/// <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("d38cdefb-0535-410c-bcc8-d515748fc7e4")]
public class NGOFileDumpProcessTimmerJobEventReceiver : SPFeatureReceiver
{
const string JobName
= "NGO File Dump Process";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
SPWebApplication parentWebApp
= (SPWebApplication)properties.Feature.Parent;
SPSite site
= properties.Feature.Parent as SPSite;
CreateJob(parentWebApp);
});
}
catch (Exception ex)
{
throw ex;
}
}
private bool CreateJob(SPWebApplication site)
{
bool jobCreated
= false;
try
{
NGOFileDumpTimmerJob job
= new NGOFileDumpTimmerJob(JobName, site);
SPMinuteSchedule schedule
= new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 15;
job.Schedule = schedule;
job.Update();
}
catch (Exception)
{
return jobCreated;
}
return jobCreated;
}
public bool DeleteExistingJob(string jobName, SPWebApplication site)
{
bool jobDeleted
= false;
try
{
foreach (SPJobDefinition job in site.JobDefinitions)
{
if (job.Name
== jobName)
{
job.Delete();
jobDeleted = true;
}
}
}
catch (Exception)
{
return jobDeleted;
}
return jobDeleted;
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
lock (this)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
SPWebApplication parentWebApp
= (SPWebApplication)properties.Feature.Parent;
DeleteExistingJob(JobName, parentWebApp);
});
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
4- Add Code in Template.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" AlwaysForceInstall="True" ActivateOnDefault="FALSE">
</Feature>
5 – select Scope of Timer Job : web Application
No comments:
Post a Comment