Resize QuickLaunch

on Monday, July 13, 2009



var _tk_spDragProps = new Object();



function _tk_setGrab()

{

try

{

var oNavigation = document.getElementById("LeftNavigationAreaCell");


if (oNavigation == null) return;


var oDivider = oNavigation.nextSibling;

oDivider.style.cursor = "col-resize";

oDivider.onmousedown = _tk_spStartDrag;

_tk_findNavContainers(oNavigation);


_tk_spDragProps.element = oNavigation.nextSibling;

_tk_spDragProps.sidePanel = oNavigation;

_tk_spDragProps.element.nextSibling.style.width = "0px";

_tk_spDragProps.element.style.width = "5px";

_tk_spDragProps.element.nextSibling.nextSibling.style.width = "5px";


var width = _tk_getNavWidthCookie();

if (width != null)

{

_tk_setContainerWidths(width);

_tk_spDragProps.dragStartLeft = width;

}

}

catch (e)

{

alert(e.message);

}

}


function _tk_findNavContainers(e)

{

for (var c=0; c
{

var oChild = e.children[c];


if (oChild.id.indexOf("TreeViewNavigationManager") > 0)

_tk_spDragProps.navMan = oChild;


if (oChild.id.indexOf("TreeViewRememberScroll") > 0)

{

_tk_spDragProps.navScroll = oChild;

return;

}

_tk_findNavContainers(oChild);

}

}


function _tk_spStartDrag(e)

{

_tk_sp_startDrag(window.event);

}

setTimeout("_tk_setGrab()", 50);



function _tk_sp_startDrag(e)

{

var x = e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;

_tk_spDragProps.mouseX = x;

_tk_spDragProps.dragStartLeft = parseInt(_tk_spDragProps.sidePanel.style.width, 10);


if (isNaN(_tk_spDragProps.dragStartLeft)) _tk_spDragProps.dragStartLeft = 150;


_tk_spDragProps.element.style.zIndex = ++_tk_spDragProps.element.style.zIndex;


document.attachEvent("onmousemove", _tk_sp_dragMove);

document.attachEvent("onmouseup", _tk_sp_dragEnd);

window.event.cancelBubble = true;

window.event.returnValue = false;

}


function _tk_setContainerWidths(width)

{

_tk_spDragProps.sidePanel.style.width = width + "px";


if (_tk_spDragProps.navScroll != null)

_tk_spDragProps.navScroll.style.width = _tk_spDragProps.sidePanel.style.width;


if (_tk_spDragProps.navMan != null)

_tk_spDragProps.navMan.style.width = _tk_spDragProps.sidePanel.style.width;

}


function _tk_sp_dragMove()

{

var x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;

var width = (_tk_spDragProps.dragStartLeft + x - _tk_spDragProps.mouseX);

if (width < 150) width = 150;


_tk_setContainerWidths(width);


window.event.cancelBubble = true;

window.event.returnValue = false;

}


function _tk_getNavWidthCookie()

{

var oCookies = document.cookie;

var arCookies = oCookies.split(";");

for (var c=0;c
{

var arCookie = arCookies[c].split("=");

if (arCookie[0].replace(/^\s+|\s+$/g, '') == "tkNavWidth")

return arCookie[1];

}


return null;

}


function _tk_setNavWidthCookie(width)

{

document.cookie = "tkNavWidth=" + width + "; path=/";

}


function _tk_sp_dragEnd()

{

_tk_setNavWidthCookie(parseInt(_tk_spDragProps.sidePanel.style.width));

document.detachEvent("onmousemove", _tk_sp_dragMove);

document.detachEvent("onmouseup", _tk_sp_dragEnd);

}


Some Important MOSS Tips

on Wednesday, July 8, 2009

If you break your Web page…

When playing with the CEWP, you run the risk of adding bad code that will break your page. SharePoint will then throw out an error message, without offering any way to undo your changes.
If this happens to you, here is a useful trick: append the “?contents=1” querystring to your URL. It will give you access to the maintenance page, where you’ll be able to get rid of the faulty Web part.
For example, if you inadvertently break this page:
http://ThisServer.com/sites/ThisSite/ThisLibrary/allitems.aspx
Enter:
http://ThisServer.com/sites/ThisSite/ThisLibrary/allitems.aspx?contents=1

A trick to edit Web Part pages

On some pages, the edit option is not available or is grayed out. This is for example the case for the edit form of a list.
The workaround here is to append the “?ToolPaneView=2” querystring to your URL, which will switch your page to edit mode. Note that it seems to be unsupported by Microsoft, though I haven’t read an official confirmation.
For example, if you want to edit:

Accessing Embedded Resources

on Friday, May 15, 2009

Accessing Embedded Resources using GetManifestResourceStream
How do you access resource files (bmp, mp3, etc) that are compiled into your Windows Forms executable? After googling the subject, the Assembly object's GetManifestResourceStream method seemed to be the solution. MSDN's entry on this subject makes it seem relatively simple. However, it took me over an hour to get it to work in my latest project. There are two things you must do in other to access an executable's resources:

1.You must know the exact name of resource (mynamespace.resource.resourcename).

2.You must embed the resource into your executable.

Accessing the resources means that you must have access to the executable's Assembly object.

Example

Assembly resourceAssembly = Assembly.GetAssembly(typeof(Program));
string[] names=resourceAssembly.GetManifestResourceNames();
foreach (string str in names)
{
Console.WriteLine(str);
}
using (StreamReader stream = new StreamReader(resourceAssembly.GetManifestResourceStream("ConsoleApplication2.NewFolder1.Embed.xml")))
{
string str=stream.ReadToEnd();
}

How to enable remote debugging when you don't have Visual Studio on Servers

on Thursday, May 14, 2009

Remote Debugging is a great feature to use, especially when you work with virtual machines. It allows you to develop and debug locally but have the code running on another machine, virtual or physical. Microsoft SharePoint can't be installed on a Windows Vista or XP workstation, but needs to be installed on Windows Server 2003 or 2008, so the general recommendations has been for developers to have either Windows Server as their main OS or have a virtual machine with Windows Server. None of these work that well; either you have problems debugging your components and you have to rely on traces or message boxes or you have to have a virtual machine with a full development environment, which will not resemble a production machine.

So, Remote Debugging, is my primary way when working with SharePoint development. It allows you to have a smaller virtual machine, and it will allow you to develop in your main OS. But, Remote Debugging has been quite problematic to set up and configure, so here is a guide that you can follow to get it work in a few minutes.

This guide uses Microsoft Visual Studio 2008 and Windows Vista with UAC on the client and a Windows Server 2003 with WSS 3.0 running in a Virtual PC on the client.

Prepare your remote host
First of all you need to prepare your remote host for accepting incoming debugging requests, this is done by running the Visual Studio Remote Debugging Monitor on the remote machine, which is found under C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Remote Debugger\. Copy that folder to your remote host and place a shortcut to the msvsmon.exe on the desktop, so you have fast access to it whenever you need to debug.

The Remote Debugging Monitor is the one accepting your debug calls and the talking back to Visual Studio on your client, so to get these things to work you have to start the client as the user running Visual Studio (or have administrative permissions on the client). You can either log in to the remote host using that user or, as I prefer, right-click on the msvsmon.exe and choose Run As... Make sure that the user you are using to run the monitor with is member of the local Administrators group.


Now the Remote Debugging Monitor has started to waiting for new debugging connections. The debugging server was named with the username that is running the application and the server name, separated with an @-character. You can rename it using Tools->Options.

Prepare your client
Now it's time to prepare your client. First of all you have to run the Visual Studio 2008 Remote Debugger Configuration Wizard, which will open up the correct ports in your firewall. You will find the wizard under the Visual Studio Tools in your start menu. The wizard also allows you to run the Remote Debugger as a service on the machine which the wizard is run on, skip this step for this guide. When the wizard asks you for how you would like to configure your firewall, choose the Allow only computers on the local network... option and the finish the wizard.

Start Debugging
Now it's time to start Visual Studio 2008 and load up your solution and hook up the debugger to the remote machine. Prior to this you need to deploy the application to be debugged on the remote machine, including the .pdb files.

In Visual Studio choose Debug->Attach to process. In the Qualifier you have to enter the name that was given to the Remote Debugger Monitor and hit Enter, then all you need to do is attach to the process you would like to debug and set some break points!

That wasn't to hard?

Problems
Here are some problems that I have stumbled upon when trying to get these things to work.

Unable to connect to the Microsoft Visual Studio Remote Debugging Monitor named 'XXX. The Visual Studio Remote Debugger on the target computer cannot connect back to this computer. Authentication failed. Please see Help for assistance.

This one is due to the fact that the user running the debugging monitor are not allowed to access the client machine, make sure that the user running the monitor is either the same user running Visual Studio or the member of the Administrators group on your client.

You only see a few processes in the Attach to Process dialog.

First of all make sure you have checked the Show processes from all users check box, then make sure that the user running the monitor has access to the process on the machine, that is you have to make the user member of the local Administrators group. After adding the account to the Administrators group you have to restart the monitor.

Unable to connect to the Microsoft Visual Studio Remote Debugging Monitor named 'ibvsretail'. Logon Failure: The target account name is incorrect.

This one is pretty uncommon, but still I have had it. Somehow the server account in Active Directory had gone wrong so I hade to remove the machine from the domain and add it back.

Code behind variable in aspx pages

on Tuesday, May 5, 2009

For asp.net Server Controls if you want to access code behind variable in aspx pages. There are 3 things to remember:
1. Scope of the variable has to be protected or Public
2. The variable can be accessed only using DataBinding Syntax like below.
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DateTime.Now.ToString()%>'></asp:TextBox>
3.You have to Call Page.DataBind() or TextBox1.DataBind() method in the Page_Load event or any other event before Page_Render event's base.Render(writer) method call.

For HTML controls you could just use the traditional syntax like below.
<input type='text' value='<%=DateTime.Now.ToString()%>' />

Serialization Deserialization

on Friday, April 24, 2009

There are two kind of Serialization that you could perform on the object Xml and Binary.
Samples are below.

Suppose We have Class Animal.
[Serializable]
public class Animal
{
public Animal()

{
}
[NonSerialized]//field which doesn't required to be serialized and deserialized
public String animalName;
public String foodTypeCategory;
public Boolean isDomesticed;
}


Xml Serializer doesn't need class to be marked as Serializable while Binary Serialization does.
//Xml Serialization
class Program
{
static void Main(string[] args)
{
Animal cow = new Animal();
cow.animalName = "Cow";
cow.foodTypeCategory = "Any";

//Stream stream = File.Open("c:\\abc.xml", FileMode.Create);
MemoryStream stream = new MemoryStream();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Animal));
serializer.Serialize(stream, cow);
byte[] array = stream.GetBuffer();
stream.Close();
MemoryStream stream1 = new MemoryStream(array);

Animal obj1 = (Animal)serializer.Deserialize(stream1);
}
}

