Saturday, May 1, 2010

Add New toolbar button on Task Form , MOSS 2007

Ok...it took me some time to keep my word, i promised in my previous blog that I am going to write a blog for adding new custom button to Task toolbar with out creating custom forms..!

Lots of blogs I found on internet says that you need to create a custom form if you want to modify something in default forms that generated automatically whenever you create list in sharepoint..but i don't want to follow that procedure because for me that does not make much sense for small modifications...

You may be thinking, is it can be deployed to production? Yes, the procedure I am following is very easy to deploy to production and you DON'T need to have Sharepoint Designer in production.!!

Ok, enough theory now we see how we can do this....

First, if you did not read my previous blog then first read that blog and come back here because I am not going to explain how to create "CustomToolBar.ascx" page.

STEPS TO FOLLOW

1. Now, I am assuming you read my previous blog and created "CustomToolBar.ascx" page and placed it in  right folder.
2. Open the file you created in above step [you can open with Notepad or you can open with Visual studio].
3. Create custom button which we are going to use in the Task form

Code to create Custom Button

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace MAR.HariCustomButton
{
   public class CustomOKButton : Button
    {
        public CustomOKButton() : base()
        {
        }
        public override string AccessKey
        {
            get
            {
                return base.AccessKey;
            }
            set
            {
                base.AccessKey = value;
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Text = "Custom OK";
            this.UseSubmitBehavior = false;
            this.ID = "saveCustonButton";
            this.CssClass = "ms-ButtonHeightWidth";  //default sharepoint button CSS
            this.Click += new EventHandler(CustomOKButton_Click);

          //SHOW this button in NEW/EDIT forms only...
            if (SPContext.Current.FormContext.FormMode == SPControlMode.New || SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
            {
                this.Visible = true;
            }
            else
                this.Visible = false;
        }
       void CustomOKButton_Click(object sender, EventArgs e)
       {
           try
           {
               //DO WHAT EVER YOU WANT HERE.....................
                SPListItem NewListItem = SPContext.Current.ListItem;
               NewListItem["updateFieldName"] = "FieldValuechanged";
               NewListItem.Update();
           }
           catch (System.Exception ex)
           {
               throw new System.Exception(ex.Message);
           }
       }

    }
}



4.Compile the project and sign the assembly. 'Signing' of assembly is required [to sign, right click on project, go to "properties", Select "Sign" tab and sign it]

5. Now go ahead register the assembly, which you created in above step,  in "CustomToolbat.ascx" page.
<%@Register TagPrefix="MAR" Assembly="MAR.HariCustomButton, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4dakji9c38ec5" namespace="MAR.HariCustomButton"%>

** Version and PublicKeyToken is different for your assembly **

6. find "<wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" [if you search you find this tag 2 times so add following code in 2 places]  in "customToolbar.ascx" file. Add your control in  "<Template_RightButtons>" section.



<Template_RightButtons>
<MAR:CustomOKButton accesskey="<%$Resources:wss,tb_save_AK%>" style="display:none" target="_self" runat="server"/>
<SharePoint:SaveButton runat="server" />
<SharePoint:GoBackButton runat="server"/>
</Template_RightButtons>



** When you add your button in <Template_RightButtons>  section then it appears in all lists. So if you want your button to appear on all Lists then you can delete style="display:none" and you are done....!!

7. But, my case is different, I don't want my button to appear in all lists forms so I make it invisible by default so that I can make it visible for particular list. For that you need to follow 2 more steps.

8. Lets say, I have a custom list in sharepoint named as "ShowMyButtonList" and I have following javascript in my "showbutton.js" file.



function showButtoncontrol()    //'saveCustonButton' is the id of my custom button
{
var FieldName = 'saveCustonButton';
var arr = document.getElementsByTagName("input");
for (var i=0;i < arr.length; i++ )
{
if (arr[i].id.indexOf(FieldName) > -1 || arr[i].title.indexOf(FieldName) > -1)
{
var control = arr[i];
control.style.display = 'block';     //when we created button it is 'invisble' by default, make it visible
}
}
}


//Insert javscript based on page
var strHref = window.location.href;
//u can find this from URL when you open your list
if (strHref.indexOf("Lists/ShowMyButtonList/NewForm.aspx") > -1 || strHref.indexOf("Lists/ShowMyButtonList/EditForm.aspx") > -1)
{
_spBodyOnLoadFunctionNames.push("showButtoncontrol");
}



9. Place the javascript file you created in Step 8 in to "12/Template/LayOuts/1033/showbutton.js"

10. Now, very important step....if you followed all my above steps then Enjoy...else repeat step 1 to 9 to make sure everything is correct...

Happy programming.....

No comments:

Post a Comment