Tuesday, January 20, 2009

Attaching a database without (.ldf) file

If you don't have the log (.ldf) file for an SQL server database, and you need to attach the database into sql server 2005, then run this sql query from SQL server 2005:

sp_attach_single_file_db N'OMS', N'E:\DataBase\OMS.mdf'

The first part is the name of the database and the second part is the physical address of the (.mdf) file. "N" prefix represents National Language Character Set. Which means the subsequent string is in unicode.

Alternatively, you can directly add a Database into a visual studio project. For this, you need to add an entry in the app.config file:

<add name="WpfDataEntry.Properties.Settings.OMSConnectionString"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|\OMS.mdf;
Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient" />

The |Datadirectory| is a variable which holds the location of the root folder for databases. For ASP .NET web applications, the name of the folder is App_data. for desktop applications, the folder is the "Bin" folder

Credit for this post goes to Tanveer vai (Tanveer Ibn Haresh, Software Engineer, Bording Vista Ltd.)

While editing an ultrawebtree node, stop users to input escape characters

In order to stop users, from giving input of escape characters, you have to handle the editkeydown client side event. in the designer file inside the client side events add this:

EditKeyDown="UltraWebTree1_EditKeyDown"

In the javascript file add this function:

function UltraWebTree1_EditKeyDown(treeId, nodeId, keycode) {
if (keycode == 220) {
var treeInstance = igtree_getTreeById(UltraWebTreeClientID)
treeInstance.endEdit(false);
}
}

when the user presses "\" then the value of keycode becomes 220. endEdit(false) ends the editing process of a node, and goes back to the text that was there before editing began. endEdit(true) stops the editing but keeps the updated text that was while the tree node was being edited. So, its always a good idea to use endEdit(false) method. UltraWebTreeClientID was passed from the server side.

If you need to block user from pressing other keys too, then debug the process and get the value of keycode for corresponding characters, then add those values in the if condition.

Sunday, January 18, 2009

Backup and Restore a sharepoint site from 1 pc to another with installed features

In order to backup and restore a sharePoint site say from pc1 to pc2 using command prompt, First open the command prompt in pc1:
start... Run... type cmd... press enter

In the command prompt, type:

cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
...press enter

Then run this command to take backup of an existing site:

stsadm -o backup -url http://svradm002:11226/ -filename c:\backUpSite.dat -overwrite
replace the url and file location with the data you need

In Pc2, copy and paste the backupSite.dat file to any location (ex: in c drive). Open command prompt and go to the bin folder in preceding way. Then use this command to restore from a backup to an existing site. Remember, you have to create the site on the following location (http://vpc-wss:30322/) first, then you can restore from the dat file on that site. (Assume that the address of our target site is: http://vpc-wss:30322/)

stsadm -o restore -url http://vpc-wss:30322/ -filename c:\backupSite.dat -overwrite

U will find that the feature is not available with the new site that you created. If you added the feature manually, by creating feature folder, element & manifest file then you have to do again in the new PC. Check my previous blog post. But if you created a feature using Visual Studio with vsewss (visual studio extension for windows sharePoint server) installed, then you have to do this:
-In PC1, open the project folder
-copy the debug folder
-paste it anywhere in pc2
-then edit the setup.bat file inside the debug folder. replace old addresses http://localhost:11226/ with new one: http://localhost:30322/
-then run the setup.bat file.
this will add the feature in the new site.

Monday, January 12, 2009

Use a single event to traverse back and forth between server-side and client-side 2 times to complete a task

I placed a UltraWebTree in a Usercontrol(.ascx) file, and used a (.cs) file to handle event and logic. Then, reference a Javascript where I handled the client side events. I had to add a new node to a tree from client-side, rename it, add it into the database from server-side, then select it from the client side, and go back to server side to open folder associated with that new node. I did these in 4 steps:

Step1. Create a new node from client side, rename it:

Add this code into the Javascript file to add a new node:

var currentNodeId;
var newNodeAdded = false;
var newNodeId;

function addChild() {
var tree = igtree_getTreeById(UltraWebTreeClientID);
var node = tree.getNodeById(currentNodeId);
if (node.hasChildren()) {
if (node.getExpanded() == false) {
node.setExpanded(true);
}
}
if (node) {
newnode = node.addChild(newFolderName);
newNodeId = newnode.Id;
newnode.edit();
newNodeAdded = true;
}
}

CurrentNodeID is the id of the node that is currently selected taken from the nodeSelectionChanged mathod. UltraWebTreeClientID is the id of the tree that was passed from the (.cs) file's pageLoad section's If(!Page.isPostback) portion:

String Script = "var UltraWebTreeClientID = '" + UltraWebTree1.ClientID.Replace("_", "") + "';";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "InitVariable", Script, true);


Step2: Go back to server-side to add a record into the DB:

After a new node has been added, the nodeAdded event will be fired, which you need to handle:

void UltraWebTree1_NodeAdded(object sender, Infragistics.WebUI.UltraWebNavigator.WebTreeNodeEventArgs e)
if (uxTreeEventLog.Text == "")
{
TreeNodeChangedArgs args = new TreeNodeChangedArgs();
args.Name = e.Node.Text;
args.Parent = e.Node.Parent.Tag.ToString();
args.TreeNodeAction = TreeNodeChangeAction.Added;
if (TreeNodeChanged != null)
TreeNodeChanged(sender, args);
e.Node.DataKey = Convert.ToInt32(args.ID);
e.Node.Tag = Convert.ToInt32(args.ID);
}

TreeNodeChangedArgs is a class declared globally to handle the event properties:


public class TreeNodeChangedArgs
{
public String ID;
public String Name;
public String Parent;
public TreeNodeChangeAction TreeNodeAction;
public Boolean Cancel;
}

TreeNodeChangedAction is an enum declared globally:


public enum TreeNodeChangeAction
{
Added,
Changed,
Removed
}

TreeNodeChange is a variable of a deligate also declared globally:

public delegate void TreeNodeChangedEventHandler(object sender, TreeNodeChangedArgs e);
public TreeNodeChangedEventHandler TreeNodeChanged;

It will take the event outside the usercontrol and let you handle the event from the page where you have placed this usercontrol. There you will add a new record using Business Layer in the DB taking info of the node (ex: id, name, parent, etc...) that was passed from here.

Step 3: Go back to client-side to select the node:

To go back to client-side again, you need to use end request method from the (.cs) file's pageLoad section's If(!Page.isPostback) portion:

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "RegisterFunciton", "Sys.WebForms.PageRequestManager.getInstance().add_endRequest(TreeControl_EndRequestHandler);", true);

Now, you need to write a function in the javascript file with the name TreeControl_EndRequestHandler:

function TreeControl_EndRequestHandler(sender, args) {
if (newNodeAdded == true) {
var tree = igtree_getTreeById(UltraWebTreeClientID);
var node = tree.getNodeById(newNodeId);
node.setSelected(true);
newNodeAdded = false;
}
}

Step 4: Open associated folder from server-side:

After selecting the new node from the client-side, the selectionChanged event will be fired which you have to handle in the (.cs) file:


void UltraWebTree1_NodeSelectionChanged(object sender, WebTreeNodeEventArgs e)
{
if (NodeClicked != null)
NodeClicked(sender, e);
}

Again, NodeClicked is a variable of a delicate that was declared globally:

public delegate void NodeClickedEventHandler(object sender, WebTreeNodeEventArgs e);
public NodeClickedEventHandler NodeClicked;

You can take the event outside of this control and handle it on the page, where you can search for associate folder in the DB, and open it (in a different panel beside to the tree)

Step 5: Create chain effect:

If you have a debugging point set at the TreeControl_EndRequestHandler function, then you will notice that the process has come back to client-side again. In fact, after every server-side events, process will come back to here, from where you can chain it back to server side again.

Most part of this code(server-side part) was done by Ranku vai (Anupam Ranku, Senior Software Engineer, Bording Vista Ltd.) Thank you very much, Ranku vai.

Check whether all the checkboxes of a tree are unchecked from client side (Javascript)

We are using infragestics for our develpment. so we had to use the UltraWebTree provided by infragestics. Add this code in client side:

First, declare a global varriable:

var allUnchecked = true;

This part of the code should be written inside a javascript function where u will handle the event. For my work, I used the event handling of UltraWebMenu.


function ultraMenu1_itemClicked(MenuId, ItemId) {
if (ItemId == UltraWebMenuClientID + "_4") {
var treeInstance = igtree_getTreeById(UltraWebTreeClientID);
var testNodes = treeInstance.getNodes();
allUnchecked = true;
checkNodes(testNodes);
if (allUnchecked == true) {
alert('All are unchecked');
}
}
}

UltraWebTreeClienID holds the client id of infragestics ultrawebtree that was passed from the server side (a .cs code behind file where the ultrawebtree has been placed) in following way:


String Script = "var UltraWebTreeClientID = '" + UltraWebTree1.ClientID.Replace("_", "") + "';";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "InitVariable", Script, true);


UltraWebMenuClientID was passed from that .cs file in the same way, and _4 checks whether the 4th item of the ultrawebmenu is clicked.
checknodes is the function that checks all the nodes whether they are checked or not. Place it anywhere in the javascript file:

function checkNodes(testNodes){
for (var i = 0; i < testNodes.length; i++) {

if (testNodes[i].getChecked() == true) {
allUnchecked = false;
}
if (testNodes[i].hasChildren() == true) {
checkNodes(testNodes[i].getChildNodes())

}

}

}




After running checknodes recursively for all the nodes, it will go back to the main function and will show the alert if all nodes are unchecked.