Commands

Some points to be kept in mind while working with Timer Job:

I have been working with timer job since a long time and I have listed some points that one needs to keep in mind while working on it.

The following are some of the points that we need to keep in mind:

  • In order to consume a wcf service in timer job, we need to add an endpoint in the owstimer.exe.config file of the web application.
  • owstimer.exe.config is the config file for timer jobs. It is very similar to the web.config for the web applications.
  • One can declare the global variables for multiple timer jobs over here like one does in the web.config file.
  • The owstimer.exe.config is located at the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN location.
  • It is advisable to save a backup of this file before making any changes to it so that if one messes up the file we can recover it from the backup.
  • The following is the code to be added in the file:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />
<bindings>
<basicHttpBinding>
<binding name=”BasicHttpBinding_Iapi” closeTimeout=”00:01:00″ openTimeout=”00:01:00″ receiveTimeout=”00:10:00″ sendTimeout=”00:01:00″ allowCookies=”false” bypassProxyOnLocal=”false” hostNameComparisonMode=”StrongWildcard” maxBufferSize=”2147483647″ maxBufferPoolSize=”2147483647″ maxReceivedMessageSize=”2147483647″ messageEncoding=”Text” textEncoding=”utf-8″ transferMode=”Buffered” useDefaultWebProxy=”true”>
<readerQuotas maxDepth=”32″ maxStringContentLength=”8192″ maxArrayLength=”16384″ maxBytesPerRead=”4096″ maxNameTableCharCount=”16384″ />
<security mode=”None”>
<transport clientCredentialType=”None” proxyCredentialType=”None” realm=”” />
<message clientCredentialType=”UserName” algorithmSuite=”Default” />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address=”url” binding=”basicHttpBinding” bindingConfiguration=”BasicHttpBinding_Iapi” contract=”ContractName.Iapi” name=”BasicHttpBinding_Iapi” />
<endpoint address=”url” binding=”basicHttpBinding” bindingConfiguration=”BasicHttpBinding_Iapi” contract=”WCFServiceReference.Iapi” name=”BasicHttpBinding_Iapi” />
</client>
</system.serviceModel>

  • Add the <system.serviceModel> tag after the <runtime>.
  • Here contract name is the name of the namespace that is obtained from the app.config file of the timer job.
  • In order to add multiple end points, add the tag as per requirement.
  • Always set the scope to Web Application.
  • Many a times during the deployment we encounter an error where we are told to force the deployment. For that we need to add the AlwaysForceInstall=”TRUE” in Manifest.xml file
  • The file looks something like the one shown below:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature xmlns=”http://schemas.microsoft.com/sharepoint/&#8221;
AlwaysForceInstall=”TRUE” >
</Feature>

 

 

Commands

Very Useful PowerShell Commands to get, add and remove event receiver associated to a specific list

To get the list of attached event receiver to the list

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> $spList = $spWeb.Lists[“List Display Name”]  $spList.EventReceivers | Select Name, Assembly, Type

 

To remove specific event receiver from the list

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> $spList = $spWeb.Lists[“List Display Name”] $eventsCount = $spList.EventReceivers.Count$assembly = “Company.SharePoint.Client.EventReceivers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=570c484d87a3aa61” $class = “Company.SharePoint.Client.EventReceivers.ProvisionServiceAreaSecurityGroups.ProvisionServiceAreaSecurityGroups” $name = “ProvisionServiceAreaSecurityGroupsItemUpdating” $type = 2 for ($i = 0; $i -lt $eventsCount; $i+=1){      if ($spList.EventReceivers[$i].Assembly -eq $assembly –and         $spList.EventReceivers[$i].Class -eq $class –and         $spList.EventReceivers[$i].Type -eq $type –and         $spList.EventReceivers[$i].Name -eq $Name)         {              $spList.EventReceivers[$i].Delete()         } } $spList.Update()

 

 

 

 

 

 

 

To remove all attached event receiver form the list

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> $spList = $spWeb.Lists[“List Display Name”]  $spList.EventReceivers | Select Name, Assembly, Type$eventsCount = $spList.EventReceivers.Countfor ($i = 0; $i -lt $eventsCount; $i+=1){                    $spList.EventReceivers[$i].Delete() }

 

 

 

To add the event receiver to the list

$spWeb = Get-SPWeb –Identity <SharePointWebUrl> $spList = $spWeb.Lists[“List Display Name”] $spEventReceiver = $spList.EventReceivers.Add() $spEventReceiver.Assembly = “Company.SharePoint.Client.EventReceivers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=570c484d87a3aa61” $spEventReceiver.Class = “Company.SharePoint.Client.EventReceivers.ProvisionServiceAreaSecurityGroups.ProvisionServiceAreaSecurityGroups” $spEventReceiver.Name = “ProvisionServiceAreaSecurityGroupsItemUpdating” $spEventReceiver.Type = 2 $spEventReceiver.SequenceNumber = 1000 $spEventReceiver.Synchronization =  [Microsoft.SharePoint.SPEventReceiverSynchronization] : : Synchronous$spEventReceiver.Update()

 

List of Event Receiver Types :

        InvalidReceiver         = -1,

        ItemAdding              = 1,

        ItemUpdating            = 2,

        ItemDeleting            = 3,

        ItemCheckingIn          = 4,

        ItemCheckingOut         = 5,

        ItemUncheckingOut       = 6,

        ItemAttachmentAdding    = 7,

        ItemAttachmentDeleting  = 8,

        ItemFileMoving          = 9,

        FieldAdding             = 101,

        FieldUpdating           = 102,

        FieldDeleting           = 103,

        ListAdding              = 104,

        ListDeleting            = 105,

        SiteDeleting            = 201,

        WebDeleting             = 202,

        WebMoving               = 203,

        WebAdding               = 204,

        WorkflowStarting        = 501,

        ItemAdded               = 10001,

        ItemUpdated             = 10002,

        ItemDeleted             = 10003,

        ItemCheckedIn           = 10004,

        ItemCheckedOut          = 10005,

        ItemUncheckedOut        = 10006,

        ItemAttachmentAdded     = 10007,

        ItemAttachmentDeleted   = 10008,

        ItemFileMoved           = 10009,

        ItemFileConverted       = 10010,

        FieldAdded              = 10101,

        FieldUpdated            = 10102,

        FieldDeleted            = 10103,

        ListAdded               = 10104,

        ListDeleted             = 10105,

        SiteDeleted             = 10201,

        WebDeleted              = 10202,

        WebMoved                = 10203,

        WebProvisioned          = 10204,

        WorkflowStarted         = 10501,

        WorkflowPostponed       = 10502,

        WorkflowCompleted       = 10503,

        EmailReceived           = 20000,

        ContextEvent            = 32766