Getting Deep into .net

November 23, 2009

How to upload file to SharePoint document library with Object Model

Filed under: .NET, How To, SharePoint — goldytech @ 10:01 am
Tags: ,

Introduction

So far so good, it has been wonderful journey of learning SharePoint. With every end of day I learn something new and wants to share that with my readers. So here I am again showing you one more aspect of SharePoint programming. In my earlier post I had written about, Programming SharePoint Lists, in which I showed all the CRUD operations that can be done on SharePoint List. Here I shall be talking about the document libraries. Well they are also one type of the list , but the vast difference between them is that the Lists stored the structured data, well as Document Libraries are responsible for storing the unstructured data like documents or any other binary data. Their features and events are also different from the standard lists. In this short post , I will be teaching you the following:

  • how you can upload the files in the document library programmatically
  • how to create a new folder in the document library

So let us take one by one.

How to upload the files in the document library programmatically

With rich object model of SharePoint , this can be accomplished with few lines of code.So let us assume that you have FileUpload control of the asp.net, where the end user will be selecting the file. I have kept this control in the user control.This user controls also exposes other properties like SiteCollection,Web and DocumentLibraryPath. All these three properties are of String type. Let us see now what happens when user clicks on the Upload Button

Protected Sub BtnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnUpload.Click

      If Not String.IsNullOrEmpty(_DocLibPath) Then
          If Not String.IsNullOrEmpty(Me.FileUpload1.FileName) Then

              Dim objSiteCollection As New SPSite(_SiteCollection)
              Dim objWeb As SPWeb
              objWeb = objSiteCollection.AllWebs(_Web) 
             

              If Not objWeb Is Nothing Then
                  Try 
                      objWeb.AllowUnsafeUpdates = True
                      objWeb.Files.Add(_DocLibPath & Me.FileUpload1.FileName, Me.FileUpload1.FileBytes) 
                       objWeb.AllowUnsafeUpdates = False
                  Catch ex As Exception
                  Finally
                      objSiteCollection.Dispose()
                      objWeb.Dispose()
                  End Try
              End If
          Else
              Label1.Text = "Please provide the file name"
          End If

      Else
          Throw New Exception("Please define the property value of DocumentLibararyPath")
      End If

  End Sub

After initializing the SPSite and SPWeb objects, with the property values of the user control. We add new Item to FilesCollection property of SPWeb object. This method accepts the document name along with its full path and the second parameter is the array of bytes, which is the actual content of the document.Notice one thing here is that I am calling the Add method after I have set the AllowUnsafeUpdates property of the SPWeb object to True. This is very important because of Security issues,as your code is going to update the SharePoint objects without any pre authentication. SharePoint will throw the exception "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again." Once the file is uploaded I am setting back the property to false. So with just few lines of code, we have uploaded a new document to the SharePoint document library. Now let us jump to the next section.

How to create new folder in SharePoint document library

If Not String.IsNullOrEmpty(TxtFolder.Text) Then
                        Dim objFolders As SPFolderCollection = objWeb.Folders
                        Dim CheckIfFolderExists = From folder As SPFolder In objFolders _
                                                Where folder.Name.Equals(Me.TxtFolder.Text)
                        If CheckIfFolderExists.Count > 0 Then
                            objWeb.AllowUnsafeUpdates = True
                            objFolders.Add(_DocLibPath & TxtFolder.Text)
                            objWeb.AllowUnsafeUpdates = False
                        Else
                            Me.Label1.Text = "Folder Already Exists"
                        End If

End If

After checking whether folder with the same name already exists in the document library through Linq expression. I use SPFolderCollection object and initialize this object with the SPWeb.Folders property. Again using the Add method of the SPFolderCollection which accepts the full path of the document library along with the folder name. Again it is being done by setting the AllowUnsafeUpdates to true and false.

Closure

Document library has an important role in SharePoint. It is good to have the knowledge of it and how can you perform the tasks on these objects programmatically. Download the sample code from here. Please set all the user control properties in your aspx page before running the solution.

Cheers!

November 17, 2009

How to read csv file and import its data with Linq

Filed under: .NET, How To, Linq — goldytech @ 8:58 am
Tags: , ,

Introduction

I was recently tasked with the project of reading a CSV file and importing its data to SQL Server database. This may sound very common and I believe there are lot of traditional methods to achieve this.As always the time was a big constraint , I wanted some quick solution . I could not recommend SSIS packages as customer was using SQL Server Express Edition.

Scenario

You are given a CSV file with the "," as delimiter of the file. There are three fields in the file

  1. Customer Id
  2. Customer Name
  3. City

The first line of the file has the column headings, this line should not be imported in the table. You have the table called Customer in the database which has the same columns. You are required to import the csv file data into this table. Sounds pretty easy, you must be thinking of reading the file loop through its contents and add the each line record in the table using DML statements.C’mon guys I will show you a better and improved way of doing the same without any loops…, So your eyes are open wide, without loop how is that possible. Well Linq will help us to achieve this fete.

Solution

I knew that File class function ReadAllLines method returns array of string.And there is the catch whatever that implements IEnumerable can be queried with Linq. As I needed to store the csv data into the table. I had already added Linq to SQL class in my solution. So let us see some code.

