Friday, February 06, 2015

Create new Link in Welcome Control and hiding links in welcome control

Welcome control is normally present in layouts/controltemplates folder. To create new item in welcomecontrol, take a backup of existing control and work on the backupfile. Do not modify the welcome control file directly. Below are steps to create new link in contextual menu of welcome control.

1. Take a backup of existing welcome control
2.  In <CustomTemplate> tag , add below line of code for the new link

<SharePoint:MenuItemTemplate runat="server" id="NewLink"
Text="New LInk"
Description=New LInk"
MenuGroupId="300"
Sequence="1001"
 ClientOnClickNavigateUrl="~site/_layouts/15//NewLink.aspx"
 HiddenScript ="GetLinkVisibility();"
/>

where

ID - Id of contextual menu
Text - Text to be displayed
Description - Description of the menu item
MenuGroupId - Id of the group in which the link is to be displayed
Sequence - Sequence order in which the link is to be displayed. Increment the order from the last menu item
HiddenScript - Calls javascript method which will return values (true or false) to set visibility of the link

3. If link is to be displayed based on certain user permission, write the c# code at top as below
<script type="text/javascript">
var linkVisibility;
    function SetLinkVisibility(reportVisiblity) {
if(reportVisiblity=='visible')
{
linkVisibility=false;

}
else
{
linkVisibility=true;

}

     
    }

function GetLinkVisibility()
{
return linkVisibility;
}
</script>
<script runat="server">
      protected override void OnLoad(EventArgs e)
      {
  if (!Page.IsPostBack)
                {
           SPSite site = SPContext.Current.Site;
          SPWeb web = site.OpenWeb();
 SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser;
                SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
                SPRoleDefinition roleDefinition = roleDefinitions["CustomPermissionLevel"];

               
          if (usersRoles.Contains(roleDefinition))
          {
 System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "visiblelinks", "javascript:SetLinkVisibility('visible');", true);

          }
          else            
          {
 System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "hidelinks", "javascript:SetLinkVisibility('hidden');", true);
             
          }
 }
 base.OnLoad(e);
      }

  </script>

4. Update the reference of the welcome control in master page.