Wednesday, August 26, 2009

call javascript form a sharepoint cutom toolbar

Here is a short code snippet which calls a javascript when you click a custom button in sharepoint. You should register the custom javascript through Page.ClientScript.

//Load the custom button
ToolBarButton printButton = (ToolBarButton)Page.LoadControl("~/_controltemplates/ToolBarButton.ascx");
printButton.ImageUrl = "~/layout/image/print.png";
//Here is where the magic happens
printButton.NavigateUrl = "javascript:PrintPage()";

ToolBar toolBar = (ToolBar)Page.LoadControl("~/_controltemplates/ToolBar.ascx");
toolBar.Buttons.Controls.Add(printButton);