Sunday, March 30, 2008

SharePoint and .net Tips and Tricks






































Tips and Tricks

1.

To enable debug operation in SharePoint server web part, follow the following steps


i. Open web.config file of the SharePoint web site in which you want to make debug mode.


ii. Make callStack "false" to "true" inside of <SafeMode> tag

<SharePoint>

<SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">

<PageParserPaths>

</PageParserPaths>


iii. Set Custom Error mode to "Off" inside of <customErrors> tag.

</httpHandlers>

<customErrors mode="Off" />

<httpRuntime maxRequestLength="51200" />


iv. Set Compilation batch "false" to "true" and debug "false" to "true" in <compilation> tag.

<globalization fileEncoding="utf-8" />

<compilation batch="true" debug="true">

<assemblies>

2.

Fetching multiple SQL query at one time and store in dataset tables.


// Set SQL query to fetch projects

string sqlQuery = "Select * from Projects; Select * from Steps";


// Create dataset

DataSet dataSet = new DataSet();


// Populate dataset

using (SqlConnection connection = new SqlConnection(m_ConnectionString))

{

SqlCommand command = new SqlCommand(sqlQuery, connection);

SqlDataAdapter dataAdapter = new SqlDataAdapter(command);

dataAdapter.Fill(dataSet);

}


// Set dataset table names

dataSet.Tables[0].TableName = "Projects";

dataSet.Tables[1].TableName = "Steps";

3.

Sorting DataTable if it is not binding to controls(using dataview)


Here, main Data Table is objDataTable, It will in sorting by the column 'sd_planned' in ascending after following coding.


//Sorting Data Table

DataView dtview = new DataView(objDataTable);

dtview.Sort = "sd_planned asc";

objDataTable = dtview.ToTable();

dtview = null;

4.

Using JavaScript in ASP.NET and handling enter key from the client side.


<script language="javascript" type="text/javascript">

function clickButton(e, buttonid){

var bt = document.getElementById(buttonid);

if (bt){

if(navigator.appName.indexOf("Netscape")>(-1)){

if (e.keyCode == 13){

bt.click();

return false;

}

}

if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){

if (event.keyCode == 13){

bt.click();

return false;

}

}

}

}

</script>


For use JavaScript in ASP.NET in dynamic way (Code Behind), we have to call attribute method of the control using following codings


TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

5.

How to display MS Visio file in SharePoint WebPart?


<OBJECT classid="CLSID:279D6C9A-652E-4833-BEFC-312CA8887857"

codebase="http://download.microsoft.com/download/4/5/2/452f8090-413f-408f-83c0-edd66db786ee/vviewer.exe"

id="viewer1" width="100%" height="100">

<param name="BackColor" value="16777120">

<param name="AlertsEnabled" value="1">

<param name="ContextMenuEnabled" value="1">

<param name="GridVisible" value="0">

<param name="HighQualityRender" value="1">

<param name="PageColor" value="16777215">

<param name="PageVisible" value="1">

<param name="PropertyDialogEnabled" value="1">

<param name="ScrollbarsVisible" value="1">

<param name="ToolbarVisible" value="1">

<param name="SRC" value="http://wssxxxx/Shared%20Documents/Yukon%20Timeline.vsd">

<param name="CurrentPageIndex" value="0">

<param name="Zoom" value="-1">

</object>

5.

How to Create User Control from Web Page (.aspx to .ascx)


In addition to explicitly creating a user control, we can also convert a Web page to a user control. The primary benefit is that we can do our prototyping and testing without having to deal with placing the control on a Web page.


- Remove the <html>, <body>, and <form> begin and end tags.

- Change the @Page directive at the top of the file to @Control directive.

- Change the file extension of your Web page from .aspx to.ascx.

- In the @Control directive, change Inherits="System.Web.UI.Page" to Inherits="System.Web.UI.UserControl".

6.

Raising events from user control to web page


Rasing events from user control to a web page, we can pass any processed value from user control to web page. Download the solution project

7.

[SQL Server] Exporting records from the selection of columns and records from various tables.


The following sql code format should be used to export or create table with records from the selection of columns and records from various tables. In this sql, the different columns are taking from different three tables and also selected set of records. The records from this selection query will be stored in table named 'NewTableName'.


Select Table1.Col1, Table2.Col2, Table3.Col

Into NewTableName

From Table1, Table2, Table3

Where Table1.ID=Table2.ID and Table2.NextID=Teble3.ID