Dim dataImport = From line As String In File.ReadAllLines("YourFileNamewithFullPath") _
                         Skip 1 _
                         Let CR = line.Split(",") _
                         Select New TblCustomer With {.CustomerId = CR(0), _
                                                 .CustomerName = CR(1), _
                                                 .City = CR(2)}

The above code reads the array of lines using Linq syntax and store each line in CR variable by splitting the line contents with delimiter using Let statement. This record is then stored in constructor of the TblCustomer, which is the Linq to SQL Class for Customer table in the database.Notice that I am using Skip clause for not to read the file header, which are actual the column names in the csv file.So now your dataImport variable contains all the data of the csv file excluding the header. Now this data needs to be imported into the Customer table in the database. Let us have look at the code for the same.

Ctx.TblCustomer.InsertAllOnSubmit(dataImport)

Ctx.SubmitChanges()

Here Ctx is the object variable of Linq Data Context class. The InsertAllOnSubmit method will do bulk insert of the data provided to it as a collection object in its parameter. So that’s it with just two lines of the code I have imported data into my table. Kudos to Linq

Closure

I always like to do the old things in newer way and this was the classic example of it. I hope you must be agreeing with my thoughts, use Technology to best of its advantage otherwise don’t use. Happy coding…

November 5, 2009

Winner once again at dotnetfunda.com

Filed under: Uncategorized — goldytech @ 7:16 am
Tags: ,

I have been again chosen the winner at www.dotnetfunda in the article section for the month of Oct 2009. Previously I was awarded for the month of August 2009 in the same section. I once again thanks the .net community for their appreciation and support. Please keep motivating me to write.

Thanks and Happy Learning to everyone.

October 30, 2009

Working with SharePoint collection objects

Filed under: .NET, SharePoint — goldytech @ 3:22 pm
Tags: , ,

 

Introduction

Continuing further from my last post of Programming SharePoint Lists , in this post I shall be talking about the SharePoint collection objects. Though there are lot of collection objects available with SharePoint object model. I shall be introducing to you on the collection objects which comes under the hierarchy of the SharePoint architecture. The below diagram represents the hierarchy SharePoint objects

SharePoint collection objects

The solution that I will be showing in this post , is a simple web page which has a text box to show the site url and two cascading dropdownlists for Webs and Lists. When the user selects the item from Web dropdown list the Lists dropdown gets populated with items corresponding to that web.There is also a GridView which displays the fields of the selected list. So let us jumpstart with the code. First let us get the webs list based on the site url.

using (SPSite site = new SPSite(this.TxtSiteCollection.Text))
            {

                this.CboWebs.DataTextField = "Title";
                this.CboWebs.DataValueField = "ID";
                this.CboWebs.DataSource = site.AllWebs;

                this.CboWebs.DataBind();
                CboWebs.Items.Insert(0, new ListItem("Select Web"));

            }

The AllWebs property of the SPSite object is of SPWebCollection Type. So now we have Webs(Top level site and subsites) based on SPSite. Now let us look into how to get the Lists that are there is the selected Web

using (SPSite site = new SPSite(this.TxtSiteCollection.Text))
               {
                   using (SPWeb web = site.OpenWeb(new Guid(this.CboWebs.SelectedValue)))
                   {
                       this.CboLists.DataTextField = "Title";
                       this.CboLists.DataValueField = "ID";
                       this.CboLists.DataSource = web.Lists;
                       this.CboLists.DataBind();
                       this.CboLists.Items.Insert(0, "Select List");

                   }
              }

The Lists property of the SPWeb object is of SPListCollection Type. It is always a good practice to open every SharePoint object with its GUID. In the above code I am opening the Web by using its GUID with the OpenWeb method, though there are other ways of opening the web by appending with the site url by using OpenWeb method overloading operators. So now we have filled the Lists dropdown also , let us now get the fields based on the selected List. Here I would like to draw the readers attention that by default SharePoint creates lot of additional hidden and read only fields for the support of its various features. So this will display all those unnecessary fields that are not relevant to the developer  but there is way out of it. I have used Linq to Objects and filter those fields by setting those properties to false. Have look at the code below

using (SPSite site = new SPSite(this.TxtSiteCollection.Text))
               {
                   using (SPWeb web = site.OpenWeb(new Guid(this.CboWebs.SelectedValue)))
                   {

                       SPList list;
                       list = web.Lists[new Guid(this.CboLists.SelectedValue)];

                       var FldsLst = (from SPField field in list.Fields
                                      where field.Hidden == false && field.ReadOnlyField == false
                                      select new { FieldName = field.Title, FieldType = field.Type.ToString() }).ToList();
                       this.GrdFields.DataSource = FldsLst;
                       this.GrdFields.DataBind();

                   }
               }


Closure

SharePoint is a great technology to work with, its rich object model allows to create great applications on this platform.Download the sample code of this post from here.

Tip: Always use Linq to query the SharePoint collection objects rather than looping in them. It will save those great CPU cycles.

October 29, 2009

WCF and JSON Fusion

Filed under: .NET, WCF — goldytech @ 7:01 am
Tags: , ,

Introduction

