Tuesday 7 January 2014

Hide Edit Page Link From Site Actions Menu

If you have a requirement to hide a link from site actions menu, for example, the edit page link, you can use SharePoint designer instead of creating a custom code.

Here are the steps on how to hide edit page link from site actions menu of a SharePoint site:

1. Open the site using SharePoint Designer.
2. Go to _catalogs >> masterpage.
3. Check out and edit default.master page.
4. Find the below code from the page:

<SharePoint:MenuItemTemplate runat="server" id="MenuItem_EditPage"
Text="<%$Resources:wss,siteactions_editpage%>"
            Description="<%$Resources:wss,siteactions_editpagedescription%>"
            ImageUrl="/_layouts/images/ActionsEditPage.gif"
            MenuGroupId="100"
            Sequence="200"
            ClientOnClickNavigateUrl="javascript:MSOLayout_ChangeLayoutMode(false);" />


and then add the following lines on it:

PermissionsString="ManageSubwebs"
PermissionMode="Any"


The code should now look like:

<SharePoint:MenuItemTemplate runat="server" id="MenuItem_EditPage"
            Text="<%$Resources:wss,siteactions_editpage%>"
            Description="<%$Resources:wss,siteactions_editpagedescription%>"
            ImageUrl="/_layouts/images/ActionsEditPage.gif"
            MenuGroupId="100"
            Sequence="200"

            ClientOnClickNavigateUrl="javascript:MSOLayout_ChangeLayoutMode(false);"
            PermissionsString="ManageSubwebs"
            PermissionMode="Any" />


5.  Save and check in default.master page.


 

Attach OnChange Event to a SharePoint Lookup Field Using JQuery

The below script will attach onchange event to a SharePoint lookup field (with items < 20 or items >= 20).

var ddlBranch;

// check if no. of items < 20 items
 if($("select[title='Branch']").html() != null)
 {
        ddlBranch = $("select[title='Branch']")[0];
 }
 // check if no. of items >= 20
 else if ($("input[title='Branch']").html() != null)
 {
        ddlBranch = $("input[title='Branch']");

        //  override the default HandleOptDblClick function in core.js
        //  added CallCustomFunction function

        HandleOptDblClick = function()
        {         
                var opt=event.srcElement;
                var ctrl=document.getElementById(opt.ctrl);
                SetCtrlFromOpt(ctrl, opt);
                SetCtrlMatch(ctrl, opt);
                opt.style.display="none";
   
                // this checking is needed if you have other lookup fields in the form.
                // this ensure that your custom function will be called only onchange of your specific lookup field
                if($(ctrl).attr('title') == 'Branch')
                         CallCustomFunction();
       
        }   
 }

 // attach onchange event to ddlBranch
 $(ddlBranch).change(function() {
        CallCustomFunction();
 }); 



//  override the default FilterChoice function in core.js
//  changed strHandler into "onclick" instead of "ondblclick"
//  this will allow the user to just click once to select item instead of double click
function FilterChoice(opt, ctrl, strVal, filterVal)
{
        var i;
        var cOpt=0;
        var bSelected=false;
        var strHtml="";
        var strId=opt.id;
        var strName=opt.name;
        var strMatch="";
        var strMatchVal="";
        var strOpts=ctrl.choices;
        var rgopt=strOpts.split("|");
        var x=AbsLeft(ctrl);
        var y=AbsTop(ctrl)+ctrl.offsetHeight;
        var strHidden=ctrl.optHid;
        var iMac=rgopt.length - 1;
        var iMatch=-1;
        var unlimitedLength=false;
        var strSelectedLower="";
        if (opt !=null && opt.selectedIndex >=0)
        {
                bSelected=true;
                strSelectedLower=opt.options[opt.selectedIndex].innerText;
        }
        for (i=0; i < rgopt.length; i=i+2)
        {
                var strOpt=rgopt[i];
                while (i < iMac - 1 && rgopt[i+1].length==0)
                {
                        strOpt=strOpt+"|";
                        i++;
                        if (i < iMac - 1)
                        {
                                strOpt=strOpt+rgopt[i+1];
                        }
                        i++;
                }
                var strValue=rgopt[i+1];
                var strLowerOpt=strOpt.toLocaleLowerCase();
                var strLowerVal=strVal.toLocaleLowerCase();
                if (filterVal.length !=0)
                        bSelected=true;
                if (strLowerOpt.indexOf(strLowerVal)==0)
                {
                        var strLowerFilterVal=filterVal.toLocaleLowerCase();
                        if ((strLowerFilterVal.length !=0) && (strLowerOpt.indexOf(strLowerFilterVal)==0) && (strMatch.length==0))
                                bSelected=false;
                        if (strLowerOpt.length > 20)
                        {
                                unlimitedLength=true;
                        }
                        if (!bSelected || strLowerOpt==strSelectedLower)
                        {
                                strHtml+="<option selected value=\""+strValue+"\">"+STSHtmlEncode(strOpt)+"</option>";
                                bSelected=true;
                                strMatch=strOpt;
                                strMatchVal=strValue;
                                iMatch=i;
                        } 
                        else
                        {
                                strHtml+="<option value=\""+strValue+"\">"+STSHtmlEncode(strOpt)+"</option>";
                        }
                        cOpt++;
                }
        }
       
var strHandler=" onclick=\"HandleOptDblClick()\" onkeydown=\"HandleOptKeyDown()\"";
        var strOptHtml="";
        if (unlimitedLength)
        {
                strOptHtml="<select tabIndex=\"-1\" ctrl=\""+ctrl.id+"\" name=\""+strName+"\" id=\""+strId+"\""+strHandler;
        }
        else
        {
                strOptHtml="<select class=\"ms-lookuptypeindropdown\" tabIndex=\"-1\" ctrl=\""+ctrl.id+"\" name=\""+strName+"\" id=\""+strId+"\""+strHandler;
        }
        if (cOpt==0)
        {
                strOptHtml+=" style=\"display:none;position:absolute;z-index:2;left:"+x+   "px;top:"+y+   "px\" onfocusout=\"OptLoseFocus(this)\"></select>";
        }
        else
        {
                strOptHtml+=" style=\"position:absolute;z-index:2;left:"+x+   "px;top:"+y+   "px\""+   " size=\""+(cOpt <=8 ? cOpt : 8)+"\""+   (cOpt==1 ? "multiple=\"true\"" : "")+   " onfocusout=\"OptLoseFocus(this)\">"+   strHtml+   "</select>";
        }
        opt.outerHTML=strOptHtml;
        var hid=document.getElementById(strHidden);
        if (iMatch !=0 || rgopt[1] !="0" )
                hid.value=strMatchVal;
        else
                hid.value="0";
        if (iMatch !=0 || rgopt[1] !="0" )
                return strMatch;
        else return "";
}