Tuesday, 29 April 2014

How to send alert (Reminder Email) in SharePoint Workflow - Visual Studio.

Blog approach - In many projects  it's a requirement to send an email on daily basis or hourly.
Using Delay Activity  we can send an alert.

To achieve above requirement we need to use listen Activity , delay Activity and code activity.

Listen Activity : The listen activity will wait for any of the branch to wake up and whichever wakes up first - completes the activity. The Listen Activity activity cannot be used in state machine workflows.

Delay Activity : Here we have to set time span / Initialize time out duration .

Code Activity : Once in time of delay activity is up , code activity will be execute where we will write send mail code.


Put listen activity with two branches under while loop .

One branch will be use for execute the code on  "OnTaskChanges"  and another branch will be use for execute thye code on Dealy Activity ( which will use for send an email ) .

Here is a code activity (codeActivitySendReminder)  which will execute the code for send an alert if the time in dealy activity is up.

Here is a delay Activity handler code :

private void delayActivity_InitializeTimeoutDuration(object sender, EventArgs e)
{
   DelayActivity dlyAct = sender as DelayActivity;
   dlyAct.TimeoutDuration = new TimeSpan(<Number Of Days Here>, 0, 0, 0);
}

Now let's say we want to send a reminder every 2 days  if the task has not completed,  then the above line would look like

dlyAct.TimeoutDuration = new TimeSpan(2, 0, 0, 0);


Some explanation:

If you set <Number Of Days> as 1 (i.e. 1 Day),So It will consider as 24 Hr means time out duration will up after 24 Hr and then fire the code within Code Activity i.e. CodeActivitySendReminder.

Consider an example- If Time out duration of delay activity is initialized at 3:00 PM on dated 1/1/2014. So Time of delay activity will up at 3:00 PM of 2/1/2014 and hence code activity (CodeActivitySendReminder)  fires up.

If You want to send Reminder mail at particular time , suppose at 3:00 AM.

Calculate the value for 3:00 AM as per 24 Hr. Time and it will be 27.

private void delayActivity_InitializeTimeoutDuration(object sender, EventArgs e)        {
   int TaskGenerateTime = DateTime.Now.TimeOfDay.Hours;
   int AlertFiredTime = 27 - TaskGenerateTime;
   DelayActivity dAct = sender as DelayActivity;
   dAct.TimeoutDuration = new TimeSpan(AlertFiredTime, 0, 0);
}

private void CodeActivitySendReminder(object sender, EventArgs e)
{
    //TODO: code for send mail.

}

Going further more: 

The above code can be replaced with the days based on actual Due date of the task in CreateTask activity.
 Example:  
TimeSpan ts = TaskObject.DueDate -  DateTime.Today ; // (Calculate ts before the While loop above)
and hence use 

ts.Days above i.e.
dlyAct.TimeoutDuration = new TimeSpan(ts.Days, 0, 0, 0); 

Sunday, 27 April 2014

How to add user control to Sharepoint master page.

How to add user control to Sharepoint master page.

we can add user control using two ways
a] Register user control into master page
b] using module

Consider-
Project Name: MasterPageControl
master page:  v4.master
User control: RestrictPasteUrlb.ascx

Firstly add user control in your project.
   right click on project -> Add new item->select User Control
    (write your code in that)

using a first way i.e. Register user control into master page

1] Register user control in master page.

<%@ Register  TagPrefix="myControl"  TagName="CurrentPageControl"
Src="~/_controltemplates/MasterPageControl/ RestrictPasteUrlb.ascx"  %>

Src : is a path where user control is placed. i.e  in Template folder -> controltemplates  -> Folder of project name in our case (MasterPageControl)

2] Then you can add control to master page.

<mycontrol:CurrentPageControl Id="myControl" runat="server"/>

3] Delopy

using second way i.e Using Module.

1] Add module into project
right click on project -> Add new item->select Module.

2]Open Element.xml
(Here we will register our user control using module)

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Module">
    <File Path="Module\Sample.txt" Url="Module/Sample.txt" />
  </Module>
      <Control ControlSrc="~/_controltemplates/ MasterPageControl /RestrictPasteUrl.ascx" Id="AdditionalPageHead" />
</Elements>

ControlSrc : It's a path where user control is placed. i.e  in Template folder -> controltemplates  -> Folder of  project name in our case (MasterPageControl)
Id : It's a control id of sharepoint delegate control where we are going to add our user control  on master page (v4.master).

3] Delopy

I would preferred second way (using module ).