JavaScript Object Notation (JSON) a lightweight, text-based data exchange format. Data formatted according to the JSON standard can be easily parsed by JavaScript code thus making it an ideal data exchange format for Ajax-style Web applications.The .NET Framework 3.5 includes improvements to Windows Communication Foundation (WCF) to support JSON serialization. With these enhancements developers can easily use JSON serialization in your WCF services, and consume those services from your client-side code just as you have been able to do with ASMX services.In this post I will be doing a complete walkthrough of the example of how to consume WCF service through JavaScript code. WCF service method will return us the collection object. The same object we shall be accessing via client side code and update UI. I assume that the readers have the basic knowledge of WCF services.

WCF + JSON

Earlier it was not possible to represent the data in the object format returned by the server to the client. You would only get the vanilla xml. But with invent of JSON that is now become history. Any data returned by the server can be used as object in the same fashion how you created that object with the server side code. Let me talk now about the solution which is provided in this post. From the functional point of view , the app is very super simple it will display the employees names as per the progress of your keyboard strokes. Precisely speaking it is a progressive search. The opportunities are immense once you have the JSON object returned from the server then using JavaScript rich object model, you can do wonders on the client side giving it real Ajax touch. From technical aspects our Visual Studio 2008 with SP1 solution consists of 2 projects.

  1. WCF Service Library
  2. ASP .net Web Application

The WCF service library has Employee class which is marked with Data Contract attribute in WCF.This is our key object and we shall be working with it in our JavaScript code.

[DataContract]
   public class Employee
   {
       int empId;

       string empName;

       [DataMember]
       public int EmployeeID
       {
           get { return empId; }
           set { empId = value; }
       }

       [DataMember]
       public string EmployeeName
       {
           get { return empName; }
           set { empName = value; }
       }

       public Employee(int Employeeid, string EmployeeName)
       {
           empId = EmployeeID;
           empName = EmployeeName;
       }
       public Employee()
       {
       }
   }

The above code is no brainer , simple class with two simple properties. Let us now have a look at the operation contract method code.

[OperationContract]
        Employee[] GetEmployeeInfo(string value);

The operation contract method returns the array of employees. The actual implementation of this method is shown below.

public Employee[] GetEmployeeInfo(string value)
        {
            List<Employee> RtnEmployees = GetAllEmployees();
            List<Employee> FilterEmployees = new List<Employee>();
            FilterEmployees = RtnEmployees.Where(e => e.EmployeeName.ToLower().StartsWith(value.ToLower())).Select(e => e).ToList();
            return FilterEmployees.ToArray();
        }

The GetAllEmployees method returns me the list of all employees, in this solution I have hard coded the names but in real time scenarios this will be coming from the actual database and it would be good if you store the collection in cache , so it does not hit the database with every key stroke.

So that is the end of WCF Service Library project code , now let us come to ASP .net Web Application project code.First thing that needs to be done on this project is adding the reference of WCF Service Library project. Most of the developers have mind set that the Service Library project can be only set as a Service Reference. No folks you can also set the dll reference also. In this sample I have done the same. So now the next big question comes how do I reference to my service without the .svc file. Hold your horses. In fact it is pretty simple and straight forward. You need to add a text file in your project and rename the file extension from .txt to .svc and next you need to add the following code in the .svc file

<%@ ServiceHost Language ="C#" Service ="WcfServiceLibrary.Service1" Factory ="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

You need to change the above code for the Service attribute to correspond to your namespace and class names and obviously the language of your choice in the Language attribute. So that’s it you are done now. The only thing that is pending is to give the reference of the service which can be achieved by the ScriptManager tag. The code is shown below.

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/WCFService.svc" />
        </Services>
    </asp:ScriptManager>

So your aspx page knows that there is a service reference attached to it. Now we can call this service methods via JavaScript code.

<script language="JavaScript" type="text/JavaScript">
        function getFilterEmployees() {
            goldytech.WCFJsonFusion.IService1.GetEmployeeInfo(document.getElementById("TxtEmpName").value, onCallBack)
        }
        function onCallBack(RtnJSON) {
            if (RtnJSON.length > 0) {
                Result.innerHTML = "";
                for (i = 0; i < RtnJSON.length; i++) {
                    Result.innerHTML += "Employee Id " + RtnJSON[i].EmployeeID + " Employee Name " + RtnJSON[i].EmployeeName + "<br/>";

                }
            }
            else {
                Result.innerHTML = "No matching Employees found";
            }

        }
    </script>

The getFilterEmployees JavaScript function calls the service method GetEmployeeInfo , which is an operation contract and returns me the array of Employee class , which is the data contract. One important thing to note over here is the namespace (goldytech.WCFJSONFusion), this namespace was provided in the ServiceContract attribute of WCF. The onCallBack function , which accepts the parameter of return object from the server , I am able to access that object via its properties like EmployeeID and EmployeeName. So you have seen the dual power of WCF and JSON. I have got the complete object with all its properties instead of just xml. The object that is returned I can do whatever with it. In the above example I am looping through the array and displaying the results in the div tag.

Closure

The WCF and JSON duo can do wonders for you. It is the ultimate combo of two great technologies. Do let me know your comments for it. Download the sample code for this post from here.

October 22, 2009

Take a scoop of Silverlight Toolkit Pie Chart

Filed under: .NET, Silverlight 3.0 — goldytech @ 2:45 pm
Tags:

Introduction

