Friday, December 4, 2009

Collecting Data from web parts and submitting to back end using C#

There can be scenarios, wherein you may have to collect data from various web parts in a page and submit to back end. One solution will be to use Connected Web parts feature. Or the sample code below also will help to achieve this;


1) Define one base class
public abstract class IBaseForm : WebPart
    {

    }



2) Implement this class in all the web parts whose data need to be collected
public class InputForm2 : IBaseForm
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            TextBox t1 = new TextBox();
            t1.ID = "LastName";
            t1.Text = "Last Name";
            this.Controls.Add(t1);
        }
    }



3) This is the piece of code which will collect the data from all the web parts which implement the base class
public class SubmitButton : WebPart
    {
        TextBox t1 = new TextBox();
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Button b1 = new Button();
            b1.ID = "submitData";
            b1.Text = "Submit Data";
            b1.Click += new EventHandler(b1_Click);
            this.Controls.Add(b1);

            t1.ID = "result";
            this.Controls.Add(t1);
        }

        void b1_Click(object sender, EventArgs e)
        {
            t1.Text = string.Empty;
            System.Web.UI.ControlCollection colls = this.Parent.Controls;
            IEnumerable bf= colls.OfType();
            foreach (IBaseForm f in bf)
            {
                t1.Text = t1.Text + GetValue(f);
            }
        }

        string GetValue(IBaseForm f)
        {
            string res = string.Empty;
            TextBox t = default(TextBox);

            foreach (System.Web.UI.Control c in f.Controls)
            {
                if (c.GetType().Equals(typeof(TextBox)))
                    t = c as TextBox;
                break;
            }
            res = t.Text;
            return res;
        }
    }

Wednesday, December 2, 2009

To disable "Use command line option '/keyfile' or appropriate project settings instead of 'AssemblyKeyFile'"

This is what will help to avoid this warning;


// disable warning about using /keyfile instead of AssemblyKeyFile
#pragma warning disable 1699
[assembly: AssemblyKeyFile(@"..\AAS.Portal.snk")]
#pragma warning restore 1699

Friday, November 20, 2009

GroupBy and SUM using LINQ in C#

public class Test : IEnumerable<Test>
    {
        public string ColName
        {
            get;
            set;
        }

        public int ColVal
        {
            get;
            set;
        }

        #region IEnumerable<Test> Members

        public IEnumerator<Test> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }





Code to execute:


List<Test> tests = new List<Test>();

            Test test = new Test();
            test.ColName = "A";
            test.ColVal = 123;
            tests.Add(test);

            test = new Test();
            test.ColName = "A";
            test.ColVal = 10;
            tests.Add(test);

            test = new Test();
            test.ColName = "B";
            test.ColVal = 231;
            tests.Add(test);

            var query1 = from t in tests
                         group t by t.ColName into gr
                         select new
                         {
                             Id = gr.Key,
                             Sum = gr.Sum(r => r.ColVal)
                         };
            foreach (var grp in query1)
            {
                Console.WriteLine("{0}\t{1}", grp.Id, grp.Sum);
            }

Thursday, November 19, 2009

Configuring SSL for WCF services

Please follow these steps for configuring the SSL for WCF services


1.     Create and Install the Certificate on the server for Transport Security
2.     Install the Certificate on the client for Client Authentication
3.     Configure the wsHttpBinding with Certificate Authentication and Transport Security
<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpEndpointBinding">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          security>
        binding>
      wsHttpBinding>
    bindings>
    <client/>
    <services>
      <service behaviorConfiguration="ServiceBehavior"
      name="MyService">
        <endpoint binding="wsHttpBinding"
        bindingConfiguration="wsHttpEndpointBinding"
        name="wsHttpEndpoint" contract="IService" />
        <endpoint address="mex" binding="mexHttpBinding"
        contract="IMetadataExchange"/>
      service>
    services>
      system.serviceModel>
4.     Configure the mex Endpoint to Use wsHttpbinding with Certificate Authentication Configuration
<endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
 name="mexEndpoint" contract="IMetadataExchange" />
5.     Configure the Virtual Directory to Use SSL and require Client Certificates
a.     Open the IIS
b.    Right the website where the WCF service is hosted
c.     Select the Directory Security tab
d.    Click “Edit Secure Communications”
e.     Click “Require Secure Channel (SSL)” and click “Require Client Certificates”
Now we can open the hosted WCF service over SSL channel.

For client applications to be able to consume this service, its application configuration file should like as shown below;
<configuration>
  <system.serviceModel>
    <client>
      <endpoint
      behaviorConfiguration="ClientCertificateBehavior"
      binding="wsHttpBinding"
      bindingConfiguration="Binding1"
      contract="IMetadataExchange"
      name="https" />
    client>
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    <bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCertificateBehavior">
          <clientCredentials>
            <clientCertificate findValue="CN=clienttempcert"
            storeLocation="CurrentUser"
            storeName="My"
            x509FindType="FindBySubjectDistinguishedName" />
          clientCredentials>
        behavior>
      endpointBehaviors>
    behaviors>
  system.serviceModel>
configuration>

Tuesday, November 3, 2009

Business Action Model - Design Pattern

Recently, I had designed an Orchestration functionality using the "Business Action Model" design model. Thought of sharing this. Find below the UML class diagram for the same;