Wednesday, January 4, 2012

Overwriting the SP 2010 'Email Link' Ribbon button

And so it goes, our user community was looking for a way to email the content from a wiki page to users outside of the network. The solution was to overwrite the "E-Mail a Link" button found on the ribbon within the Page tab. But how do we accomplish this? Well, it turns out, it's pretty easy.

While we're not technically overwriting the functionality of the button, we are hiding it and generating our own. Thus, this task became a two step process (three if you want to add a configuration page for the email form):

Step one, hide the default "E-Mail a Link" button: To perform this, we need to write a feature that calls a Custom Action. I won't bore you with the specifics of how to generate a feature project and I'll cut to the juicy details on what needs to be written in the elements.xml file.

<CustomAction
    Id="RemoveEmailPageLinkRibbonButton"
    Location="CommandUI.Ribbon">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.WikiPageTab.Share.EmailPageLink" />
      </CommandUIDefinitions>
    </CommandUIExtension>
  </CustomAction>


Step two, add a new Ribbon button to the Page tab: To perform this, we need to include in our Element.xml file another Custom Action. Again, I won't bore you with feature generation, so here's the code needed to add the button.

Few things to note about this code - I depended on Javascript (don't ask, I prefer it to jQuery). The clientside code here simply reads the current page and captures the content within the content area for the Wiki. Once that's established, I copy those values to variables in my modal dialog window, making them available to the child window.

<CustomAction
    Id="EmailContentButton"
    Location="CommandUI.Ribbon" 
    Sequence="5" 
    Title="Email">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.WikiPageTab.Share.Controls._children">
          <Button 
          Id="Ribbon.WikiPageTab.Share.EmailContentButton"
          Alt="E-Mail Page Content"
          Sequence="5"
          Command="EmailPageContent_Button"
          Image32by32="/_layouts/1033/images/formatmap32x32.png" Image32by32Top="-128" Image32by32Left="-448"
          Image16by16="/_layouts/1033/images/formatmap16x16.png" Image16by16Top="-16" Image16by16Left="-88"
          LabelText="E-Mail"
          TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
        Command="EmailPageContent_Button"
        CommandAction="javascript: 
         function GetEmailContent()
          {
            var objMainDiv = document.getElementById('ctl00_PlaceHolderMain_WikiField');
            var params = new Object();
            var options;
            params.pageTitle = document.title;
            params.parentUrl = window.location.href;
            params.pageContent = '';
             
            // if regular wiki 
            if(objMainDiv != 'undefined' && objMainDiv != null)
            {
             var objTable = objMainDiv.getElementsByTagName('table');
             
             if(objTable[0] == 'undefined' || objTable[0] == null)
             {
                if(confirm('Could Not Detect Wiki Content.\nE-Mail Page Link?'))
                    location.href='mailto: ?subject='+document.title+'&body=' + escape(window.location.href);
                return;
              }
              var objDiv = objTable[0].getElementsByTagName('td');
              if(objDiv[0] == 'undefined' || objDiv[0] == null)
             {
                if(confirm('Could Not Detect Wiki Content.\nE-Mail Page Link?'))
                    location.href='mailto: ?subject='+document.title+'&body=' + escape(window.location.href);
              }
             
             for(var i=0;i<objDiv.length;i++)
             {
                if(objDiv[i] != 'undefined' && objDiv[i] != null)
                  params.pageContent += objDiv[i].childNodes[0].innerHTML;
             }
            }
            else
            {// else check for enterprise wiki 
              objMainDiv = document.getElementById('ctl00_PlaceHolderMain_PageContent__ControlWrapper_RichHtmlField');
              
              if(objMainDiv != 'undefined' && objMainDiv != null)
              {
                 params.pageContent += objMainDiv.innerHTML;
              }
              else
              {
                 if(confirm('This Wiki Has No Content.\nE-Mail Page Link?'))
                    location.href='mailto: ?subject='+document.title+'&body=' + escape(window.location.href);

                 return;
              }
            }
            
            options = { 
                url:'{SiteUrl}/_layouts/EmailWikiContent/EmailPageContent.aspx',
                title: document.title,
                width:550,
                height:650,
                args: params};
                SP.UI.ModalDialog.showModalDialog(options);
                }
                GetEmailContent();" />
              </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>


Ok, so I hinted that there may be a step three involved. In my project, I included code to perform runtime configurations for the E-Mail form; such as enabling/disabling the subject and body fields. In any case, you have to add another Custom Action. Here goes, Step Three:

This code adds the link to my custom configuration Page to the Site Administration section of Site Settings.

<CustomAction
    Id="ModifyFeatureMailToProperty" 
    GroupId="SiteAdministration" 
    Location="Microsoft.SharePoint.SiteSettings" 
    Title="E-Mail Wiki Page Settings" 
    Rights="ManageWeb"
    Description="Adjust the Default E-Mail Settings for My Custom Wiki Page Content E-Mails">
    <UrlAction Url="_layouts/EmailWikiContent/configureCustomEmailForm.aspx" />
  </CustomAction>

Cheers!

No comments:

Post a Comment