| | Stumble It! | Add to Mixx! | | diigo it | | Slashdot |

Wednesday, January 28, 2009

SharePoint Tooltip Popup Preview - Round 2

*** UPDATED 18 Feb 09 re: Stout's comment ***
(see the bottom of this post)

A little over a month ago I posted an article about how to include popup previews into SharePoint calendars with the caveat that it was a more complex solution than I typically post. In the course of my last few projects, I've stumbled across better ways to accomplish this in a much cleaner way which I'll address here.

This is a two-step process and will require that you to have access to SharePoint Designer if you want it to look clean and professional.

Step 1 - Create the Popup page
  1. From within SharePoint Designer, create a new document.
  2. Insert > SharePoint Controls > List Form
  3. In the next window, select the list that you want to create the popup for and make sure that the "Display" button is selected and that the "Show standard toolbar" box is unchecked.
  4. Next, customize the page to suit your tastes, but you'll need to either set the width of the page to 450px or tweak the code below to match whatever width you specify.
  5. Save this new page and remember where because you'll be using that address in the code below. This example assumes that the file name is "popUpDispForm.aspx".
Step 2 - Add the Code
  1. Make any changes to the code as required by steps 4 & 5 above.
  2. Open the list that you're working with for editing in your browser.
  3. Add a Content Editor Web Part (CEWP) to the bottom of the page.
  4. Open the "Source Editor..." window for the CEWP and add the following code:
    <div id="popUp" onMouseOut='popDown()'
    style="position:absolute;display:none;">
    <iframe id="popUpFrame" src="" frameborder="0px"
    style="width:450;
    height:300;
    border-style:dashed;
    border-width:1px;
    border-color:black;
    position:absolute;
    top:0px;
    left:0px;">
    </iframe>
    </div>
    <script type="text/javascript">
    var popUp = document.getElementById("popUp");
    var theAs = document.getElementsByTagName("A");
    var browser = navigator.appName;
    var a = 0;
    while (a < theAs.length) // insert onMouseOver event into the Title column links
    { try
    { if (theAs[a].href.search("DispForm.aspx") > 0 && theAs[a].href.search("SPBookmark") < 0)
    { theAs[a].setAttribute("myId", theAs[a].href.substr(theAs[a].href.search("ID=")+3));
    if (browser == "Netscape") { theAs[a].addEventListener('mouseover', toolTip, false); }
    else { theAs[a].attachEvent('onmouseover', toolTip); }
    }
    }
    catch(err){}
    a+=1;
    }

    function popDown()
    { popUp.style.display = "none"; }

    function toolTip(e)
    {
    var x = (browser == "Netscape") ? e.target.getAttribute("myId") : e.srcElement.getAttribute("myId");
    url = "http://path/to/popUpDispForm.aspx?ID=" + x;
    document.getElementById("popUpFrame").src = url;
    popUp.style.top = e.clientY - 5;
    popUp.style.left = e.clientX + 5;
    popUp.style.display = "";
    }
    </script>
  • This code will look through all the links on the page and for each one that points to "DispForm.aspx" it will add the popup when you hover the mouse over the link.
Enjoy and as always, I'm happy to help with any implementation troubles you may encounter!

*** UPDATE FOLLOWS ***
To answer Stout's question in the comments section, it is possible to have the "EditForm.aspx" popup in the tooltip instead of "DispForm.aspx", though I found formatting this form was a bit trickier.

To do this, follow the same steps above, but with the following modifications:

Step 1 -
  1. No changes.
  2. No changes.
  3. Select "Edit Item Form" instead of "Display Item Form".
  4. Use the following CSS to help format your form:
    <style type="text/css">
    .ms-formlabel {
    font-family: "Courier New", Courier, monospace;
    font-size: x-small;
    font-weight: bold;
    width:50px;
    overflow:hidden;
    }
    .ms-formbody {
    font-family: "Courier New", Courier, monospace;
    font-size: x-small;
    }
    .ms-descriptiontext {
    font-family: "Courier New", Courier, monospace;
    font-size: xx-small;
    }
    .ms-standardheader {
    font-family: "Courier New", Courier, monospace;
    font-size: x-small;
    }
    .ms-toolbar {
    font-family: "Courier New", Courier, monospace;
    font-size: xx-small;
    }
    </style>
  5. Use the file name of "popUpEditForm.aspx" instead of "popUpDispForm.aspx".
Step 2 -
  1. No changes.
  2. No changes.
  3. No changes.
  4. Make the following substitutions in the Javascript code to point to the new form:
    function toolTip(e)
    {
    ...
    url = "http://path/to/popUpDispForm.aspx?ID=" + x;
    ...
    }
    is changed to
    function toolTip(e)
    {
    ...
    url = "http://path/to/popUpEditForm.aspx?ID=" + x;
    ...
    }

22 comments:

Stout said...

Can you attach it the popup to EditForm.aspx instead of DispForm.aspx?

Ben said...

Stout,

If I understand your request, you'd like EditForm.aspx to be in the pop-up tooltip instead of DispForm.aspx? It should work, but I can't test it at the moment as I've just left work and am headed home.
I'll try this out tomorrow and will post the results then.

-Ben

Ben said...

Hi Stout,

Turns out that it is possible to use the EditForm.aspx in the tooltip!

-Ben

cris said...

This a good approach...
i will be trying to do it with the NewForm.aspx

:D

