using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.CustomerProfiles;
namespace CustomerTimerJob
{
class CustomerTimerJob : SPJobDefinition
{
public const string CustomerRPFOILELIST_TIMERJOB_NAME = "Customerr Timer Job";
public CustomerTimerJob()
: base()
{
}
public CustomerTimerJob(SPWebApplication web)
: base(CustomerRPFOILELIST_TIMERJOB_NAME, web, null, SPJobLockType.Job)
{
this.Title = "Customerr Timer Job";
}
public override void Execute(Guid targetInstanceId)
{
SPWebApplication webApp = this.Parent as SPWebApplication;
SPList Customerr = webApp.Sites[0].RootWeb.Lists["Customerr"];
SPServiceContext spServiceContext = SPServiceContext.GetContext(webApp.Sites[0]);
CustomerrProfileManager CustomerrProfileManager = new CustomerrProfileManager(spServiceContext);
DateTime fromDate = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1));
CustomerrProfileChangeQuery CustomerrProfileChangeQuery = new CustomerrProfileChangeQuery(false, true);
CustomerrProfileChangeToken CustomerrProfileChangeToken = new CustomerrProfileChangeToken(fromDate);
CustomerrProfileChangeQuery.ChangeTokenStart = CustomerrProfileChangeToken;
CustomerrProfileChangeQuery.SingleValueProperty = true;
CustomerrProfileChangeQuery.CustomerrProfile = true;
CustomerrProfilePropertyName CustomerrProfilePropertyName = new CustomerrProfilePropertyName();
if (Customerr != null)
{
SPCustomerr spCustomerr;
foreach (SPListItem item in Customerr.Items)
{
if (item["Login_x0020_Name"] != null)
{
string fieldValue = item["Login_x0020_Name"].ToString();
SPFieldCustomerrValue CustomerrValue = new SPFieldCustomerrValue(webApp.Sites[0].RootWeb, fieldValue);
spCustomerr = CustomerrValue.Customerr;
string[] spCustomerrAccountName = spCustomerr.LoginName.Split(new string[] { "i:0#.w|" }, StringSplitOptions.RemoveEmptyEntries);
for (int lenght = 0; lenght < spCustomerrAccountName.Length; lenght++)
{
if (spCustomerrAccountName[lenght].ToString() != "")
{
getChangePropertyValue(CustomerrProfileManager, spCustomerrAccountName[lenght], CustomerrProfileChangeQuery, CustomerrProfilePropertyName);
updateCustomerr(webApp.Sites[0].RootWeb, CustomerrProfilePropertyName, item, false);
}
}
}
else if (item["Employee_x0020_ID"].ToString() != null)
{
string accountName = GetLoginName(item["Employee_x0020_ID"].ToString(), CustomerrProfileManager, CustomerrProfilePropertyName);
getChangePropertyValue(CustomerrProfileManager, accountName, CustomerrProfileChangeQuery, CustomerrProfilePropertyName);
updateCustomerr(webApp.Sites[0].RootWeb, CustomerrProfilePropertyName, item, true);
}
}
}
}
private void updateCustomerr(SPWeb sPWeb, CustomerrProfilePropertyName CustomerrProfilePropertyName, SPListItem item, bool isLogInNameEmpty)
{
try
{
if (isLogInNameEmpty)
{
if (CustomerrProfilePropertyName.logInName != null)
{
item["Login_x0020_Name"] = CustomerrProfilePropertyName.logInName.ToString();
}
}
if (CustomerrProfilePropertyName.employeeNumberValue != null)
{
item["Employee_x0020_ID"] = CustomerrProfilePropertyName.employeeNumberValue.ToString();
}
if (CustomerrProfilePropertyName.preferredNameValue != null)
{
item["Display_x0020_Name"] = CustomerrProfilePropertyName.preferredNameValue.ToString();
}
if (CustomerrProfilePropertyName.designaitonValue != null)
{
item["Designation"] = CustomerrProfilePropertyName.designaitonValue.ToString();
}
if (CustomerrProfilePropertyName.locationValue != null)
{
item["Location"] = CustomerrProfilePropertyName.locationValue.ToString();
}
if (CustomerrProfilePropertyName.departmentValue != null)
{
item["Department"] = CustomerrProfilePropertyName.departmentValue.ToString();
}
if (CustomerrProfilePropertyName.mobileValue != null)
{
item["Mobile"] = CustomerrProfilePropertyName.mobileValue.ToString();
}
if (CustomerrProfilePropertyName.deskNumberValue != null)
{
item["Desk_x0020_Number"] = CustomerrProfilePropertyName.deskNumberValue.ToString();
}
sPWeb.AllowUnsafeUpdates = true;
item.Update();
sPWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
}
}
private string GetLoginName(string strEmpID, CustomerrProfileManager CustomerrProfileManager, CustomerrProfilePropertyName CustomerrProfilePropertyName)
{
string strLoginName = "";
try
{
foreach (CustomerrProfile Customerr in CustomerrProfileManager)
{
if (Customerr["employeeNumber"].Value != null)
{
if (Customerr["employeeNumber"].Value.ToString() == strEmpID)
{
strLoginName = Customerr["AccountName"].Value.ToString();
CustomerrProfilePropertyName.logInName = Customerr["AccountName"].Value.ToString();
break;
}
}
}
}
catch (Exception ex)
{
}
return strLoginName;
}
private void getChangePropertyValue(CustomerrProfileManager CustomerrProfileManager, string logInName, CustomerrProfileChangeQuery CustomerrProfileChangeQuery, CustomerrProfilePropertyName CustomerrProfilePropertyName)
{
try
{
CustomerrProfileChangeCollection CustomerrProfileChangeColl = CustomerrProfileManager.GetCustomerrProfile(logInName).GetChanges(CustomerrProfileChangeQuery);
foreach (CustomerrProfileChange CustomerrProfileChange in CustomerrProfileChangeColl)
{
if (CustomerrProfileChange is CustomerrProfileSingleValueChange)
{
CustomerrProfileSingleValueChange singleValueChange = (CustomerrProfileSingleValueChange)CustomerrProfileChange;
if (singleValueChange.ProfileProperty.Name.Equals(CustomerrProfilePropertyName.employeeNumber, StringComparison.OrdinalIgnoreCase))
{
if (singleValueChange.NewValue.ToString() != "")
{
CustomerrProfilePropertyName.employeeNumberValue = singleValueChange.NewValue.ToString();
}
}
if (singleValueChange.ProfileProperty.Name.Equals(CustomerrProfilePropertyName.preferredName, StringComparison.OrdinalIgnoreCase))
{
if (singleValueChange.NewValue.ToString() != "")
{
CustomerrProfilePropertyName.preferredNameValue = singleValueChange.NewValue.ToString();
}
}
if (singleValueChange.ProfileProperty.Name.Equals(CustomerrProfilePropertyName.designaiton, StringComparison.OrdinalIgnoreCase))
{
if (singleValueChange.NewValue.ToString() != "")
{
CustomerrProfilePropertyName.designaitonValue = singleValueChange.NewValue.ToString();
}
}
if (singleValueChange.ProfileProperty.Name.Equals(CustomerrProfilePropertyName.location, StringComparison.OrdinalIgnoreCase))
{
if (singleValueChange.NewValue.ToString() != "")
{
CustomerrProfilePropertyName.locationValue = singleValueChange.NewValue.ToString();
}
}
if (singleValueChange.ProfileProperty.Name.Equals(CustomerrProfilePropertyName.department, StringComparison.OrdinalIgnoreCase))
{
if (singleValueChange.NewValue.ToString() != "")
{
CustomerrProfilePropertyName.mobileValue = singleValueChange.NewValue.ToString();
}
}
if (singleValueChange.ProfileProperty.Name.Equals(CustomerrProfilePropertyName.deskNumber, StringComparison.OrdinalIgnoreCase))
{
if (singleValueChange.NewValue.ToString() != "")
{
CustomerrProfilePropertyName.deskNumberValue = singleValueChange.NewValue.ToString();
}
}
}
}
}
catch (Exception ex)
{
}
}
}
}
--------------------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace CustomerTimerJob.Features.Feature1
{
[Guid("344b4b58-f515-4903-8a51-a232bb2a7b8a")]
public class Feature1EventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
DeleteJob(webApp.JobDefinitions);
CustomerTimerJob CustomerTimerJob = new CustomerTimerJob(webApp);
SPDailySchedule spDailySchedule = new SPDailySchedule();
spDailySchedule.BeginHour = 0;
spDailySchedule.BeginMinute = 0;
spDailySchedule.BeginSecond = 0;
spDailySchedule.EndHour = 23;
CustomerTimerJob.Schedule = spDailySchedule;
CustomerTimerJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
DeleteJob(webApp.JobDefinitions);
}
private void DeleteJob(SPJobDefinitionCollection sPJobDefinitionCollection)
{
foreach (SPJobDefinition job in sPJobDefinitionCollection)
{
if (job.Name.Equals(CustomerTimerJob.CUSTOMERPFOILELIST_TIMERJOB_NAME, StringComparison.OrdinalIgnoreCase))
{
job.Delete();
}
}
}
}
}
---------------------------------------------------------------------------------------------
No comments:
Post a Comment