For Binary Serialization the only line which changes in the above code is XmlSerializer object creation line which will be replaced with
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new BinaryFormatter();


Also If some time you want to Control the Serialization And Deserialization of your class then you could Implement ISerializable interface and override following methods for Animal class as an example.

public Animal(SerializationInfo info, StreamingContext ctxt)
{
animalName=(String)info.GetValue("animalName",typeof(string));
foodTypeCategory = (String)info.GetValue("foodTypeCategory", typeof(string));
isDomesticed = (Boolean)info.GetValue("isDomesticed", typeof(bool));
}

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("animalName", animalName);
info.AddValue("foodTypeCategory", foodTypeCategory);
info.AddValue("isDomesticed", isDomesticed);
}


Also If you want to convert string to byte[] and byte[] to String you could use following functions.
private String UTF8ByteArrayToString ( Byte[ ] characters )

{

UTF8Encoding encoding = new UTF8Encoding ( );

String constructedString = encoding.GetString ( characters );

return ( constructedString );

}



///



/// Converts the String to UTF8 Byte array and is used in De serialization

///


///

///

private Byte[ ] StringToUTF8ByteArray ( String pXmlString )

{

UTF8Encoding encoding = new UTF8Encoding ( );

Byte[ ] byteArray = encoding.GetBytes ( pXmlString );

return byteArray;

}

System.Transactions Timeout facts

on Tuesday, April 7, 2009

Maximum timeout value: 10 min(default). If your application requires different maxTimeout value then you could configure it in machine.config file which would be applicable to all the applications. maxTimeout can only be specified in machine.config file and it will be applicable to all the apps.

Sample:
<configuration>
<system.transactions>
<machinesettings maxtimeout="00:02:00">
</system.transactions>
</configuration>

Configurable timeout value: 1 min(default). If you want specific timeout value other than 1 minute you could set it in either app.config(application specific) and/or machine.config(machine wide setting) or in code as well.

Sample:
<system.transactions>
<defaultsettings timeout="00:00:59">
</system.transactions>


Note: If the specified defaultSettings timeout value is greater than maxTimeout then maxTimeout value will take precedence.