Search code examples
c#javascriptsharepoint-2007

Code does not work in content editor web part SharePoint 2007


I'm still trying to figure out this slideshow task that I need to display in a SharePoint 2007. In my other post folks suggested doing things that are more challenging that I'm not willing or have skills to take on. So what I have here is perhaps a simpler solution but it's not working either.

I'm using a content editor web part into which I copied the code that works in a stand alone web site but not here.

I was hoping you can tell me if this can work and suggest a fix or should I just scrap it all toghether and take on the prior challenge.

In the default.master I have Javascript like this:

<script language="javascript" type="text/javascript">
<!--
var slideimages=new Array()
var slidelinks=new Array()
function slideshowimages(){
  for (i=0;i<slideshowimages.arguments.length;i++){
    slideimages[i]=new Image()
    slideimages[i].src=slideshowimages.arguments[i]
  }
}

function slideshowlinks(){
  for (i=0;i<slideshowlinks.arguments.length;i++)
    slidelinks[i]=slideshowlinks.arguments[i]
}

function gotoshow(){
  if (!window.winslide||winslide.closed)
    winslide=window.open(slidelinks[whichlink])
  else
    winslide.location=slidelinks[whichlink]
  winslide.focus()
}               
//-->

Then in content editor web part I have this:

<script runat="server" >

protected void Page_Load(object sender, EventArgs e)
{        
    int iCounter = 0;
    string path = Server.MapPath("/Bulletin/");

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
    System.IO.FileInfo[] slides = di.GetFiles("*.jpg");
    iCounter = slides.Length;

    string mystring = string.Empty; 

    int i = 0;
    foreach (System.IO.FileInfo file in slides)
    {
        if (i < iCounter-1)
        {
            mystring += "'/Bulletin/" + slides[i].ToString() + "',";
            i++;
        }
        else
        {
            mystring += "'/Bulletin/" + slides[i].ToString() + "'";
        }
    }

    String scriptString = @"<script language=JavaScript>";

    scriptString += @"slideshowimages(" + mystring + @")
        slideshowlinks() // Still need to build this up
        var slideshowspeed = 10000
        var whichlink = 0
        var whichimage = 0
        function slideit() {
        if (!document.images)
        return
        document.images.slide.src = slideimages[whichimage].src
        whichlink = whichimage
        if (whichimage < slideimages.length - 1)
        whichimage++
        else
        whichimage = 0
        setTimeout('slideit()', slideshowspeed)
        }
        slideit()
         ";
   scriptString += @"</";
   scriptString += @"script>";

    if (!this.IsStartupScriptRegistered("clientScript"))
        this.RegisterStartupScript("clientScript", scriptString.ToString());
}

I have tried placing everything in the webpart. I have also placed the JavaScript in its own file, placed in a script directory in the root of my app and call it like that. I have also copied in the /_layout/1033 directory but none of these steps helped.

Thank you for your time!


Solution

  • <script runat="server" > You cannot execute server side code in content edit web part.