Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have list View webpart in my sharepoint 2010 team site home page. Generally Web Part title can be easily modified by editing the webpart and providing Title value. I am looking for a solution to display the Day of the week using javascript dynamically.

Thank You

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

I have added a content editor webpart and a list webpart in a blank webpart page and then only used JQuery to implement the same. Please check provided code.

JQuery: I have added the below mentioned JQuery in a simple text file and uploaded in document library. After that i used it in content editor webpart. You might have to modify the Id's according to your website.

<SCRIPT type=text/javascript src="/Documents/jquery-1.4.2.min.js"></SCRIPT>
<SCRIPT type=text/javascript>
$(document).ready(function(){         
$('#WebPartTitleWPQ2 h3 a nobr span').text('');
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var dayofweek  = weekday[new Date().getDay()];
$('#WebPartCaptionWPQ2').text(dayofweek);
});
</SCRIPT>
share|improve this answer
    
That Worked Thank You very Much!!!!! –  octopus May 24 '13 at 20:58
add comment

You can insert the following JavaScript into the page can be in a Content Editor Web Part or what you want:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
jQuery.noConflict();
jQuery(document).ready(function($){
    var daynum = new Date().getDay();
    var dayArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var day = dayArray[daynum];
    $(".ms-WPTitle span:contains('Shared Documents')").text(day)
});
</script>

You can replace the url to jquery with what your preferred version or remove it if it's already in your master page.

You can replace "Shared Documents" with the title of the web part you want replaced with the day of week

share|improve this answer
add comment

Make few changes to this code and add it to your site collection master page and it should work. I didn't tested it yet, so you better go through code before implementing it.


var siteUrl = '/sites/MySiteCollection';
var serverRelativeUrl = '/sites/MySiteCollection/Default.aspx';

function updateWebPartTitle() {

    this.clientContext = new SP.ClientContext(siteUrl);
    var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);

    var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
    this.collWebPart = limitedWebPartManager.get_webParts();

    clientContext.load(collWebPart);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.changeTitle), Function.createDelegate(this, this.onQueryFailed));
}

function changeTitle() {

    if (!collWebPart.get_count()) {
        alert('No Web Parts on this page.');
    }

    var oWebPartDefinition = collWebPart.get_item(2);
    this.oWebPart = oWebPartDefinition.get_webPart();



    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'/'+dd+'/'+yyyy;

    oWebPart.set_title(today);

    oWebPartDefinition.saveWebPartChanges();

    clientContext.load(oWebPart, 'TitleUrl');

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Title changed for Web Part: ' + this.oWebPart.get_titleUrl());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


Note:

Here's the references I picked this code from,

How to: Work with Web Parts on a Page Using JavaScript

Get Current DateTime using JS

share|improve this answer
    
Hello Muhammad, can i plug this code in Content editor webpart on a page inside javascript tags. How do i target one specific webpart? –  octopus May 24 '13 at 16:26
    
this will only work if the person accessing the page has owner or designer permissions. –  Christophe May 24 '13 at 17:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.