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 been researching how to use the SPSite site = SPContext.Current.Site reference to pull the current site name, but I am unsure how to call this in a content editor web part. All I need is the site "Title" displayed, not the URL, with the end goal being to use CSS formatting to create (nearly) on the fly banners for any new sites. I will be testing on SP 2007 but will ultimately be using on 2010, most likely linking web parts to .txt file to maintain formatting.
Thanks

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

document.getElementsByTagName('title')[0].innerHTML='yourTitle'

http://stackoverflow.com/questions/10835634/add-site-title-to-page-using-cewp

to add the above javaScript:

<script language="JavaScript">

document.getElementsByTagName('title')[0].innerHTML='yourTitle';

</script>

or use

<script>

document.write(document.all["onetidTitle"].innerText);

</script>

I dont know if im right I could be wrong but you could try the above and see if work which I think it should, sorry cant test it as I havent got acess to my sharepoint site! All you need to do is copy the code above and paste into the cewp.

http://sharepointsolutions.blogspot.co.uk/2007/07/adding-javascript-to-content-editor-web.html

http://www.ehow.com/how_10012230_use-javascript-sharepoint.html

EDIT

Ok, yes you are missing somthing ;) its not going to show anything as your not putting it into anything! all your doing is getting the title :) what you need to do to test it is encapsulate it with an alert to show you what it returns. For your case obviously you want it in a label to display within the cewp i presume. Please follow this guide first as its not great to add javascript straight into the cewp so you can do the following:

http://techtrainingnotes.blogspot.co.uk/2012/02/sharepoint-how-to-add-javascript-to.html

This way you can link to an external file that contains the javascript that you can edit properly in sharepoint designer or notepad++.

Now for the code :) try this for now in the cewp, I know this works as iv just tested it out:

alert(document.getElementsByTagName('title')[0].innerHTML);

.......ok so now I know its working what i did was create a label and added it to the page ;) like so...

var GetSiteTitle = document.createElement("span"); 

GetSiteTitle.id = "span1"; 

GetSiteTitle.className ="dynamicSpan"; 

GetSiteTitle.innerHTML =  document.getElementsByTagName('title')[0].innerHTML;

document.body.appendChild(GetSiteTitle); 

Now you need one last part as i stated at the top you need to add the Javascript tag like so:

<script type="text/javascript" language="javascript"> 

var GetSiteTitle = document.createElement("span"); 

GetSiteTitle.id = "span1"; 

GetSiteTitle.className ="dynamicSpan"; 

GetSiteTitle.innerHTML =  document.getElementsByTagName('title')[0].innerHTML;

document.body.appendChild(GetSiteTitle); 

</script>

Now this is working code that i know works as iv just tested it ;) hope you like it! just copy past the last section of code as its exactly what you want! Dont forget to follow the link to add the JS in a seperate file once you have tested on cewp. Otherwise youll be adding a version every time to the dom over and over again what really bad and would come into other problems :)

Ok tested again and this works 100%, before it was loading before the page loads causing an error, to solve this i just made it wait till the page loads:

<script type="text/javascript" language="javascript">  

window.onload=func1;
 function func1() {
var GetSiteTitle = document.createElement("span");  

GetSiteTitle.id = "span1";  

GetSiteTitle.className ="dynamicSpan";  

GetSiteTitle.innerHTML =  document.getElementsByTagName('title')[0].innerHTML; 

document.body.appendChild(GetSiteTitle);  
 }

</script> 

This should show up at the bottom half of the page but you can apply css to it using its class name dynamicSpan!

Edit Only show part of the title

To only show the second part of the title do the following:

<script type="text/javascript" language="javascript">  

window.onload=func1;
 function func1() {

var GetSiteTitle = document.createElement("span");   

GetSiteTitle.id = "span1";   

GetSiteTitle.className ="dynamicSpan";   

var str= document.getElementsByTagName('title')[0].innerHTML;

var n=str.split("-");

GetSiteTitle.innerHTML = n[1] ;  

document.body.appendChild(GetSiteTitle); 
 }

</script> 

If you want to show the first part of the title just change

GetSiteTitle.innerHTML = n[1] ;

to

GetSiteTitle.innerHTML = n[0] ;

I hope this helps :)

share|improve this answer
    
Thank you but do I need a specific DocType declaration in order for this to work or a JavaScript tag or something else? I still either get the code itself or nothing at all depending on what else I put in the source editor. –  S0L1D Aug 20 '12 at 19:41
    
Iv changed the answer and hope it helps :) –  ali Sharepoint Aug 20 '12 at 21:45
    
I have attempted to use both the language/type javascript tags without any luck. The code never returns anything to display? I have verified the site column is indeed called 'title'... Thank you for your help, do you think I need to use 'document.write' to display? Or is there something else I am missing? –  S0L1D Aug 21 '12 at 15:57
    
Ok made changes and I hope you like it :) –  ali Sharepoint Aug 21 '12 at 21:21
    
Thank you very much for your help. I was able to get the alert working on both 2007 and 2010 by file linking, so I know your code is functional... However there is probably something on the SharePoint admin side that is preventing this from displaying the site name... I have used JavaScript many times in CEWP's, so i'm baffled why this doesn't work. I will have to discuss with the SP server admin about this. But I am marking this question answered since I know your code is correct, thank you again! –  S0L1D Aug 22 '12 at 15:37
show 4 more comments

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.