Posts Tagged ‘Development’

Add Alerts to All Sites

Wednesday, May 13th, 2009

I recently had a request from a client that would allow them to automatically display a Notification on all Sites and Pages.  This was easy enough to do because I was already stapling a custom page layout to the publishing infrastructure along with a custom Master Page and CSS.  The Master Page was altered to include a pointer to a hidealert.css file residing in a document library I had created on the root site.  The Page Layout was then altered to include an IFrame that loaded an HTML file called alert.htm sitting in the same document library on the root site.  When the editor renamed the file to something other than hidealert.css (recommended to rename it to showalert.css) the IFrame would appear with the alert.htm contents.  Below are the files, description and code:

Master Page – Load hidealert.css

Description: Custom Master Page that had one additional CSS file being loaded to easily turn the Alert visibility on or off.  This also enabled it to be done by a content editor with no IT involvement.

<link rel="stylesheet" type="text/css" href="/design/hidealert.css">

Page Layout

Description: Code to Load IFrame.  The code was placed above one of the web part zones so they would dynamically slide down if the IFrame loaded.

<iframe src="/design/alert.htm" class="alert" scrolling="no" frameborder="0">
<a href="/design/alert.htm">SharePoint Requires Internet Explorer 5.5 SP2 or higher.</a>
</iframe>

hidealert.css

Description: CSS to Hide Alert IFrame When Not In Use

.alert
{
 display:none;
}

alert.htm

Description: HTML to Appear in IFrame (includes javascript to resize IFrame to match)

<html>
<head>

<script type="text/javascript">
        function resizeToFit(that){
                var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
                var objIframe = window.parent.document.getElementById('resizeToFit');
                var intScrollX, intScrollY, intWinWidth, intWinHeight, intMaxWidth, intMaxHeight;
               
                var intWhileCount = 0;
                do{
                    // scroll the document by 1 pixel
                    window.scrollTo(1,1);
                    // measure the scroll position           
                    intScrollX = (document.all) ? document.body.scrollLeft : window.pageXOffset ;
                    intScrollY = (document.all) ? document.body.scrollTop : window.pageYOffset ;
                    // measure window size
                    intWinWidth = (document.all) ? document.body.offsetWidth : window.innerWidth ;
                    intWinHeight = (document.all) ? document.body.offsetHeight : window.innerHeight ;
                    // if the scroll position is not 0
                    if(intScrollY>0){
                        // make the window larger
                        window.resizeBy(0,32);
                        // make the iframe larger
                        if(objIframe!=null && !document.all) objIframe.style.height = (objIframe.style.height=='') ? '64px' : (parseInt(objIframe.style.height) + 32) + 'px';
                    }
                    // count the steps
                    intWhileCount += 1;
                }while((intScrollX>0 || intScrollY>0) && intWhileCount<900);
            }
            function waitToFit()
            {
                setTimeout("resizeToFit()",100);   
            }
           
            onload = waitToFit;
</script>

</head>
<body onload="resizeToFit();">
<!– Make Change Below This Line –>

This is a test

<!– Make Changes Above This Line –>
</body>
</html>

The content editor was given instructions to rename the CSS file from hidealert.css to showalert.css and add any custom HTML code between the <Make Changes> comment lines in the alert.htm.

Jeff

 

Hide Custom Actions

Saturday, January 31st, 2009

I recently learned about hiding items in SharePoint using the HideCustomAction element.  I had requests to hide items such as the Site Theme, Site Features, and Site Collection Features in Site Settings (I hide all three in my example below).  It is as easy as creating a feature with two XML files.

NOTE: This will remove the links only.  Users will still be able to manually browse to the pages if they know the full URL.  This is not a replacement for security!

 Feature.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Feature Id=”af6215af-f836-4a09-aa3b-8685981d478f
Title=”Hide Custom Action Links”
Description=”This feature currently hides: site features, site collections features, and site themes.”
Version=”1.0.0.0″
Scope=”Site”
xmlns=”http://schemas.microsoft.com/sharepoint/”>
  <ElementManifests>
    <ElementManifest Location=”HideCustomActions.xml” />
  </ElementManifests>
</Feature>