Silverlight has come a long way from just being a media and graphics enhancements tool to full blown Line of Business Application platform.In this short post we will dive into the inner workings of Pie Chart that is available as a part of Silverlight Toolkit. Have a look at the image below which shows the output that we shall be working for.  PieChart

Control Templates and Styles

Silverlight is a great technology to work with. It gives so much leverage and freedom to developers that you can can completely override the default look of any control and give more sparkles to it by adding styles . In this post I shall be using the combination of both. Scott Guthrie has great post on his blog which discusses the control templates. I shall be targeting PieDataPoint, this class represents each slice in the pie chart. I shall be also using style palette which is part of System.Windows.Controls.DataVisualization.Toolkit assembly. In the style palette we will be setting the different backgrounds of each slice. Though this is completely optional if you don’t specify the style palette , silverlight Charting engine takes the complete control and renders the colors of each slice automatically.Further using Styles and Control Templates I shall be changing the Legends that are getting displayed on the right side of the chart. I shall also be overriding the default tooltip text in the control template of PieDataPoint.

Getting Started

This post assumes that you have following software’s installed on your computer.

Create new project of SilverLight Application as displayed in the figure below Silverlight Application new project Add the references of following assemblies

  1. System.Windows.Controls.DataVisualization.Toolkit
  2. System.Windows.Controls.Toolkit

Any chart requires data for its display. Here in this example we will generate the random figures (between 1000 and 50000) of each month (Jan to Dec).I have created MonthlyCollection class for it , the Total property of this class will be binded to Chart ItemsSource. The complete code listing of this class is given below.

Public Class MonthlyCollection

Private _Month As String

Public Property Month() As String

Get

Return _Month

End Get

Set(ByVal value As String)

_Month = value

End Set

End Property

Private _Amount As Double

Public Property Amount() As Double

Get

Return _Amount

End Get

Set(ByVal value As Double)

_Amount = value

End Set

End Property

Public Shared ReadOnly Property Total() As List(Of MonthlyCollection)

Get Dim objMonthlyCollection As New List(Of MonthlyCollection)

Dim Rnd As New Random

With objMonthlyCollection

