Sunday, February 28, 2010

Changing Default ICON in Custom Activity Designer in WF 4.0

In WF 4.0 when we create Custom Activity Designer it always shows the following Default ICON . In this post i am going to show how to  change the Default ICON to custom one.  Here i am using  the custom activity the  GetAllFiles  which i created in my pervious blog.[http://mstecharchitect.blogspot.com/2010/02/wf-40-custom-activity-designer.html]

image

Here is the XAML code which changes default ICON to Custom ICON.

image

WF 4.0 Activity Designer provides the following Controls

image

image

After doing all the above work now just compile the project you will see the Custom Activity ICON changed as Shown below.

image

Nandri(Thanks)

SreenivasaRagavan,

Saturday, February 27, 2010

WF 4.0 Custom Activity Designer

In this blog post i am going to build Custom Activity Designer for the Custom Activity which I created in my previous blog post.  [http://mstecharchitect.blogspot.com/2010/02/wf-40-custom-code-activity-vsnet-2010.html]. 

Activity Designer:

The WF 4.0 Activity Designer is based on WPF ( Windows Presentation Foundation) which means we have all the power of WPFs which is Rich UI , Styling, Data binding,Triggers etc., for our Custom Activity. In addition to this WF also provides some user controls that you can use in your Activity Designer to simplify the task of displaying an individual child activity, or a collection of activities.

The four primary controls are:

  • ActivityDesigner – root WPF control used in activity designers
  • WorkflowItemPresenter – used to display a single Activity
  • WorkflowItemsPresenter – used to display a collection of child Activities
  • ExpressionTextBox – used to enable in place editing of expressions such as arguments.
  • Now we are going to add new Item  template called [Activity Designer] to our Custom Activity project  as shown below.

    image

    After adding Activity Designer template we need to Create User Interface in XAML .

    image

    Here i am using ActivityDesigner item template to create a UI for custom activity because i have only one Activity in my project. Suppose if you have more number of custom activities you can use WF4 ActivityDesignerLibrary project template.  Once we have added the Designer ,now we are ready to  customize the look and feel of out custom activity by laying out how to display data and visual representations of child activities.  Within the designer you have access to a ModelItem that is an abstraction over the actual activity, and surfacing all properties of the activity. 

    Once we build the UI now we need to attach designer to our Custom Activity class as shown below. 

    image

    Now add Custom Activity project as a Reference in Main [hosting] Workflow project & Compile the Custom Activity project. Now this custom activity will appear in Toolbox. now Drag and drop and set the properties for the custom activity. 

    image

    Since this custom activity returns the List of File names from the given input Directory to store the output result we need to create Workflow variable then to loop through the List of File names  add ForEach<String> activity and print the file name using WriteLine Activity. 

    image

    image

    image 

    Now Hit F5 and Run the Main Work Flow project. you will see the Files listed form the given directory as shown below.

    image

    For More Info Please Refer the following URI: http://msdn.microsoft.com/en-us/library/ee342461.aspx.

    Nandri(Thanks)

    SreenivasaRagavan.

    WF 4.0 Custom Code Activity using CodeActivity<T>

    In this blog post we are going to use CodeActivity<T> Generic class to create custom code activity. Instead of defining a workflow OutArgument to get output from workflow  we are going  to change Execute method return type as shown below. [ To Follow this post please Read http://mstecharchitect.blogspot.com/2010/02/wf-40-custom-code-activity-vsnet-2010.html first.]

    image

    When we get output from workflow we will use  [Result] is the key as shown below.

    image

    Nandri(Thanks)

    SreenivasaRagavan.

    WF 4.0 Custom Code Activity [VS.NET 2010 RC].

    Windows Workflow Foundation 4.0 is completely re-write of the WF 3.0 which is initially Introduced. Activities are building blocks of Workflow. In this post we are going to see how to create custom activity in WF 4.0 with VS.NET 2010.

    All Custom Code Activities are derived from System.Activities.CodeActivity or System.Activities.CodeActivity <T>.  This type of activities should not perform long running process and there is no way cancel.

    Now we are going to write simple custom Code activity without using Workflow project Template.

    image

    image

    Now we are going to create a custom activity called GetAllFile. This activity basically takes Directory as input and returns List of String as Files from it. 

    First we need to drive a class from CodeActivity and override the Execute method as shown below. Here we are going to use InArgument for passing input to the workflow and OutArgument to get output from the workflow.

    image

    Now we need to invoke the above activity .  Here we are going to use WorkflowInvoker class. The advantage of using WorkflowInvoker is easy to call or invoke workflow and its easy to write unit test.

    [Note: Most of the workflow hosting environments uses WorkflowApplication class]. 

    image

    Result or Output:

    image

    Nandri(Thanks)

    SreenivasaRagavan.

    Monday, February 15, 2010

    Windows Phone 7 Series

    Today at Mobile World Congress 2010, Microsoft CEO Steve Ballmer introduced the next generation of Windows Phones, Windows Phone 7 Series.

    MS Says Windows Phones are designed for life in motion, so you don’t miss a moment.

    image

    For more visit  http://www.microsoft.com/presspass/presskits/windowsphone/default.aspx

    Nandri(Thanks)

    Sreenivasaragavan

    Saturday, February 13, 2010

    VS.NET IDE TODO Comments [Helps to Centralize the Task Lists]

    I am not sure how many of you knew already about this nice feature. But i came across today only after Installing  VS.NET 2010 RC. Its also there in all pervious version of VS.NET IDE ( 2003, 2005, 2008). I am too late to know about this. :)

    image

    Suppose when we are working in a project we may have many tasks to remember while coding , we may use PAD or some other media to keep track of these tasks. But this tasks list are separated from our IDE. Using VS.NET TODO Comments we can track them in VS.NET IDE itself. This Comments help you to get more organized about your projects to-do items in a central place.  

    let me show you how we can use TODO comments in our C# programs.  Here i am putting TODO comments with some notes. like that in a big project we may have lot of TODO list.

    image

    Look at the above code snippet we have some flexibility on  TODO comments  we can write  Todo or todo .

    To open or to List the the TODO list, go to View menu -> Task List as shown below.

    image 

    image

    Even we can use  HACK and UNDONE Comments as well for the same.

    for more information  visit http://msdn.microsoft.com/en-us/library/zce12xx2.aspx

    User Tasks

    image

    User tasks this allows us to add Task directly to the task list.

    Nandri(Thanks)

    SreenivasaRagavan.

    Friday, February 12, 2010

    How to add Custom Event Handler for UDTs (User Defined Types)

    Today one of my friend asked me how to add custom events to our custom class.With simple example i explained to him then  I thought i can blog his question and Answer. Here is the example i used

    Suppose let say we have UDT Called  Contact with the following properties Name, Phone , Email, and Address. suppose we want to notified if some one changes Contact Phone number property .

    First let us define UDT as C# Class.

    public class Contact
    {
    private string phone = string.Empty;
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }

    // Define the event for Phone number change.
    public event EventHandler PhoneChanged;
    public string Phone
    {
    get
    {
    return phone;
    }
    set
    {
    this.phone = value;
    if (this.PhoneChanged != null) //Check to see event is not null
    this.PhoneChanged(this , new EventArgs());
    }
    }


    }



    Now let see how we can invoke this event from code.



    namespace ConsoleApplication6
    {
    class Program
    {
    static void Main(string[] args)
    {
    Contact c = new Contact();
    c.PhoneChanged += new EventHandler(c_PhoneChanged);
    c.FirstName = "Sreeni";
    c.LastName = "Ramadurai";
    c.Address= "123 Main Street";
    c.Phone = "214-499-2837"; // Here i am setting (changing) the phone number.
    c.PhoneChanged+=new EventHandler(c_PhoneChanged);
    }

    static void c_PhoneChanged(object sender, EventArgs e)
    {
    Console.WriteLine("PhoneChanged");

    Contact c = (Contact)sender; //we need to cast here
    Console.WriteLine(c.Phone ); // Display the phone number.
    }
    }




         image  



    Nandri(Thanks)



    SreenivasaRagavan.

    The MEF [In Silverlight Application] Part-III

    MEF –Managed Extensibility Framework also Part of  Silverlight. If you happened to land directly  In this blog post i would suggest to read the part I and Part II of this series.

    Here is the Links for Part 1 & Part II

    http://mstecharchitect.blogspot.com/2010/02/mef-managed-extensibility-framework.html –PART I

    http://mstecharchitect.blogspot.com/2010/02/mef-managed-extensibility-framework_08.html –Part II

    In this blog post i am going to create simple Silverlight Application then using MEF i am going to provide ToolTip for Textbox  UI element.

    ToolTips are quite useful for for displaying helpful information when the user hovers over a control. Using MEF we can  Load our ToolTips dynamically and more over useful and nicer looking.

    image

    After creating new SL Application first you need Add the following references to the project. you can find these Assembly in Silverlight SDKS folder.

    image 

    Here is my  XAML Code and UI Look and Feel.

    <UserControl x:Class="MEFToolTip.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <
    Grid x:Name="LayoutRoot" Background="White">
    <
    Border BorderBrush="Green" BorderThickness="5">
    <
    StackPanel VerticalAlignment="Top" HorizontalAlignment="Center" Width="350">
    <
    TextBlock Text="Tool Tip Using MEF" FontSize="30"></TextBlock>
    <
    StackPanel Margin="10" Width="300">
    <
    TextBox Width="250" Height="50" Name="txtbox" BorderBrush="Brown" BorderThickness="5"></TextBox>
    </
    StackPanel>
    </
    StackPanel>
    </
    Border>
    </
    Grid>
    </
    UserControl>




    image



    Now i am going to add another Silverlight project (User Control) where i will designed my Tooltip and using MEF i will plug into the above TextBox.



    Export



    namespace TooTipPlugin
    {
    [Export(typeof(UserControl))]
    public partial class SLToolTip :
    UserControl
    {
    public SLToolTip()
    {
    InitializeComponent();
    }
    }
    }



    Import and Compose in Main Application




    namespace MEFToolTip
    {
    public partial class MainPage :
    UserControl
    {
    [ImportMany(typeof(UserControl),AllowRecomposition=true)]
    public IEnumerable<UserControl> toolTip { get; set; }
    public MainPage()
    {
    InitializeComponent();
    // PartInitializer.SatisfyImports(this);
    var catalog = new PackageCatalog();
    catalog.AddPackage(Package.Current);
    Package.DownloadPackageAsync(new Uri("TooTipPlugin.xap", UriKind.Relative), (s, p) => catalog.AddPackage(p));
    var container = new CompositionContainer(catalog);
    container.ComposeParts(this);
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {

    foreach (var item in toolTip)
    {

    ToolTipService.SetToolTip(txtbox, item );
    }
    }
    }
    }




    image



    Next blog post I will show how to use Metadata so that we can apply a Tooltip from List of available Imports. 



    Nandri(Thanks)



    SreenivasaRagavan

    Tuesday, February 9, 2010

    NOW VS.NET 2010 & .NET 4.0 RC Available for Download.

    The Visual Studio 2010 and .NET Framework 4 Release Candidate (RC) is available to MSDN subscribers on Monday, February 8th, with general availability on February 10th.

    image image

    image

    Nandri(Thanks)

    SreenivasaRagavan.

    Monday, February 8, 2010

    The MEF (Managed Extensibility Framework) PART-II

    In this Part-II we are going to separate our plug-ins from our main application and we are going to use Directory and Aggregate Catalogs to compose our parts.

    Here is the project solution structure  which has  three projects.

    1) Contract

    2) Text Format Plug-in as separate Assembly.

    3) Main Application with HTML Format Plug-in.

    image

    Now i am going to create new folder name called Plug-in in our Main Application project. Inside this folder we are going to copy the Text Format Plug-in project output Assembly. Now our Compose able parts are in two different places( Folder, Executing Application). Now we need to Aggregate them. To do we need to re-write the Blog part 1 ComposeParts method .

    public void ComposeParts()
    {
    AggregateCatalog mergeParts = new AggregateCatalog();

    // Get parts from Same Assembly.
    var asmcatalog =new AssemblyCatalog((Assembly.GetExecutingAssembly()));

    //Get Parts from Directroy
    var dirCatalog = new DirectoryCatalog(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Plugin", "*.dll");

    mergeParts.Catalogs.Add(asmcatalog);
    mergeParts.Catalogs.Add(dirCatalog);
    var container = new CompositionContainer(mergeParts );
    container.ComposeParts(this);

    }




    Assembly Catalog shows HTMLBasedFormat  Part=1 


    image



    Directory Catalog shows Parts=1



    image



    Now the Aggregate Catalogs shows Parts=2.



    image



    Next post we will see how we can use MEF in SilverLight Application.



    Nandri(Thanks)



    SreenivasaRagavan.

    Sunday, February 7, 2010

    The MEF [Managed Extensibility Framework] Series Part-1

    Yesterday afternoon [Feb-6th-2010] I spent some time  chatting to Glenn Block, Program Manager of MEF. After that i thought let’s blog on this MEF here it is.

    What is MEF?

    Managed Extensibility Framework (MEF) is the new library framework from Microsoft to build  Plug-In or adding classes to the application at run-time. MEF is part of .NET 4.0 and its not application specific you can use it any .NET Application like  Windows Forms, WPF , SilverLight ,ASP.NET etc. MEF lives in side the following .NET assembly.

    image

    MEF is all about  Export , Import and ComposeNow let us explore the  MEF developing by Simple application.

    [Export]- Describes a capability that a part has to share in the composition

    [Import] – Describes a compositional dependency that a part has. This is a requirement that a part needs to have satisfied when it participates in composition

      ]image

    image

    The above Figure shows that the Main Application and Plug-In do not know each other existence and the Contract assembly  is required to integrate both the assemblies.  

    Here i am going to build small email client application initially it sends only Text  Format based emails, after that we are going to see how MEF can help to support other format like  HTML , Video &HTML etc.

    First let us defined  the Interface with one method called Send() as shown below.

    public interface IMailMessage
    {
    void Send(EmailMessage msg);
    }


    Export


    [Export(typeof(IMailMessage))]
    public class TextBasedFormat :
    IMailMessage
    {

    StringBuilder sbmsg = new StringBuilder();
    string msgTo = string.Empty;

    public void Send(EmailMessage msg)
    {
    sbmsg.AppendFormat("To :{0}", msg.To);
    sbmsg.AppendFormat("\nFrom:{0}", msg.From);
    sbmsg.AppendFormat("\nSubject:{0}", msg.Subject);
    sbmsg.AppendFormat("\nMessage Body :{0}", msg.Message);
    sbmsg.Append("\n\n");
    sbmsg.AppendFormat("\nThanks \n{0}", msg.From);

    msgTo = @"C:\Appdev\" + msg.To + ".txt";

    StreamWriter sw = new StreamWriter(msgTo);
    sw.WriteLine(sbmsg.ToString());
    sw.Flush();
    sw.Close();
    }
    }



    Import:



    [Import(typeof(IMailMessage))]
    public IMailMessage MailPlugIn { get; set; }


    Compose: (HOSTING)


    Here we have asked our application to find all the plug-in (classes with ‘Export’ attributes) from the currently executing assembly. because our plug-in [Export] resides in the same application. Suppose if we implement our Plug-In in separate assembly and the output assembly we can copy to some directory (example: myPlugIn)  in this case our Plug-in resides inside directory now we need to use DirectoryCatalog. 


    AggregateCatalog :Suppose if our Plug-Ins  are coming from two different source (Same Assembly,Directory ) then we need use AggregateCatalog to combine both the Assembly parts.


    Here is the object view for the Plug-in Hosting .


    image 


    public void ComposeParts()
    {
    var catalog =new AssemblyCatalog((Assembly.GetExecutingAssembly()));
    var container = new CompositionContainer(catalog );
    container.ComposeParts(this);

    }


    image



    My Email Application user Interface.



    image



    On Send Button Click i am calling Send() method



    private void Button_Click(object sender, RoutedEventArgs e)
    {
    EmailMessage msg = new EmailMessage ();
    msg.To = txtTo.Text;
    msg.From = txtFrom.Text;
    msg.Subject = txtSub.Text;
    msg.Message =txtMsg.Text ;
    MailPlugIn.Send(msg);
    MessageBox.Show("Message has been sent..");
    }


    Now let us build HTML based Plug-in.



    [Export(typeof(IMailMessage))]


    public class HTMLBasedFormat :
    IMailMessage

    {


        StringBuilder sbmsg = new StringBuilder();


        string msgTo = string.Empty;


        public void Send(EmailMessage msg)


        {


            sbmsg.Append("\n<html><head><title>Message</title></Head><body>");


            sbmsg.AppendFormat("<h2>To :{0}</h2>", msg.To);


            sbmsg.AppendFormat("\n<h2>From:{0}</h2>", msg.From);


            sbmsg.AppendFormat("\nSubject:{0}", msg.Subject);


            sbmsg.AppendFormat("\n<p>Message Body :{0}</p>", msg.Message);


            sbmsg.Append("\n\n");


            sbmsg.AppendFormat("\nThanks \n{0}", msg.From);


            sbmsg.Append("</Body></Html>");


            msgTo = @"C:\Appdev\" + msg.To + ".html";





            StreamWriter sw = new StreamWriter(msgTo);


            sw.WriteLine(sbmsg.ToString());


            sw.Flush();


            sw.Close();



    }




    Now we have two compose able parts to import inside our application, so now we need to use [ImportMany] attribute.  that is from single part to collection of parts.



    [ImportMany(typeof(IMailMessage))]
    public IEnumerable<IMailMessage> MailPlugIn { get; set; }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
    EmailMessage msg = new EmailMessage ();
    msg.To = txtTo.Text;
    msg.From = txtFrom.Text;
    msg.Subject = txtSub.Text;
    msg.Message =txtMsg.Text ;
    // Enumerate all the pulgin
    foreach (var item in MailPlugIn )
    {
    item.Send(msg);
    }

    MessageBox.Show("Message has been sent..");
    }


    TESTING:


    image 


    Both Message format files are created 


    image 



    [Note: Here I am sending email to windows folder to mimic ]



    Next blog post we will see how to use Directory and aggregate Catalogs.



    Nandri(Thanks)



    Sreenivasaragavan.

    Monday, February 1, 2010

    Customizing SharePoint 2010 List form Using InfoPath 2010

    In this blog we are going to see how to customize SharePoint 2010 List form. In have SharePoint site with Custom list called  SreeniCustomer. Here is the default Input form view shows when user click Add New Item  SreeniCustomer custom list .

      image

    Now we are going to customize this above form using InfoPath 2010. 

    image

    1) First click the SreeniCustomer custom list.

    2) Select the List TAB from the Ribbon Menu.

    3) Click Customize form. this action causes SreeniCustomer List form opens in InfoPath 2010 and shows like this

    image

    Now we are going to customize the above form Look and Feel.  

    image

     

    Above screen numbering shows the steps to add validation rule to check user input is valid  Email Address or not if not display the custom error message. Once we are done with form customization now ready to deploy into SharePoint server 2010.  To publish the above customized SharePoint list form to SharePoint server just click the File and click Publish your form.

    image

    Now our customized SharePoint List form published successfully into SharePoint 2010 Server. Now let’s go to SharePoint site and we add some new customer using the new SharePoint List form.

    image

    The Following Screen Shot shows the Customized form Inside SharePoint  and also it shows  validation error when user input invalid email address . This because  we have added validation rule for the email Address filed inside InfoPath .

    image

    Nandri(Thanks)

    SreenivasaRagavan.