HideCustomActions.xml

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
  <HideCustomAction
  GroupId=”SiteAdministration”
  HideActionId=”ManageSiteFeatures”
  Location=”Microsoft.SharePoint.SiteSettings”
/>
  <HideCustomAction
  GroupId=”SiteCollectionAdmin”
  HideActionId=”ManageSiteCollectionFeatures”
  Location=”Microsoft.SharePoint.SiteSettings”
/>
  <HideCustomAction
  GroupId=”Customization”
  HideActionId=”Theme”
  Location=”Microsoft.SharePoint.SiteSettings”
/>
</Elements>

John Holliday has a great list of all of the Custom Action IDs on his site: http://johnholliday.net/resources/customactions.html

Jeff

Programmatically Apply a Master Page and Theme to Specific Managed Path

Tuesday, January 27th, 2009

I recently had a request from a client that required programmatically applying a master page and theme to all new site collections (and sub sites) under a specific managed path.  I created a feature to deploy the master page and one to automatically apply the master page (ensynch.master) and theme (ensynch_theme) on site creation.  This was done by stapling the feature at the farm level to the required site definitions.  Using an IF statement I was able to apply the master page and theme settings to all site collections (and sub sites) created under the managed path.  In the example code below the managed path is called MANAGEDPATH.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace Ensynch.SetCustomMaster
{
public class ChangeMaster : Microsoft.SharePoint.SPFeatureReceiver
{
public override void FeatureInstalled
(SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling
(SPFeatureReceiverProperties properties)
{
}
protected string EndOfURL(string url)
{
    return url.Substring(url.LastIndexOf(“/”) + 1);
}
        public override void FeatureActivated
        (SPFeatureReceiverProperties properties)
        {
            SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;
            if (CurrentWeb.ServerRelativeUrl.StartsWith(“/MANAGEDPATH”))
            {
                if (CurrentWeb.IsRootWeb == true)
                {
                    CurrentWeb.MasterUrl = CurrentWeb.ServerRelativeUrl + “/_catalogs/masterpage/ensynch.master”;
                    CurrentWeb.CustomMasterUrl = CurrentWeb.ServerRelativeUrl + “/_catalogs/masterpage/ensynch.master”;
                }
                else
                {
                    CurrentWeb.MasterUrl = “/MANAGEDPATH/” + EndOfURL(CurrentWeb.Site.Url) + “/_catalogs/masterpage/ensynch.master”;
                    CurrentWeb.CustomMasterUrl = “/MANAGEDPATH/” + EndOfURL(CurrentWeb.Site.Url) + “/_catalogs/masterpage/ensynch.master”;
                }
                CurrentWeb.ApplyTheme(“ENSYNCH_THEME”);
                CurrentWeb.Update();
                CurrentWeb.Dispose();
            }
        }
        public override void FeatureDeactivating
        (SPFeatureReceiverProperties properties)
        {
            SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;
            CurrentWeb.MasterUrl = CurrentWeb.ServerRelativeUrl + “/_catalogs/masterpage/default.master”;
            CurrentWeb.CustomMasterUrl = CurrentWeb.ServerRelativeUrl + “/_catalogs/masterpage/default.master”;
            CurrentWeb.ApplyTheme(“none”);
            CurrentWeb.Update();
            CurrentWeb.Dispose();
        }
    }
}

Jeff

Hide Edit Page in Site Actions Menu

Monday, January 14th, 2008

I had a request from a client to hide the Edit Page option under the Site Actions menu for all users without Full Control permissions.  I performed the following steps to remove the edit page option for user’s without the managesubwebs right.

  1. Open the master page for the site.
  2. Find the following lines of code:

           <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=”BLOCKED SCRIPTMSOLayout_ChangeLayoutMode(false);”
            />

  3. Add to the following lines to the code:
            PermissionsString=”ManageSubwebs”
            PermissionMode=”Any”

  4. 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=”BLOCKED SCRIPTMSOLayout_ChangeLayoutMode(false);”
            PermissionsString=”ManageSubwebs”
            PermissionMode=”Any”
            />
  5. Save the master page and login with an account that does not have Full Control, but is not read only either…  The Site Actions drop down should now resemble:

Hide Edit Page