.Add(New MonthlyCollection With {.Month = "Jan", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Feb", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Mar", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Apr", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "May", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Jun", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Jul", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Aug", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Sep", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Oct", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Nov", .Amount = Rnd.Next(1000, 50000)})

.Add(New MonthlyCollection With {.Month = "Dec", .Amount = Rnd.Next(1000, 50000)})

End With

Return objMonthlyCollection

End Get

End Property

End Class

Now as the data is in place , let us now look at the control templates and styles code. As we will be using the animation for each slice in a simultaneous mode. Let us first look at the PieDataPoint control template.

<ControlTemplate x:Key="PieDataPoint" TargetType="Chart:PieDataPoint">
                <Path
                        Data="{TemplateBinding Geometry}"
                        Fill="{TemplateBinding Background}"
                        Stroke="{TemplateBinding BorderBrush}"
                        RenderTransformOrigin="0.5,0.5">
                    <Path.RenderTransform>
                        <ScaleTransform x:Name="Scale" ScaleX="0" ScaleY="0"/>
                    </Path.RenderTransform>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="RevealStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.5"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Shown">
                                <Storyboard>
                                    <DoubleAnimation
                                            Storyboard.TargetName="Scale"
                                            Storyboard.TargetProperty="ScaleX"
                                            To="1"
                                            Duration="0"/>
                                    <DoubleAnimation
                                            Storyboard.TargetName="Scale"
                                            Storyboard.TargetProperty="ScaleY"
                                            To="1"
                                            Duration="0"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Hidden">
                                <Storyboard>
                                    <DoubleAnimation
                                            Storyboard.TargetName="Scale"
                                            Storyboard.TargetProperty="ScaleX"
                                            To="0"
                                            Duration="0"/>
                                    <DoubleAnimation
                                            Storyboard.TargetName="Scale"
                                            Storyboard.TargetProperty="ScaleY"
                                            To="0"
                                            Duration="0"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ToolTipService.ToolTip>
                        <StackPanel Orientation="Horizontal" >
                            <ContentControl Content="Collection for " />
                            <ContentControl Content="{TemplateBinding IndependentValue}" />
                            <ContentControl Content=" is " />
                            <ContentControl DataContext="{TemplateBinding DependentValue}" Content="{Binding  Converter= {StaticResource DataFormatter},ConverterParameter=\{0:C\}}"  /> 
                          </StackPanel>
                    </ToolTipService.ToolTip>
                </Path>
            </ControlTemplate>

One thing in the above code that is worth observing is the ToolTip, using the powerful feature of control templates how am I transforming the mere tooltip text into useful information. Let us now look at how can I customize the Legends of the chart rather than just text.

<Style x:Key="LegendStyle" TargetType="Chart:LegendItem">

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate  TargetType="Chart:LegendItem">
                            <Grid>
                                <Rectangle Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="1" />
                                <DataVisuals:Title Content="{TemplateBinding Content}" Margin="2" Foreground="White" />

                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>

</Style>

In the above code I am using TemplateBinding to bind the content and showing each legend in rectangle with the appropriate background. Finally let us look into the StylePallete , as discussed above this is an optional step but would be good to have if you want more control on the output. Have a look at the code below.

<DataVisuals:StylePalette x:Key="PieDataPointPalette">
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FFF4F4F4"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FF5A88CE"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FFCE5A5A"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FFFFFF72"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="Blue"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="Red"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="Green"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="Pink"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="Cyan"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FF555555"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FF313861"/>
    </Style>
    <Style TargetType="Chart:PieDataPoint">
        <Setter Property="Template" Value="{StaticResource PieDataPoint}"/>
        <Setter Property="Background" Value="#FF000000"/>
    </Style>

</DataVisuals:StylePalette>

In this code I am targeting the style for the control template, PieDataPoint and setting its background property of various colors.One thing important to note here is that as my data represents of monthly collection which involves twelve months of the calendar. I need to specify twelve style attributes accordingly.The user can interact with the chart by clicking on the each slice of the pie chart. Once he click he gets corresponding info of the slice clicked on the labels on the right top.We can achieve this functionality by trapping the SelectionChanged event of Chart:PieSeries

Private Sub PieSeries_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        If e.AddedItems.Count >= 1 Then
            Me.PnlInfo.DataContext = e.AddedItems(0)
        Else
            Me.PnlInfo.DataContext = Nothing
        End If
    End Sub

The above code assigns the first element of AddedItems Collection to the DataContext property of StackPanel.

Now let us look into the fetching of the new data, When user clicks on Refresh Data button the new data is binded to ItemsSource. Have a look at the code for the same.

Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        Dim objPieSeries As DataVisualization.Charting.PieSeries
        objPieSeries = CType(Me.PieChart.Series(0), DataVisualization.Charting.PieSeries)
        objPieSeries.ItemsSource = MonthlyCollection.Total 
End Sub


Closure

There is a well known saying that picture is worth thousand words. You can bring your data to life by providing visuals animations to it. It can be great help to create digital dashboards. I hope you must have enjoyed reading the post. Download the sample code from here. Cheers and Happy Charting !

October 12, 2009

Programming SharePoint Lists

Filed under: .NET, SharePoint — goldytech @ 3:11 pm
Tags: , ,

Introduction

My Blog has been quite for sometime now. I was busy in sharpening my skills on Microsoft Office SharePoint Server 2007, well known as MOSS 2007.This is awesome product from Microsoft stable.The all in one product which supports the following features out of the box.

  • Web Content Management
  • Enterprise Search
  • Portal Services
  • Business Intelligence
  • Custom Workflows

Another good thing is that it has very rich object model.Whatever you can do from its GUI , the same thing can be achieved via its api’s. Last but not least this platform is based on ASP.net Technology , so there wont be a steep learning curve for existing asp.net developers you just need to get acquainted to the new terms and its rich object model. Each and every object starts with SP as prefix. To know more about it , please visit MOSS Official Website In this post I shall be talking about SharePoint Lists. To keep things simple for new kids of SharePoint development, I will be speaking in context of terms which they are familiar with. So fasten your seat belts and enjoy the joy ride of SharePoint.

SharePoint Lists

A SharePoint List is more or less similar to a table in the database. How you have rows and columns in the table , a SharePoint lists also have the rows and columns. The whole record in the list is called ListItem. The SharePoint List provides more functionality than the database tables.To name few , you can create versioning of the list items, attach a custom workflow on the list and many more.They play a vital role in SharePoint architecture. To store any custom data for your custom SharePoint solutions , custom lists are your best bet.The user interface for creating, updating and deleting the list items comes as built in feature of SharePoint. I will show you how you can achieve the same through its rich object model.

SharePoint Object Model

To perform Add,Update and Delete operations on your custom list, you would require the following objects

Now let us see some code in action.

To keep things short and simple, the below example assumes that there is a custom list by the name "Customer" which has two columns

  1. Title (This column gets auto created whenever you create new custom list.It will also be our key column to retrieve the ListItem
  2. ContactPerson (This column holds the name of the Customer)
ADDING ITEM IN CUSTOM SHAREPOINT LIST

private static void AddNewItem()

{

string objUrl; // the url of the sitecollection

string objName; // the path of the site

string objList; // the name of the list

objUrl=”http://Yourservername:Yourportnumber/sites/YoursiteCollectionName/”;

objName = "/sites/YourSiteName/";

objList = "Customer"; //it is the name of list

SPSite objsiteCollection = new SPSite(objUrl);

SPWeb objSite = objsiteCollection.AllWebs[objName];

SPListItemCollection objListItems = objSite.Lists[objList].Items ;

SPListItem objListItem;

objListItem = objListItems.Add();

objListItem["Title"] = "Northwind Traders";

objListItem["ContactPerson"] = "Alfred Pinto";

objListItem.Update();

}

Let us do the dry run of the code. Initially all required variables are declared, please change the values of the variables that has prefix “Your” to suit your setup.

The first object is SPSite , which is the sitecollection and SPWeb is the site present in that site collection. SPListItemCollection is the object which contains all the list items present in the custom list. Finally SPListItem represent the individual ListItem present in the ListItemCollection object.

We use the Add method of ListItemCollection object to add new item. Please notice the syntax how we are referencing the columns name in the ListItem object to assign the values to the columns.Finally Update method of ListItem is called to update the ListItemCollection.

UPDATING ITEM IN CUSTOM SHAREPOINT LIST

private static void UpdateItem(object itemTobeUpdated)

{

string objUrl; // the url of the site

string objName; // the name of the site

string objList; // the name of the list

objUrl=”http://Yourservername:Yourportnumber/sites/YoursiteCollectionName/”;

objName = "/sites/YourSiteName/";

objList = "Customer";

SPSite objsiteCollection = new SPSite(objUrl);

SPWeb objSite = objsiteCollection.AllWebs[objName];

SPListItemCollection objListItems = objSite.Lists[objList].Items;

var lstitem=(from SPListItem spItem in objListItems where spItem

["Title"].ToString() == Convert.ToString(itemTobeUpdated )

select spItem ).FirstOrDefault();

SPListItem lstItm = (SPListItem)lstitem ;

lstitem["ContactPerson"] = "Albert Einsten";

lstitem.Update();

}

The first few lines of the above code remains the same. We retrieve the SPListItem object from the SPListItemCollection using Linq. SharePoint has its native query language to query the data from custom lists. It is called CAML (Collaborative Application Markup Language). There is also the class name called SPQuery in SharePoint object model.To keep things simple and straight forward I haven’t used it in this post. So after retrieving the SPListItem via Linq we assign the new value to its column in the similar syntax. Calling the Update method updates the SPListItem in the SPListItemCollection.

DELETING ITEM IN CUSTOM SHAREPOINT LIST

private static void DeleteItem(object itemTobeDeleted)

{

string objUrl; // the url of the site

string objName; // the name of the site

string objList; // the name of the list

objUrl=”http://Yourservername:Yourportnumber/sites/YoursiteCollectionName/”;

objName = "/sites/YourSiteName/";

objList = "Customer";

SPSite objsiteCollection = new SPSite(objUrl);

SPWeb objSite = objsiteCollection.AllWebs[objName];

SPListItemCollection objListItems = objSite.Lists[objList].Items;

int itemsCount = objListItems.Count;

//loop the list items collection to find the desired item

//if found then delete it and exit from loop

for (int i = 0; i < itemsCount ; i++)

{

SPListItem objListItem=objListItems[i];

if (objListItem["Title"].ToString()==Convert.ToString(itemTobeDeleted))

{

objListItems.Delete(i);

return;

}

itemsCount--;

}

In the above code we rotate in loop of SPListItemCollection until we find the matching SPListItem that we want to delete. Once the item is found we call Delete method of SPListItemCollection by passing the index value in the parameter.

Conclusion

This was your first baby step towards SharePoint programming.As discuss above , it has very rich api and sky is the limit. On a concluding note , all SharePoint objects must be destroyed explicitly by calling their Dispose method. In the above examples I have purposely not used it so that it can be short and precise. I hope you must have enjoyed reading this post. Let me know your comments.

September 15, 2009

Master Details CRUD Operations with ADO.net Entity Framework

Filed under: .NET — goldytech @ 6:52 am
Tags: , ,

INTRODUCTION

I got a overwhelming response for my post , CRUD Operations using Ado.net Entity Framework. Continuing further on this I wanted to show some advanced features of ado. net entity framework. In this post I discuss about how you can do CRUD Operations on master details tables. Perform some business validation rules using Entity Framework partial classes both at the field level as well as on the record level. Sounds interesting then continue read on. This post is the Part two of Entity Framework. I strongly recommend all the readers to read the Part one post before.

MASTER DETAILS CRUD OPERATIONS

I continue from where I stopped on my last post. We will be using the same Employee table. For this post I have added new table in the database called Languages. This tables contains the information about the languages that are known by the employee. It is a detail table to the master table , employee. The employee id is the reference key here.

WPFMasterDetailsForm

As shown in the figure above the UI is very simple. I have used WPF DataGrid , which is included in WPF Toolkit. This DataGrid control is very powerful. It has rich object model with many events. When you are in Add Mode you can press TAB key to insert a new row and when you are in Update Mode you can you use DELETE key to remove the row from the datagrid. Based on these actions it has also the corresponding events attached. When adding I use the following event

Private Sub DgLanguages_RowEditEnding(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs) Handles DgLanguages.RowEditEnding
        If e.EditAction = Microsoft.Windows.Controls.DataGridEditAction.Commit Then
            EmployeeLanguages.Add(CType(e.Row.DataContext, Languages)) 
       End If

End Sub

The above code uses RowEditEnding event of Wpf DataGrid , if you are committing the row that is pressing the TAB key I add new language in the Languages list by doing type casting of the Row datacontext. Similarly when the user press DELETE key I have used DataGrid Unloading Row event. Here I remove the the language from the language list.

Private Sub DgLanguages_UnloadingRow(ByVal sender As Object, ByVal e As Microsoft.Windows.Controls.DataGridRowEventArgs) Handles DgLanguages.UnloadingRow
        Dim Language As Languages
        Language = e.Row.DataContext
        If Not Language Is Nothing Then
            EmployeeLanguages.Remove(Language)
            Db.DeleteObject(Language)
        End If
    End Sub

So that was the additional code to my previous post. 

BUSINESS RULES VALIDATION WITH ENTITY FRAMEWORK

The coolest feature about the entity framework is that it can be extended through Partial classes. You can always add your custom code on the top of the default code generated by Visual Studio. If you would have worked with Entity Framework before you must be aware that Visual Studio generates .designer.vb or .designer.cs. This file can be seen under your .edmx file. If you have through look into this file you will observe that Visual Studio has generated one EntityContext class and the rest classes depends on the number of tables that you dragged and dropped on the designer surface of the edmx file. Each of this class can be extended as they are declared partial. And that is the catch , the rich object model of the entity framework exposes lot of events which can be handled by developer by creating the partial classes. If you want to use the field level validation then you can extend your Entity class. If you want the Record level validation then you can extend EntityContextClass. Let us take the example of each one by one. Suppose I want that the LastName field cannot be blank. I can use OnLastNameChanged event and check for the property variable. The code listing of the same is given below

Private Sub OnLastNameChanged()
        If String.IsNullOrEmpty(_LastName) Then
            MessageBox.Show("Last Name cannot be blank")

        End If

End Sub

If you wish to initialize some property values when the form gets loaded. You can use constructor method. The below example initializes the salary property variable to 3000

Public Sub New() 
   _Salary = 3000

End Sub

Now let us look at the record level validation. This validation can be applied on the Entities class. You need to attach the event handler for the SavingChanges Event. This is best place where you can implement all your business rules and ensure that only valid data goes into your database. Take a look at the code below.

Public Class EFDemoEntities
    Private Sub OnContextCreated()
        AddHandler Me.SavingChanges, AddressOf SaveRecord
    End Sub

    Private Sub SaveRecord(ByVal sender As Object, ByVal e As EventArgs)
        Dim ctx As ObjectContext
        ctx = CType(sender, ObjectContext)

        For Each objEntry As ObjectStateEntry In ctx.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added Or System.Data.EntityState.Modified)
            If TypeOf objEntry.Entity Is Employee Then
                Dim Emp As Employee
                Emp = CType(objEntry.Entity, Employee)
                If String.IsNullOrEmpty(Emp.FirstName) OrElse String.IsNullOrEmpty(Emp.LastName) Then
                    Throw New Exception("First Name or Last Name cannot be null")
                End If
                Dim DuplicateLanguages As New List(Of String)
                DuplicateLanguages = (Emp.Languages.GroupBy(Function(x) x.Language).Where(Function(y) y.Count > 1).Select(Function(z) z.Key)).ToList
                If DuplicateLanguages.Count > 0 Then
                    Throw New Exception("Duplicate Languages not allowed")
                End If
            End If
        Next
    End Sub
End Class

The above code make a final check of all the business rules. Besides checking for the null values for the FirstName and LastName properties. I also perform an additional check that Languages in the detail section cannot be duplicated. This check is performed by Linq using GroupBy method.

CLOSURE

Ado.net Entity Framework has lot to offer. It is very rich data access api. You need to learn more about it and use in all your applications. Hope you must have enjoy reading this post. Let me know your thoughts for it. Download the code from here.

Please rename the file extension from .doc to .rar. Also change the connectionstring in your app.config file.

September 7, 2009

Url rewriting with http module in asp.net

Filed under: .NET, Asp.net, How To — goldytech @ 6:19 am
Tags: ,

INTRODUCTION

I am highly impressed by Asp.net MVC framework. There are lot of exciting features into the framework but the one that excites me most is UrlRewriting module. Every url depends on the controller and its actions methods. The format of this url is something like this "{controllerclassname}/{actionmethodname}/{id}".  Moreover these pages doesn’t physically exists on the hard drive.  Cool very cool. This feature impressed me so much that I decided to implement the same into my one of the asp.net web forms project. In this post I introduce you to http modules and how you can unleash their power and do miracles in your solutions.

HTTP MODULES

HttpModules are extremely powerful feature of asp.net. They act like listeners to the request that your asp.net application receives. Most of built in functionality of asp.net like output cache, session management, authentication and authorization are developed on HttpModules. An http module is a class which implements IHttpModule .  This interface has two methods Init and Dispose. The Init method is where you have to write the code to implement your custom functionality. The below listing shows the list of events that are available in the Init method.

  • AcquireRequestState
  • AuthenticateRequestState
  • AuthorizeRequestState
  • BeginRequest
  • EndRequest
  • Disposed
  • Error
  • PreRequestHandlerExecute
  • PostRequestHandlerExecute
  • PreSendRequestContent
  • PreSendRequestHeaders

I won’t get into the details of each and every event and when they are triggered. You can read more about them here. This article focuses on the url rewriting and to do so we will be using the BeginRequest event. This event is raised when a new request is arrived. This is the best place to override the default functionality. Just creating the custom class doesn’t complete the work. You need to tell to the asp.net engine that I have a custom HttpModule. You can specify this in the web.config file under <httpModules> section. It should be written in the following format.

<add name ="YourmoduleName" type ="NamespaceName.ClassName,AssemblyName"/>

The next section in the post discusses my solution of url rewriting.

URL REWRITTING

This may not be a full fledge url rewriting solution. But this will help you to get started. Now let us talk about the solution I have two pages in my project

A Default page that displays the product categories and their description in the gridview. The category column is of type  HyperlinkField , clicking on this link will navigate to the Products page showing the relevant products that belongs to the category. A traditional solution would be of passing the querystring to the Products page and then displaying the records. But let us think how the asp.net mvc team thought, how about redirecting to the page which more user friendly something like “yourappname/Categories/Beverages”. Isn’t this more seo friendly and much pleasant than “yourappname/Products.aspx?Cn=Beverages”. So how do we do this ,well HttpModules can do it for you. Look at the sample code below.

public class myhttpModule :IHttpModule
    {
        private HttpApplication httpApp = null;
        #region IHttpModule Members

        void IHttpModule.Dispose()
        {
            throw new NotImplementedException();
        }

        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            httpApp = context;
        }

        /// <summary>
        /// This is the first event that triggers in request pipeline to //IIS. Best place to manipulate the output that is send to client
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void context_BeginRequest(object sender, EventArgs e)
        {
            char[] seprator= {'/'};
            if (httpApp.Context.Request.Path.Contains("Categories")) //if the request url path consists of categories word then only we want to redirect otherwise normal processing should be done
            {
                string categoryName = httpApp.Context.Request.Path.Split(seprator, StringSplitOptions.None)[2]; //getting the required element from the array
                //redirecting to actual page with query string this will be sleath forwarding user wont notice query string in his browser address bar
                httpApp.Context.RewritePath("~/Products.aspx?cn=" + categoryName);
            }
        }

        #endregion
    }

As mentioned I use BeginRequest event and look for current requested page. Do some string manipulations and then using RewritePath method of the httpApplicaton context , I redirect to the actual page. But wait a minute will user be seeing this path or user friendly path in the address bar of the browser. The rewriter is smart enough it does a sleath forwarding so the user will never come to know what is the actual path the server has redirected to him.

CLOSURE

HttpModules can do amazing things for you. Download the sample code from here. You will be requiring Northwind sample database to run the solution. Please rename the file extension from .doc to .rar

September 3, 2009

Extending Asp.net Calendar

Filed under: .NET, Asp.net, How To — goldytech @ 6:17 am
Tags: ,

INTRODUCTION

Everyone of you is familiar with asp.net calendar control. I am sure you must have used in most of your projects. Working on my recent SharePoint project , I was required to develop a webpart which is the calendar that displays all the events from the table in the corresponding cell of the calendar control. Though there are many third party controls are available to do this job by setting the few properties here and there. But in this case my client had a limited budget (Indeed it is the fact the enterprises are shrinking their IT budgets), so he told me to use something that is readily available with some programming efforts. As the famous saying goes “THE CLIENT IS ALWAYS RIGHT” , I have to do whatever he had proposed. In this short post I shall be sharing my solution with you.

CLIENT REQUIREMENTS

Being Event Management company , the client wanted to display the upcoming events in the calendar. Here are the requirements in brief

  1. All the date cells of the events should be highlighted with some background color
  2. When user gets its mouse over on the date cell , a tooltip should be displayed of the title of the event
  3. When user clicks on the date cell , it should navigate to the details of the event.

MY APPROACH

Having worked with the calendar control in my past projects , I knew this could be easily achievable. The rich object model of calendar control exposes one of the event called
DayRender. This event gets triggered for each and every date cells that are being rendered in the browser. The DayRenderEventArgs which is the argument of this event add an icing to the cake. This class exposes the cell property , which of TableCell Type. This gives you the power to extend the calendar control by overriding the default render behavior of the date cell. So before diving into the code , let us look at the figure below which shows the Event table definition.

Events Table Structure

The code of my logic is shown below

protected void Calendar_DayRender(object sender, DayRenderEventArgs e)
List eventList = new List();
eventList =(List)Session["Events"];
Event evt;
evt = eventList.Where(ed => Convert.ToDateTime(ed.EventDate).ToString("dd-MMM-yyyy") == e.Day.Date.ToString("dd-MMM-yyyy")).Select(ev => ev).FirstOrDefault();

if (evt != null)

{

e.Cell.Attributes.Add(“bgcolor”, “#faebd7″);

e.Cell.ToolTip = evt.EventTitle;

e.Cell.Attributes.Add(“onclick”, “javascript:goToUrl(‘” + evt.NavigationUrl + “‘)”);

e.Cell.Style.Add(“cursor”, “pointer”);

}

else

{

e.Cell.ToolTip = null ;

}

}

As I am using Linq to SQL in the page load event I retrieve all the events and store them in the session. The reason why I am doing so is that as mentioned above that the DayRender event of the calendar control fires for each and every date cell that is getting rendered on the browser. Hence it is not a good practice to create Linq Datacontext object everytime when the event gets fired this can be a disaster and can affect your application performance. Hence all the database operations are performed on the PageLoad event and the output is stored in the session variable. Then using the Linq Lambda expressions I find whether the date that is getting rendered is matching to my event date. If I find the matching record I override the default behaviour of that cell date.
Another thing that needs to be kept in mind is to set the calendar’s SelectionMode property to None. This is very important to avoid the postbacks on all the dates and user cannot click on any other dates except for the event dates. The event dates also that he clicks , the navigation happens through JavaScript and no postbacks happen.

CLOSURE

According to me the difference between average asp.net developer and smart asp.net developer is the knowledge of Javascript and Css. These two skills can make you distinct and you can do wonders in your application. You can even roll out your own custom controls library. Think about it.
Download the sample code. Please rename the file extension from .doc to .rar

Next Page »

Blog at WordPress.com.