Thursday, July 31, 2008

How to add Source parameter to SharePoint links automatically?

Hi,

You know when you click a link to view item in SharePoint list and when you click Close it goes back to the page you came from?
Or when you edit an item - clicking OK returns you the page you original came from?

Well, you may have noticed SharePoint does that by adding the "Source=" url query string parameter to the address, and uses it to knwo where to go after we user is done.

So, basically - you can use that to redirect the user anywhere you want after he finishes with the form you sent him to, but - what if you want it to automatically be set to the current page's address?
Of course you can hard code the page's address, but what about additional query string parameters, of multiple addresses to the same page (different host headers)?

There is an easier way - the way SharePoint adds the "Source" parameter -
simply add this bit to your a link in HTML:
ONCLICK="GoToLink(this);return false;"

so that your link will look like:
<a href="/Docs/Lists/Announcements/DispForm.aspx?ID=1" ONCLICK="GoToLink(this);return false;">view item</a>

The GoToLink will automatically add thye current page's address before navigating to the HREF you entered!

Shai Petel

11 comments:

Anonymous said...

Wow that was exactly what I was looking for !

Thx !

Anonymous said...

Great tip, thanks!

Anonymous said...

made my day!

Anonymous said...

You're my hero!

Anonymous said...

Beautiful. Thanks.

midooley87 said...

Is there any way to do this just within the URL string. I can't imbed the URL (I'm pasting it in as a URL link in a list description), but need it to return to the original click location.

any advice would be greatly appreciated!

Anonymous said...

This is totally awsome. I always wonder how you figure this stuff out. Jim

Shai Petel said...

@Jim - lots of javascript debugging...

@midooley87 - unless you can have some script inside that field (try using rich html field and not a simple link field) i dont see how you can do that... sorry.

Trung Nga said...

Hello Shai,

I really like the tip, simple and concise explanation.

Many Thanks

Anonymous said...

awesome

Anonymous said...

Thanks, just what i needed!