PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Using ASP in JavaScript for URL Encoding in SharePoint fields

Posted by PANVEGA on February 3, 2009

In this post I want to show you how to rebuild the standard MyLinks –> Add to My Links for a custom MyLinks Control in Visual Studio and using ASP in JavaScript.

Active Server Pages exposes an object called ServerVariables, which is part of the Request object. This ServerVariables object allows the programmer to see many environment variables. You can use the ServerVariables along with IIS’s directory sercurity options to determine who is currently accessing your site. You can also grab the visitor’s IP address using the ServerVariables object. This article includes a question and answer from the Activer Server Pages mailing list, as well as an excerpt from the Visual InterDev 1.0 help files detailing all of the properties of the ServerVariables object.

https://panvega.wordpress.com/2008/02/24/different-way-how-to-customize-the-mylink-in-moss (4. How to develop your custom MyLink Control in Visual Studio.)

mylinks

Requirements:

When clicking on Add to My Links the user should be redirected to http://yourdomain/_layouts/QuickLinks.aspx?Mode=Link instead of the Webdialog Window http://yourdomain/_layouts/QuickLinksDialog.aspx. However my default the QuickLinks.aspx?Mode=Link does not commit the added URL into the Address field.

Solution:

– Customizing the MenuItemTemplate in Visual Studio for the Add new Link context.

/* Add new Link to MyLinks
* Request Url Encoding
*/
string tmpUrl = SPContext.Current.Site.Url + “/_layouts/QuickLinks.aspx?Mode=Link”;
tmpUrl += “&Url=” + Page.Request.Url.ToString();
mtpAdd.ClientOnClickNavigateUrl = tmpUrl;

– Appending a character for the current added URL (Url)

– write your custom Delegate Control with JavaScript and the ASP Request

<script language=”JavaScript”>
function requestUrl() {
//Address field
if(document.getElementById(‘ctl00_PlaceHolderMain_txbUrl’) != null)
{
//myUrl.value = ‘<%= Request.ServerVariables(“SERVER_NAME”)%>’;
if (‘<%= Request.QueryString(“Url”) %>’ != “”)
{
var requestUrl = document.getElementById(‘ctl00_PlaceHolderMain_txbUrl’);
var newValue = ‘<%= Server.UrlDecode(Request.QueryString().ToString()) %>’;
requestUrl.value = newValue.split(‘Url=’)[1];
}
}
}
_spBodyOnLoadFunctionNames.push(‘requestUrl’);
</script>

– first you have to access the address field (ctl00_PlaceHolderMain_txbUrl’)

– I used ASP actions embedded in JavaScript to request different values from browser and save it in a JS variable. You have to use it like this:

var value = ‘<%= Request.QueryString(“Url”) %>’;

– Request the QueryString from the URL and decode the string. Copy the appopriate string into the field value. The ASP Request object is used to get information from the user.

In ASP, you can use the ServerVariables collection of the Request object to retrieve the value of HTTP headers. You can choose either VBScript or JScript (JavaScript implemented by Microsoft) as the scripting language. If you use VBScript, the code for reading HTTP headers should be like this:

If you use JScript, the code for reading HTTP headers should look like this:

var accept = Request.ServerVariables(“HTTP_ACCEPT”);
var user_agent = Request.ServerVariables(“HTTP_USER_AGENT”);
var accept_charset = Request.ServerVariables(“HTTP_ACCEPT_CHARSET”);
var accept_language = Request.ServerVariables(“HTTP_ACCEPT_LANGUAGE”);
var x_wap_profile = Request.ServerVariables(“HTTP_X_WAP_PROFILE”);
var profile = Request.ServerVariables(“HTTP_PROFILE”);

– You also have to check if the user wants to add a new Link in the QuickLinks.aspx context. Here the field shoul be opend with the default http:// value instead of copying the URL.

– Finally create a Feature and deploy the whole wsp file on your server.

Good Luck!

Some usefull links for the ASP Request

http://classicasp.aspfaq.com/files/directories-fso/how-do-i-get-the-name-of-the-current-url/page.html

http://www.w3schools.com/ASP/asp_ref_request.asp

http://www.aspnut.com/reference/encoding.asp

Other useful Links:

http://www.scripts-online.net/?action=asp

http://www.developershome.com/wap/detection/detection.asp?page=readHeader

http://aspjavascript.com/lesson08.asp

Advertisements

2 Responses to “Using ASP in JavaScript for URL Encoding in SharePoint fields”

  1. paisleygo said

    I wonder if you can also write to the server variables that way?

    I am trying to have a tabbed webpart – that has one dvwp and when you click a tab – it refilteres the webpart

    any ideas?

    I have javascript code for the tabs that will display any webparts – in tha same zone as a tab – but i want to replace that with when they click a tab take attributes of that tab and use it for filtering

  2. paisleygo said

    notify me

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: