Friday, February 26, 2010

Move Files between SharePoint document library

There are different options to move files between SharePoint document libraries. We can see them one by one.

Option 1 :

We can go to Explorer view, then copy paste the files to different location.

Here we can move multiple files. But Meta data like created by and created time will be changed.

Option 2 :

We can click the file --> Send To --> Other Location

Here Meta data will be retained. But we have to move one by one.

Option 3 :

We can write a C# application.
Add Microsoft.SharePoint.dll in your project.

    using Microsoft.SharePoint;

    private void button1_Click(object sender, EventArgs e)
        SPSite sourceSite = new SPSite(SourceSiteURL);
        SPWeb sourceWeb = sourceSite.OpenWeb();
        SPSite destSite = new SPSite(DestinationSiteURL);
        SPWeb destWeb = destSite.OpenWeb();

        SPFolder oFolder = sourceWeb.GetFolder(SourceFolderName).SubFolders.Folder;
        SPFolder oDestFolder = destWeb.GetFolder(DestinationFolderName).SubFolders.Folder;

        foreach (SPFile oFile in oFolder.Files)
            string strDestURL = oDestFolder.Url + "/";
            SPFile f = oFile;
            f.MoveTo(strDestURL + f.Name);


You can customize the code as you need files inside folder or subfolder.
Here we can move multiple files with meta data.

Monday, February 8, 2010

Expand Infragistics Grid using javascript

 Call the expandGrid function in onclientclick event of button.
    function expandGrid()
        var grid = igtbl_getGridById('<%=UltraWebGrid1.ClientID%>');      

    function expand(grid)
      var rowsLength = grid.Rows.length;
        for (var i = 0; i < rowsLength; i++)
            var rowObj = grid.Rows.getRow(i);
            if(rowObj.ChildRowsCount != 0)

Format Infragistics Grid column for Currency and Export to Excel with same format

To Format the Hierarchical grid's column from integer to currency, below code can be used.

protected void UltraGrid1_InitializeLayout(object sender, LayoutEventArgs e)
      UltraGrid1.DisplayLayout.Bands[0].Columns[10].Format = "$ #####0.00";

If we export the grid to Excel using UltraWebGridExcelExporter, that column will be integer only. To get in currency format use below code.

In Page_Load add CellExported event like below.
this.UltraWebGridExcelExporter1.CellExported += new Infragistics.WebUI.UltraWebGrid.ExcelExport.CellExportedEventHandler(UltraWebGridExcelExporter1_CellExported);

Code for CellExported event will be like below.

void UltraWebGridExcelExporter1_CellExported(object sender, Infragistics.WebUI.UltraWebGrid.ExcelExport.CellExportedEventArgs e)
    if (e.GridColumn.Format == "$ #####0.00")
   {            e.CurrentWorksheet.Rows[e.CurrentRowIndex].Cells[e.CurrentColumnIndex].CellFormat.FormatString =  "$ #####0.00";

Wednesday, February 3, 2010

Convert Text file to XML file

Convert text file to xml file which has delimiter as tab.

char strDelimiter = '\t';
string strFilePath = "c:/Test.txt";
DataSet oDS = new DataSet();
DataTable oTable = new DataTable();
DataRow oRows;
int intCounter = 0;

oDS.DataSetName = "Data_Root";
oDS.Namespace = strFilePath;

StreamReader oSR = new StreamReader(strFilePath);

//Go to the top of the file
oSR.BaseStream.Seek(0, SeekOrigin.Begin);

//Add in the Header Columns
foreach (string strFields1 in oSR.ReadLine().Split(strDelimiter))
//Now add in the Rows
oTable = oDS.Tables[0];
while (oSR.Peek() > -1)
        oRows = oTable.NewRow();
         foreach (string strFields2 in oSR.ReadLine().Split(strDelimiter))
                    oRows[intCounter] = strFields2;
                    intCounter = intCounter + 1;
         intCounter = 0;
//Create XML file

Read Tables Name from Access DataBase

OleDbConnection con;
OleDbCommand cmd;
OleDbDataReader reader;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\SampleDB.mdb;" + "Jet OLEDB:Engine Type=5");
DataTable schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]{null, null, null, "TABLE"});           

for (int y = 0; y < schemaTable.Rows.Count; y++)


Read column names from Access database table

OleDbConnection con;
OleDbCommand cmd;
OleDbDataReader reader;
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:\\SampleDB.mdb;" + "Jet OLEDB:Engine Type=5");
cmd = new OleDbCommand("SELECT * from Table1", con);           
reader = cmd.ExecuteReader();
int iColumns = reader.VisibleFieldCount;
for (int x = 0; x < iColumns; x++)

Automation of Word Mail Merge using Windows Application

I have Template and database in C drive with name  Template.doc and SampleDB.mdb respectively.

Add Reference: Microsoft Word 11.0 Object Library  from COM tab

using Word = Microsoft.Office.Interop.Word;

Word.Application wrdApp;
Word._Document wrdDoc;
Object oTemplate = "c:\\Template.doc";
Object oMissing = System.Reflection.Missing.Value;
Object oFalse = false;
Object oTrue = true;
Word.MailMerge wrdMailMerge;          

// Create an instance of Word  and make it visible.
wrdApp = new Word.Application();
wrdApp.Visible = true;           

// Create MailMerge Data.                       
wrdDoc = wrdApp.Documents.Open(ref oTemplate, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
wrdMailMerge = wrdDoc.MailMerge;
object oQuery = "SELECT Name,Address1,City from Table1";
string strDB = "c:\\SampleDB.mdb";
wrdDoc.MailMerge.OpenDataSource(strDB, ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oQuery, ref oMissing, ref oFalse, ref oMissing);
wrdMailMerge.SuppressBlankLines = true;

// Perform mail merge.
wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
wrdMailMerge.Execute(ref oFalse);

// Close the Template document.
wrdDoc.Saved = true;
wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);

// Release References.           
wrdMailMerge = null;
wrdDoc = null;
wrdApp = null;

Change BackColor for Listbox Items in Windows Application

Sample code to give alternate color for alternate items in listbox.
In my application lstBoxSelected is the name of listbox.

Write these two lines in page load event.
lstBoxSelected.DrawMode = DrawMode.OwnerDrawFixed;
lstBoxSelected.DrawItem += new DrawItemEventHandler(DrawListSelected);

DrawListSelected is the event for drawing backcolor for listitem.
        private void DrawListSelected(object sender, DrawItemEventArgs e)
            //Draw ListBox Items
            Rectangle rect = new Rectangle();
            rect = e.Bounds;

            // Setup the stringformatting object
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;

            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                // Fill Backcolor for selected item               
                e.Graphics.FillRectangle(Brushes.Orange, e.Bounds);
                Brush b;  // Object used to define backcolor               
                b = Brushes.LightCyan;
                if (e.Index % 2 == 0)
                    b = Brushes.PowderBlue;
                    b = Brushes.LightCyan;
                e.Graphics.FillRectangle(b, rect);
            if (lstBoxSelected.Items.Count > 0)
                e.Graphics.DrawString(lstBoxSelected.Items[e.Index].ToString().Replace("*","").Trim(), e.Font, Brushes.Black, rect, sf);