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);
        }

        sourceWeb.Dispose();
        sourceSite.Dispose();
        destWeb.Dispose();
        destSite.Dispose();
    }


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%>');      
        expand(grid);
    }

    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)
            {           
                rowObj.setExpanded(true);
                expand(rowObj);
            }
            else
            {
                break;
            }
        }     
    } 

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;
oDS.Tables.Add("Table1");

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))
        oDS.Tables[0].Columns.Add(strFields1);
           
//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;
         oTable.Rows.Add(oRows);
}
          
//Create XML file
oDS.WriteXml("c:/Text_To_XML.xml"); 

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");
con.Open();
DataTable schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]{null, null, null, "TABLE"});           

for (int y = 0; y < schemaTable.Rows.Count; y++)
       listBox1.Items.Add(schemaTable.Rows[y][2].ToString());

con.Close();

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");
con.Open();
           
cmd = new OleDbCommand("SELECT * from Table1", con);           
reader = cmd.ExecuteReader();
int iColumns = reader.VisibleFieldCount;
for (int x = 0; x < iColumns; x++)
{                             
        listBox1.Items.Add(reader.GetName(x));               
}
con.Close();

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);
wrdDoc.Select();
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);
            }
            else
            {
                Brush b;  // Object used to define backcolor               
                b = Brushes.LightCyan;
                if (e.Index % 2 == 0)
                    b = Brushes.PowderBlue;
                else
                    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);
            }
        }