Category Archives: c#

SharePoint SPDataSource

Tired of writing codes and creating webparts? Here is a less code solution for SharePoint developers.

The SPDataSource control (Microsoft.SharePoint.WebControls.SPDataSource) means a lot to developers.

With less custom code one can quickly retrieve and display data from SharePoint lists and libraries.

This is how you can access list data from SharePoint. For select command you can use CAML query builder from U2U.

This data source can be used like a SQL data source which comes with standard controls.

<SharePoint:SPDataSource ID=”spdsSupervisor” runat=”server” DataSourceMode=”List” SelectCommand=”<Query><Where><Eq><FieldRef Name=’Heading’ /><Value Type=’Choice’>Questions</Value></Eq></Where></Query>”>

<asp:Parameter Name=”ListName” DefaultValue=”Feedback”/>


For data binding to any data bound controls this data source can be used. Given below is an example of using Data source with an asp grid view.

<asp:GridView runat=”server” id=”gvSupervisor” AutoGenerateColumns=”False” DataSourceID=”spdsSupervisor”
GridLines=”None” ForeColor=”#333333″ CellPadding=”4″ width=”100%”>

<FooterStyle BackColor=”#e2eeff” Font-Bold=”True” ForeColor=”White” />

<RowStyle BackColor=”#EFF3FB” Font-Size=”Smaller”/>


<asp:boundfield DataField=”Title” HeaderText=”Type” HeaderStyle-Font-Bold=”true” HeaderStyle-Font-Names=”verdana” HeaderStyle-ForeColor=”#525252″


<asp:templatefield HeaderText=”Rating”


    <asp:RadioButtonList runat=”server” id=”rblCourse”
RepeatDirection=”Horizontal” CssClass=”ms-radiotext”>











<PagerStyle HorizontalAlign=”Center” BackColor=”#2461BF”
ForeColor=”White” />

<SelectedRowStyle BackColor=”#D1DDF1″ Font-Bold=”True”

<HeaderStyle BackColor=”#e2eeff” Font-Bold=”True”
Font-Size=”Large” ForeColor=”White” />

<EditRowStyle BackColor=”#2461BF”/>

<AlternatingRowStyle CssClass=”ms-alternating”


The above code will result in a grid looking like below. I have used some SharePoint CSS (Core.css) to bring the SharePoint look.

The data should be displayed using bound columns. The SPDataSource can be used with SharePoint controls without any coding.

Given below is another example for binding data to InputFormCheckBoxList in SP.

<sharepoint:InputFormCheckBoxList ID=”InputFormCheckBoxList1″ runat=”server”
DataSourceID=”spdsSupervisor” DataTextField=”Title”/>

The above code will result in a checkbox list looking like the one below. On change of codes the assemblies need not be recompiled and deployed

To GAC. This means no building of WSP/CAB files and writing codes to copy assemblies to Global Assembly Cache. (Nice isn’t it)

Hassle free code with fewer bugs can be developed by using SPDataSource. There are different types of Datasourcemode-ListItem, CrossList, Webs, and ListOfLists.

SharePoint developers who develop applications and custom application pages with SharePoint as data store (Lists as tables) will be benefited by using SPDataSource.


Modal Popup Extender

 To hide a modal popup control (Ajax control toolkit may 2009 release), the below JavaScript code can be used. Instead of using OK control ID or OK script, this JavaScript code can be used in the  

 oncllientclick event of any control.  

  $find(‘mdlpopup’).hide();return false <asp:ImageButton ID=”imgbtnCancel” runat=”server” ImageUrl=”Images/cancel.gif”CausesValidation=”False” OnClientClick=”$find(‘mdlpopup’).hide();return false;” />   


While using a popup control extender, hiding it from server side sometimes gives an error. 

To avoid this use the popupcontrolextender.Cancel() event from server side. Often the control is rendered with

Server side code making it less possible to write client side scripts to hide it.

Internet explorer automation

Automating internet explorer from dotnet is possible through c# given below is a sample

To use the below code add reference to Microsoft.mshtml class and Microsoft html library class

To assist you in finding the names of html controls and their id’s use internet developer toolbar available from Microsoft (click here to get it now)    

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using SHDocVw; using System.IO;

using System.Security.Cryptography;

namespace ie


public partial class Form1 : Form

public Form1()




private void button1_Click(object sender, EventArgs e)


Object nullob = null;

Object URL =;; //Use the url of webpage to automate

mshtml.HTMLDocumentClass doc;   

InternetExplorer ie = new

ie.Visible = true;

ie.Navigate2(ref URL, ref nullob, ref nullob, ref nullob, ref nullob);







doc = (mshtml.HTMLDocumentClass)ie.Document;

mshtml.HTMLInputElementClass inputel = (mshtml.HTMLInputElementClass)doc.all.item(“sometextboxid”, null);

inputel.value = “sometext”; //Text needed to be filled in textbox get the text box id from source


inputel = (mshtml.HTMLInputElementClass)doc.all.item(“sometextboxid”, null);

inputel.value = “sometext”;


mshtml.HTMLSelectElementClass inputsel = (mshtml.HTMLSelectElementClass)doc.all.item(“questions”, null);

inputsel.selectedIndex = 2; //dropdown select


foreach (mshtml.HTMLInputElementClass inp in doc.getElementsByTagName(“INPUT”))

{ // to click the button in webpage

if (inp.value == “submit”)






Extract image from word files

Hi all, I had an requirement to extract images from word files and save it in an folder in ‘jpeg’ format

My approach went like this, to make end user easy to work with i made this simple design.
Microsoft.Office.Interop.Word is the main assembly which will be available if MS Office Word is installed.(for developement,
in case of deployment it can be packed with the product)
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass ();
Document doc = wordApp.Documents.Open(ref str1, refmissing, ref yes, ref missing, ref missing, refmissing, ref missing, ref issing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object wordApp.Visible = false ;
str1 = str;
object missing = System.Reflection. Missing.Value;
object yes = true;



//////////////////////Check whether the word file contains images and proceed to extraction 

 if (doc.InlineShapes.Count > 0)
                        for (int j = 0; j < doc.InlineShapes.Count; j++)
                            IDataObject data = Clipboard.GetDataObject();
                            if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
                                object bm = data.GetData(DataFormats.Bitmap);
                                Bitmap bmp;
                                bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));
                                tail = strcopy.Substring(strcopy.LastIndexOf(“\\”) + 1, strcopy.Length – strcopy.LastIndexOf(“\\”) – 5);
                                path = str.Remove(pos) + “\\Extractedimages” + tail;
                                tail = path;
                                path = path + “\\” + j.ToString() + “.bmp”;
                        MessageBox.Show(“images are saved in the folder” + tail);
                        wordApp.Quit(ref missing, ref missing, ref missing);                     

And hurray you are done.The images will be saved in the same location inside a folder with prefix ‘Extracted images’ and the file name.