How to hide "Respond to this Survey" when a user has already responded to the Survey

1. The Rendering Template for the Survey List is in the defaulttemplates.ascx control under 12/Template/Control template.

2. The rendering template ID is “ViewToolBar”. This template renders the "Respond to this Survey", Actions menu and Settings menu in the toolbar.

3. This template is a generic template which is used by Other lists as well.

4. The template is given below :

<SharePoint:RenderingTemplate ID="ViewToolBar" runat="server">

<Template>

<wssuc:ToolBar CssClass="ms-menutoolbar" EnableViewState="false" id="toolBarTbl" ButtonSeparator="<img src='/_layouts/images/blank.gif' alt=''>" RightButtonSeparator="&nbsp;&nbsp;" runat="server">

<Template_Buttons>

<SharePoint:NewMenu AccessKey="<%$Resources:wss,tb_NewMenu_AK%>" runat="server" />

<SharePoint:ActionsMenu AccessKey="<%$Resources:wss,tb_ActionsMenu_AK%>" runat="server" />

<SharePoint:SettingsMenu AccessKey="<%$Resources:wss,tb_SettingsMenu_AK%>" runat="server" />

</Template_Buttons>

<Template_RightButtons>

<SharePoint:PagingButton runat="server"/>

<SharePoint:ListViewSelector runat="server"/>

</Template_RightButtons>

</wssuc:ToolBar>

</Template>

</SharePoint:RenderingTemplate>

5. "Respond to this Survey" is rendered by <SharePoint:NewMenu AccessKey="<%$Resources:wss,tb_NewMenu_AK%>" runat="server" />.

6. We cannot modify OOB defaulttemplates.ascx as this would be unsupported.

7. Make a copy of defaulttemplates.ascx under CONTROL TEMPLATES FOLDER of 12 hive and rename it to CustomDefaultTemplates.ascx

8. To hide “Respond to this Survey” if a user has already responded to the survey, Create a class which inherits from the Microsoft.Sharepoint.WebControls.NewMenu.

9. In this override CreateChildControls and have your logic to hide unhide "Respond to this Survey" based on your need.

10. Build the class library and install the dll to the GAC.

11. Modify the “ViewToolBar” in the custom ascx such that to render your custom "Respond to this Survey" menu from the class library you created.

(Note : You need to add your assembly reference and register the tag prefix in the Custom ascx control.)

The following is the snippet from the custom ascx control :

<SharePoint:RenderingTemplate ID="CustomViewToolBar" runat="server">

<Template>

<wssuc:ToolBar CssClass="ms-menutoolbar" EnableViewState="false" id="toolBarTbl" ButtonSeparator="<img src='/_layouts/images/blank.gif' alt=''>" RightButtonSeparator="&nbsp;&nbsp;" runat="server">

<Template_Buttons>

<CustomSurvey:SurveyMenu ID="SurveyID" runat="server"></CustomSurvey:SurveyMenu>

<SharePoint:ActionsMenu AccessKey="<%$Resources:wss,tb_ActionsMenu_AK%>" runat="server" />

<SharePoint:SettingsMenu AccessKey="<%$Resources:wss,tb_SettingsMenu_AK%>" runat="server" />

</Template_Buttons>

<Template_RightButtons>

<SharePoint:PagingButton runat="server"/>

<SharePoint:ListViewSelector runat="server"/>

</Template_RightButtons>

</wssuc:ToolBar>

</Template>

</SharePoint:RenderingTemplate>

12. Next step is to render the custom rendering template to the "Survey List" so that the custom "Respond to this Survey" menu will be rendered in the toolbar.

13. For this the "ToolbarTemplate" property of the View should be changed in the schema.xml file of the Survey List definition.

14. As modifying the OOB files are not supported, Copy the “SurveyList” folder from 12/Template/Features and paste in the same place as “CustomSurveysList”.

Change the feature ID of the “CustomSurveyList”

15. Open the schema.xml file available under survey folder of “CustomSurveysList” and add the “ToolbarTemplate=CustomViewToolBar” attribute to the “View BaseID=0”.

16. Sample attached : <View BaseViewID="0" FreeForm="TRUE" ReadOnly="TRUE" Type="HTML" ToolBarTemplate=" CustomViewToolBar ">

17. Install and activate "CustomSurveysList"

18. Create a Survey List using CustomSurveyList template and we should be able to achieve the functionality as required.

 

CODE FOR CUSTOM ascx file
---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using Microsoft.SharePoint.Utilities;
using System.Globalization;

namespace CustomSurveyMenu
{
    public class SurveyMenu : NewMenu
    {

        protected override void CreateChildControls()
        {
            try
            {
                string valueToEncode = SPResource.GetString("ToolBarMenuRespondToSurvey", new object[0]);
                SPLinkButton child = new SPLinkButton();
                child.ID = "diidIONewItem";
                child.Text = SPHttpUtility.HtmlEncode(valueToEncode);
                child.ToolTip = valueToEncode;
                child.ImageUrl = "/_layouts/images/NewItem.gif";
                child.HoverCellActiveCssClass = "ms-buttonactivehover";
                child.HoverCellInActiveCssClass = "ms-buttoninactivehover";
                child.Permissions = SPBasePermissions.EmptyMask | SPBasePermissions.AddListItems;
                child.PermissionContext = PermissionContext.CurrentFolder;
                child.RenderContext = base.RenderContext;
                child.NavigateUrl = SPContext.Current.Web.Url + "/Lists/" + SPContext.Current.List.Title + "/NewForm.aspx?Source=" + SPHttpUtility.HtmlEncode(SPContext.Current.Web.Url + "/" + SPContext.Current.List.Views["Overview"].Url);
                child.AccessKey = (string)HttpContext.GetGlobalResourceObject("wss", "tb_NewMenu_AK", CultureInfo.CurrentUICulture);

                SPList oList = SPContext.Current.List;
                SPListItemCollection oListItemColl = oList.Items;

                SPUser oUserNew = this.Web.CurrentUser;
                string strUserName = oUserNew.Name;

                foreach (SPListItem oItem in oListItemColl)
                {
                    string strCreatedBy = oItem["Created By"].ToString().Trim();
                    string[] SplitCreated = strCreatedBy.Split(new Char[] { '#' });

                    string strModifiedBy = oItem["Modified By"].ToString().Trim();
                    string[] SplitModified = strModifiedBy.Split(new Char[] { '#' });

                    if (Convert.ToString(SplitCreated[1]).ToUpper() == strUserName.ToUpper() && oItem["Completed"].ToString().ToUpper() == "1" && Convert.ToString(SplitModified[1]).ToUpper() == strUserName.ToUpper())
                    {
                        child.Visible = false;
                    }
                }

                this.Controls.Add(child);
            }
            catch (Exception ex)
            {
                // PUT YOUR LOGIC TO CATCH EXCEPTION. Whether you would want to write it into a txt file etc..
            }
        }
    }
}