Wednesday, March 28, 2012

How To: Add Audience Field Control to SharePoint 2010 Publishing Page Layout

I just recently needed to add in the default audience field control to a publishing page layout. So that when you edit the page you can easily specify the target audience for the page without having to go to “Edit Properties” in the ribbon. The only issue is that it is not as simple as you would think. In my previous post I provided detail on how to Hide SP 2010 Page Layout Metadata – WebControls however the “Audience” field control requires some additional references to the Microsoft.Office.Server.UserProfiles field types.

I found this blog here: Showing the Audience Target field in an EditModePanel #SharePoint #SP2010 #in #ProjectServer #PS2010 that really helped with solving my problem.

So basically to add in the audience field control to the page in edit mode you would simply have to do the following within your custom page layout.

  1. Add in the following to the reference section near the top of your page layout.
    <%@ Register TagPrefix="OfficeWebControls" Namespace="Microsoft.Office.Server.WebControls.FieldTypes" Assembly="Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  2. Then add in the following code within the edit mode panel control.
    <OfficeWebControls:SPFieldTargetToControl ID="Audience" runat="server" FieldName="Audience" />

If you do not already have a hidden menu control on the page you can simply add the following:

<PublishingWebControls:editmodepanel runat="server" id="editmodepanel1">
<OfficeWebControls:SPFieldTargetToControl ID="Audience" runat="server" FieldName="Audience" />
</PublishingWebControls:editmodepanel>

Thanks Giles Hamson for your post!

Monday, March 26, 2012

How To: Add Time/Date to Layouts or Master Page

If you ever needed to add in the local time and a date for the logged in user to your custom page layout or master page below are some ways that you can achieve this.

Option #1
The first method uses a SharePoint Portal Web Control to display the logged in users current time. This control is normally used on the profile pages to show current time for the profile that you are viewing. One benefit that you get from this approach is that if you have users that view the site from multiple locations around the world they would see the current time based on the time zone that they have set in their my site Profiles.

To add this web control to your master page do the following:

  1. Add in the following registration to the top of the master page:
    <%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
  2. Add in the the following code right below the welcome control “IdWelcome” DIV in your master page.
    <div class="currenttime">
        <SPSWC:ProfilePropertyLoader runat="server"/>
        <SPSWC:LocalTimeControl runat="server" />
    </div>
  3. Then add in some quick styles to your custom style sheet so that the time is set to white.
    .currenttime{
        color: #FFF;
        text-align: right;
        font-family: Arial, sans-serif;
        font-weight: normal;
        font-size: 12px;
    }

Your site should look something like this:
image

Notice the time below the users name. Now if the logged in user changes the time zone in their profile it will automatically update the time on all the pages that use this master page.
image

I am not aware of any web control that displays the date/month/year. If you know of one please reply to this post.

Option #2
The second option is that you could use JavaScript within your page layouts or in your master page to display the current time and date of for the logged in user. The time zone is based on the settings from the computer that is accessing the site.

image

To add this JavaScript to your page layout do the following:

  1. Add in the following code anywhere within your page layout:
    <table cellpadding="0" cellspacing="0">
        <tr>
            <td class="company-homepagetime" valign="top">
                <script type="text/javascript">
                    function getClockTime()
                    {
                        var now    = new Date();
                        var hour   = now.getHours();
                        var minute = now.getMinutes();
                        var second = now.getSeconds();
                        var ap = "<span class='company-homepagetime-ampm'>AM</span>";
                        if (hour   > 11) { ap = "<span class='company-homepagetime-ampm'>PM</span>";}
                        if (hour   > 12) { hour = hour - 12;      }
                        if (hour   == 0) { hour = 12;             }
                        if (minute < 10) { minute = "0" + minute; }
                        if (second < 10) { second = "0" + second; }
                        var timeString = hour +
                            ':' +
                            minute +
                            " " +
                            ap;
                        return timeString;
                    } // function getClockTime()
                        var clockTime = getClockTime();
                        document.write(clockTime);
                </script>
            </td>
        </tr>
        <tr>
            <td class="company-homepagedate" valign="top">
                <script type="text/javascript">
                    var months=new Array(13);
                    months[1]="January";
                    months[2]="Febuary";
                    months[3]="March";
                    months[4]="April";
                    months[5]="May";
                    months[6]="June";
                    months[7]="July";
                    months[8]="August";
                    months[9]="September";
                    months[10]="October";
                    months[11]="November";
                    months[12]="December";
                    var day=new Date();
                    var lmonth=months[day.getMonth() + 1];
                    var date=day.getDate();
                    var year = day.getFullYear();
                    document.write(lmonth + " " + date + ", " + year);
                </script>
            </td>
        </tr>
    </table>
  2. Then Add in the following CSS to give it a little style
    .company-homepagetime{
        color: #000;
        font-family: Arial, sans-serif;
        font-weight: normal;
        font-size: 30px;
        text-align: left;
        padding: 10px 0px 0px 15px;
    }
    .company-homepagetime-ampm{
        color: #000;
        font-family: Arial, sans-serif;
        font-weight: normal;
        font-size: 20px;
        text-align: left;
    }
    .company-homepagedate{
        color: #000;
        font-family: Arial, sans-serif;
        font-size: 14px;
        text-align: left;
        text-transform:uppercase;
        padding: 0px 0px 0px 17px;
    }

The result should look similar to the following:

image

You could add in a content placeholder in your master page above the left navigation, or in another location so that the time can be displayed anywhere you want.

I think the second option that is using JavaScript is a lot more flexible and allows you to customize the display and format of the time and date. For example if you wanted to truncate the months to three characters all you would have to do is simply type in what it should display in the code.
Example:(Change “February” to “Feb” or “November” to “Nov”).

Post a comment if you found this useful or have any other type of solution that worked for you.

Thanks for listening,

Erik Swenson (Author)
My Book: Practical SharePoint 2010 Branding and Customization