in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

helloitsliam's blog

MOSS2007 – JavaScript Item Menus Part 3 (Real World)

One of the things I have been asked recently has been to do with changing the “Email Link To” menu item. Sometimes this link works, sometimes it doesn’t. Before we can look at changing it let’s take a look at the code that is used to create this link. The code block can be found within the “AddSendSubMenu” function within the “CORE.JS” file. Here is the code block in full:

Now let’s step through it line by line. The first line:

This line is used to check that the current user has the correct permissions, i.e. Permission to the document or item. Next we simply populate the local variable “strDisplayText” with the globally declared variable from the top of the “CORE.JS”.

Now we get to the interesting bits. Each list and library when viewed within the standard pages such as “AllItems.aspx” can be accessed via JavaScript by using some built in functions. One of these is “GetAttributeFromItemTable”. This function is actually called from the “INIT.JS” file; along with a few more that you will see further down the code. This code line is connecting to the table and grabbing the “URL”, which in reality is the direct link to the document or item.

Once we have the actual link then we find the root URL (the server URL) using a built in property of the list item which is referred to as CTX.

Once we have the root URL it is then chopped up slightly, so we have the length, and location of the relevant slashes. The local variable “fileUrl” is also declared at this point, this is used later on.  Now we populate the local variable by concatenating the root URL and the variable called “currentItemUrl” which is actually populated previously from the item table.

Now we have the new variable “fileUrl” populated and ready to use, the next variable to get populated is the “serverFileRedir”. This is used to hold any redirection page that is needed as part of the menu item link.

Now that we have all the variables populated the next few lines of code simply build the menu item. The “strAction” variable contains the action that will happen when the relevant menu item is pressed. In this case it calls another function that exists within the “INIT.JS” file. This function simply encodes the relevant URL and does a “window.location” redirect. Notice also in the code we pass auto populate the body of the email with the newly populated variable “fileUrl”.

Next an image is assigned to the menu item. The images used for this are stored here:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\IMAGES

Now we put it all together using a built in function called “CAMOpt” which exists within the “CORE.JS”. The syntax for this is shown below but let me explain it here:

CAMOpt({menu or parent menu item}, {display name variable}, {code action variable}, {image variable})

Notice that in this example the first parameter is set as “sm”. This is the ID of the “Send To” menu item on the main item menu. Some of the functions refer to the variable called “m”. This is the main menu itself and gets populated from the function called “CreateMenuEx”. In one of the last posts we looked at creating nested menun items. To achieve this we made use of this method. Last but not least it assigns a global ID to the menun item. Thisn is great as you can then create custom CSS styles that map direct to the menu item.

So now we have stepped through the code let’s modify it slightly. One of the errors you sometimes get is caused by the fact that the JavaScript URL Encodes the path before it adds it to the body of the email. An example of this is when the main server URL gets encoded also and makes the link unusable from an email client but works in the browser.

http://intranet%2Elabs%2Elocal/StaffDirectory/Team%20Documents/DemoDoc1.docx

By modifying the code we could make sure that the first part of the URL does not get encoded. The URL would then look as below:

http://intranet.labs.local/StaffDirectory/Team%20Documents/DemoDoc1.docx

To make this work we need to add the following line(s) into the code:

Notice that in this line we are calling a built in function called “UnescapeProperly” around the “httpRootWithSlash” variable and then we are using the “EscapeProperly” around the “currentItemUrl”, both of these functions are called from the “INIT.JS”. Both of these functions simply replace any spaces or special characters with their HTML (ASCII) equivalent. Next we need to tweak the link that is rendered into the body of the email. The first variable “NewEmailLength” simply grabs the item table value called “DREF”. This is used to get the relative location of the item or document. The second variable takes the current item URL and substrings the length from the previous variable which leaves us with the actual name of the file plus the extension. The idea behind this was so the subject would read: “Emailing Document Link: {Document Name}“. The “NewEmailBody” variable is populated by the variable we created earlier called “fileUrlNonEscaped”.

You will also notice that the action variable is calling a function called “CreateMailMessage”. This is a simple function that just does a “window.location” redirect for the email. The function is:

Once it all together when you access the link the email now renders as:

As you can see by using lots of the inbuilt functions you are able to change the way that the default menu items or new menu items work within MOSS2007. As a side not I have just noticed that fellow MVP Ishai Sagi has posted his findings on what all the variables are used for from within the “itemTable”. Check his post here:

http://www.sharepoint-tips.com/2007/05/meanings-of-variables-in-context-menus.html

Hope this helps. Wink

Comments

 

Jens said:

Hi Liam,

two remarks:

1. Can't see any picture (image), my fault?

2. Do I have the possibility to find some older threads? I'm looking for the "MOSS2007 - Look and Feel" and "MOSS2007 – Rebuild Corporate Website" series.

Can you help, please?

Thanks in advance,

Jens

June 27, 2007 1:54 PM
 

Mark Wilson said:

This involves changing one of the shipping files - have you found a way to update these menus without directly modifying core.js? I suppose overriding that particular function in another js file is the way to go.....

August 28, 2007 3:55 PM
 

mozier said:

Can't see the pictures...

September 5, 2007 4:25 AM
 

mozier said:

For all those who haven't found it yet, and can't see the pictures - www.helloitsliam.com/.../moss2007-–-javascript-item-menus-part-1-real-world.aspx

September 5, 2007 6:21 AM
 

Oxy said:

Hey, still don't find the pictures on your blogs, I would need the picture of the last part, the picture of the adapted encode code.

Where can I find these?

Thx

December 3, 2007 4:37 AM
 

Arpad said:

Moziers's link is broken, no pictures.

December 4, 2007 4:39 AM
 

sugengnn said:

images don't seem to appear

January 24, 2008 9:15 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts