开发者

Add Today's Date to SharePoint Master Page

开发者 https://www.devze.com 2022-12-24 07:03 出处:网络
Surely there must be a simple way of 开发者_C百科putting a simple code block into a master page :( I\'ve tried using the obvious <%= \"Hello, World!\" %> syntax but code blocks aren\'t allowed.T

Surely there must be a simple way of 开发者_C百科putting a simple code block into a master page :( I've tried using the obvious <%= "Hello, World!" %> syntax but code blocks aren't allowed. Then tried a site column, but don't know how to use them. Then tried web zones but master pages can't use them. Tried putting a web part (which are super difficult to make and deploy btw) into the page layout, but it just doesn't render :/

All I want is something nice and simple at the top of my site which shows today's date and the format I want to use is DateTime.Today.ToString("ddd, d MMMM yyyy"). How do I do it?

(Otherwise I'm resorting to javascript document.write!)

Thanks all,


The recommended solution to this would be to use a server-side control. As you say, developing a webpart for this seems like overkill. A simpler option would be to use a simple .ascx user control and then reference this on the page. This post explains how to add the user control to the Templates folder and then reference it in your master page.

Although for such a simple problem, I think your idea of JavaScript is actually ok. And it would allow you to show the current time in the user's timezone which wouldn't be as easy on the server-side. It does seem like a hack to use client-side code for this type of thing, but using JavaScript code in SharePoint is actually a really powerful tool.


Don't you get exception "Code blocks are not allowed in this file"?

Anyway, code in pages by default is not allowed. What would it end like if everyone who can edit pages with SharePoint designer would start putting some code inside?

If you choose so, edit web.conf:

<PageParserPaths> 
    <PageParserPath VirtualPath="/_catalogs/masterpage/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" /> 
</PageParserPaths>


A server side control is definitely the recommended solution for placing custom code on your Masterpage. This is more of a broad answer to encompass almost anything that you'd want to do. However, it's not always your first and last option.

In your situation, you wouldn't necessarily need to write a custom Web part or server control (and delve into the security and deployment needs). In my opinion, this is overboard. Since you already have access to the MasterPage, and a date/time is all that is required, you can use some simple JavaScript code:

var now = new Date();
document.write(now.format("ddd, d MMMM yyyy"));

This of course will show the client's date and time and not the servers. Not a huge deal in most cases since you can then utilize getTimezoneOffset() to fix any time zone discrepancy's.

0

精彩评论

暂无评论...
验证码 换一张
取 消