Amit Jha said...

hi,
i have tried this approach to display the tooltip in calendar. but its not comming for me. please help me to display the tool tip in calendar.

Thanks

Ben said...

Hi Amit,

What exactly do you need help with?

-Ben

Amit Jha said...

Hi ben,

i have default sharepoint calendar. i just want to show the tool tip on mouse over for the particular event date. Like if i have event on 21st november, i just want to show the title of that event on mouse over.


Thanks
Amit

Amit Jha said...

Hi Ben,

what i did exactly is,

i have created a page popUpDispForm.aspx using the steps you described. I have used the calendar List.

Then i went to the calendar list. Url is something like
http://Sharepoint:1234/Lists/Calendar/Allitems.aspx

here i have added a Content editor web part. In that web part in source editor i have added the code given by you.

After that i didn't get any pop-up for the title hyperlinks in that page.

please help.

-Amit

Ben said...

Hi Amit,

It's been a while since I wrote this code and I've since found a few techniques that are a bit cleaner so I just re-wrote the code. Step 1 is all still the same, but the code below should produce a popup window with the form you created in SPD.

<div id="popUp" onMouseOut='javascript:this.style.display="none";' style="position:absolute;display:none;">
<iframe id="popUpFrame" src="" frameborder="0px"style="border-style:dashed;border-width:1px;border-color:black;"></iframe>
</div>

<script type="text/javascript">
var h = 300; // customize this to your form height
var w = 450; // customize this to your form width
var url = "http://path/to/popUpDispForm.aspx?ID="; // customize this to your form URL

var events = document.getElementsByTagName("td");
for (var e in events) {
if (events[e].className == "ms-cal-monthitem") {
var evId = events[e].firstChild.href.substr(events[e].firstChild.href.search(/id=/i)+3);
events[e].innerHTML = "<div onMouseOver='popUp(\""+evId+"\");'>"+events[e].innerHTML+"</div>";
}
}

function popUp(x) {
var pFrm = document.getElementById("popUpFrame");
pFrm.src = url + x;
pFrm.style.height = h+"px";
pFrm.style.width = w+"px";

var x = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
var y = window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight;
var pDiv = document.getElementById("popUp");
pDiv.style.top = ((y/2)-(h/2))+"px";
pDiv.style.left = ((x/2)-(w/2))+"px";
pDiv.style.display = "";
}
</script>

All you'll have to modify is the first 3 lines of the <script> to match the height, width, and URL to your form that you created in SPD.

-Ben

Amit Jha said...

Thnaks Ben. You saved me. Its working Great. Now i am getting the Pop ups.

Just couple of doubts.First thing is pop up is not comming if the event is for more than one day. Like if the event in calendar is showing 14 nov to 16 nov then in this case pop up is not comming.

Second thig is can we adjust the pop-up window display. like if i want it to display always in the center of the page.

Thanks again ben.really appreaciate your help.

Thanks
Amit

Amit Jha said...

Hi Ben,

i have got the solution for my first doubt. I have just added one more if condition in the javascript. code is like this:

if (events[e].className == "ms-cal-defaultbgcolor")
{

//same stuff as yours

}

now the pop up is comming also, if the event is for more than one day.


looking for option if we can display the pop up window in the center of the page.

Thanks once again Ben.


-Amit

Amit Jha said...

Hi ben,

I have resolved that problem also. Just added the y and x co-ordinate in the popup function.

Thanks once again for your support.


Thanks
Amit

Ben said...

Hi again Amit,

Awesome, I'm glad it's working for you!

-Ben

Alex said...
This comment has been removed by the author.
Alex said...
This comment has been removed by the author.
Alex said...

Ben, amazing work! I had issues with having multiple instances of your code for different lists - I resolved this simply by creating a "popupdispform.aspx" for each list and putting the relevant code in the same web part zone as the list. I also had issues of "modifying" dispform.aspx, so all I did was Insert -> Custom List Form (instead of List form) and edited the form there. Thanks again for great code and hard work!

Alex said...

Ben.
Just wondering if we can make the height adjust automatically depending on the amount of information in the detail view. I've been playing around with your code as well as the custom list form width's and height but keep getting scrollbars.
Thanks!

Ben said...

Hi Alex,

Unfortunately I'm unaware of a way to dynamically size the popup to match the size of the content on the popup page. Sorry

-Ben

Patrick said...

Hi Ben,

This is exactly the solution that I've been searching for but I'm looking to apply this to other types of SharePoint lists used for project management (tasks, issues, risks, etc.). Do you have any thoughts on how to modify the code to support other list types?

Thanks,

-Patrick

Ben said...

Hi Patrick,

The code here will look through every link on the page and when it finds one that points to "DispForm.aspx", it inserts the tool-tip popup code. So as long as the list you're using has links that point to "DispForm.aspx", it should work.

If it's not, please feel free to send me the .aspx page that you're working with and I'll troubleshoot it to get it working.

ben[at]bradleyit.com

-Ben

Princy said...

Hi,

I am trying to put popup for announcement list. I tried the steps above but no popup shown.

Added CEWP in the
"--List/Notice/allitems.aspx" of the announcement list called Notice. Changed the Path of 'popupdisplayform'.

Can you help me?

Thanks in advance

Sabin said...

Hello Ben i am trying to apply this into Sharepoint 2010 custom lists. Is it compatible for sharepoint 2010.

Thank You