using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Workflow;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Collections;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint.Publishing;
namespace SP2010_TimerJob
{
class SP2010_TimerJob
{
}
public class SP2010_Timer_Job : SPJobDefinition
{
DateTime today = DateTime.Now;
public string[] strArray;
StringBuilder sbwfIds = new StringBuilder();
StringBuilder sbResignationID = new StringBuilder();
public bool isWFRunning;
public string conString;
public string name, passport, dept, nationality;
#region Sql Variables
public SqlConnection con;
SqlCommand cmd;
SqlDataAdapter sqlDa;
DataTable dt;
#endregion
public const string JOB_DEFINITION_NAME = "SP2010_TimerJob";
public const string JOB_DEFINITION_TITLE = "SP2010 TimerJob";
public SP2010_Timer_Job()
{
Title = JOB_DEFINITION_TITLE;
}
public SP2010_Timer_Job(SPWebApplication webApplication)
: base(JOB_DEFINITION_NAME, webApplication, null, SPJobLockType.Job)
{
Title = JOB_DEFINITION_TITLE;
}
public override void Execute(Guid targetInstanceId)
{
base.Execute(targetInstanceId);
SPWebApplication webApp = WebApplication;
try
{
using (var site = new SPSite(webApp.Sites[0].ID))
{
using (var oWebsite = site.OpenWeb())
{
//SPListCollection collLists = oWebsite.Lists;
SPList oList = oWebsite.Lists["Shared Documents"];
if (oList.Title == "Shared Documents")
{
if (oList.BaseType == SPBaseType.DocumentLibrary)
{
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;
if (!oDocumentLibrary.IsCatalog && oList.BaseTemplate != SPListTemplateType.XMLForm)
{
SPFolder folder = oWebsite.GetFolder(System.Web.Configuration.WebConfigurationManager.AppSettings["PassportCopyUrl"].ToString());
if (folder.Exists)
{
SPFileCollection collFile = folder.Files;
oWebsite.AllowUnsafeUpdates = true;
foreach (SPFile oFile in collFile)
{
if (!Convert.ToBoolean(oFile.Item["Refiled"]))
{
if (oFile.Item["Employee_x0020_ID"] != null)
{
PassportCopy(Convert.ToInt32(oFile.Item["Employee_x0020_ID"].ToString()));
if (!string.IsNullOrEmpty(name))
{
oFile.Item["Employee_x0020_Name"] = name.ToString().Trim();
}
oFile.Item["Refiled"] = System.Web.Configuration.WebConfigurationManager.AppSettings["Refiled"].ToString();
oFile.Item.Update();
oDocumentLibrary.Update();
oWebsite.Update();
}
}
}
}
oWebsite.AllowUnsafeUpdates = false;
}
}
}
}
}
}
catch
{
}
}
#region Sql Connection
public void SqlConnection()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.AppSettings["ConnectionString"].ToString());
if (con != null)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
}
});
}
#endregion
#region Stored Procedure PassportCopy
public void PassportCopy(int EmpID)
{
try
{
SqlConnection();
cmd = new SqlCommand("sp_PassportCopy", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@userid", EmpID); //.DbType = DbType.Int32;
sqlDa = new SqlDataAdapter(cmd);
dt = new DataTable();
sqlDa.Fill(dt);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["AAA"] != null)
{
name = dt.Rows[0]["AAA"].ToString();
}
if (dt.Rows[0]["BBB"] != null)
{
passport = dt.Rows[0]["BBB"].ToString();
}
if (dt.Rows[0]["CCC"] != null)
{
dept = dt.Rows[0]["CCC"].ToString();
}
if (dt.Rows[0]["DDD"] != null)
{
nationality = dt.Rows[0]["DDD"].ToString();
}
}
}
}
catch (Exception obj)
{
}
finally
{
con.Close();
}
}
#endregion
}
}
after that go to --> C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\OWSTIMER.EXE.CONFIG and edit and change as follows.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
</runtime>
<appSettings>
<add key="ConnectionString" value="Data Source=Trainingsql2k8;Initial Catalog=SP2010_PRODUCTION;Trusted_Connection=True" />
<add key="PassportCopyUrl" value="/shared documents/Passport Copy" />
<add key="Refiled" value="1" />
</appSettings>
</configuration>
note: after add above configuration file mate sure restart the SharePoint timer job service in services (start -> run -> services.msc)
note: after add above configuration file mate sure restart the SharePoint timer job service in services (start -> run -> services.msc)
following code is SharePoint 2010 timer job code
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Linq;
namespace SP2010_TimerJob.Features.SP2010_TimerJob_Feature
{
/// <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("327dbb7a-1dcb-4c80-8c61-96d276660370")]
public class SP2010_TimerJob_FeatureEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var webApp = properties.Feature.Parent as SPWebApplication;
if (webApp == null) throw new Exception("webApp");
var FCJOB = from SPJobDefinition job in webApp.JobDefinitions
where job.Name == SP2010_Timer_Job.JOB_DEFINITION_NAME
select job;
if (FCJOB.Count() > 0)
FCJOB.First().Delete();
var DailySchedule = new SPDailySchedule
{
BeginHour = 0,
BeginMinute = 0,
BeginSecond = 0,
EndHour = 2,
};
var myJOb = new SP2010_Timer_Job(webApp)
{
Schedule = DailySchedule,
IsDisabled = false
};
myJOb.Update();
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
var webApp = properties.Feature.Parent as SPWebApplication;
if (webApp == null) throw new Exception("webApp");
var FCJOB = from SPJobDefinition job in webApp.JobDefinitions
where job.Name == SP2010_Timer_Job.JOB_DEFINITION_NAME
select job;
if (FCJOB.Count() > 0)
FCJOB.First().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