Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Sending Windows authtication username in link?

  Asked By: Bob    Date: Apr 22    Category: Sharepoint    Views: 3080

I'm brand new to Sharepoint, but have a question. We have a sharepoint
site that has a bunch of links on it. We need to pass the username of
the Sharepoint user (username from windows authentication) in these
links. For example, if "timmerk" was my Windows username that I used
to log into the sharepoint site, the links I would see would be like:


Anyone know how to do this?



6 Answers Found

Answer #1    Answered By: Jaime Weaver     Answered On: Apr 22

Search back thru my previous list replies. I'm sure I have given JavaScript and
C# solutions to this question.

If you don't find anything ping me again. I wrote a custom Web Part that
retrieves the current user's login and optionally displays it and/or passes it
to another Web Part via the IRowProvider interface. I'll send you my source if
you like.

Answer #2    Answered By: Damon Garner     Answered On: Apr 22

Here is my simple C# solution. I also used Sessions, so that the user  will see the following link  only first time (or when he/she logged in)
http://acer/Topics/default.aspx?username=administrator" target="_blank" rel="nofollow">acer/Topics/default.aspx?username=administrator

If the user is browsing the site  and again comes back to default.aspx page, the URL will be
http://acer/Topics/default.aspx because session is set second time.

You can programmatically make this as a hidden web part and drop in on default.aspx page.

protected override void RenderWebPart(HtmlTextWriter output)
SPWeb myWeb = SPControl.GetContextWeb(Context);
string username  = myWeb.CurrentUser.LoginName.ToString();
if (Page.Session["setSession"] == null)
Page.Session["setSession"] = "true";
Page.Response.Redirect(myWeb.Url.ToString()+"?username="+userName.Substring( userName.LastIndexOf(@"\")+1));
catch ( Exception ex)
output.Write(" "+ex.StackTrace);

You will have to modify web.config to enable session state. Here is more help

Please let me know if you need the complete web part project.

Answer #3    Answered By: Karla Morrison     Answered On: Apr 22

There’s actually a very simple solution to this. sharepoint  (both WSS v2 & v3) have something called the Web Part Page Services Component (WPSC) that adds a ton of additional functionality client-side. One of the things is has is a token replacement. If you want more info on this, check out the reference [1] at the bottom of this reply.

If you create a link  that looks like this:

http://www.microsoft.com?username=_LogonUser_ (literally typing out the string “_LogonUser_” at the end of it, when the page is rendered, SharePoint will do a string replacement so the above URL would actually be:


While much of the WPSC is intended to be used within <script> tags, this token replacement can happen in straight inline HTML. To test, add a Content Editor Web Part to the page and create a hyperlink using the URL above after you save your changes, roll over the link to see what it rendered at the client.

If you don’t want the domain, then you can create some JavaScript function that will extract the account name from the full string and just call the JavaScript function having it return the value desired.

[1] Search for “WPSC Overview” in the WSS v3 SDK. Click on the link (with the arrow) “Client-side programming using the WPSC” to get more info on the token replacement.

Answer #4    Answered By: Patricia Richardson     Answered On: Apr 22

Sorry… that reference at the bottom was supposed to say: in the WSS v2 SDK.

The WPSC is available in both WSS v2 & WSS v3.

Answer #5    Answered By: Alexandra Patterson     Answered On: Apr 22

Mind sending  me that complete web control project and anything else I need

to use to drop it in? I have no experience in this stuff, but I think I can

manage modding your code and project and somehow putting it in Frontpage

(how do I include the hidden web control in Frontpage?)

Answer #6    Answered By: Christop Mcfadden     Answered On: Apr 22

I have attached the web part cab file with strong name in a separate email to you (sorry didn't get time to create .msi file). Please use
stsadm.exe -o addwppack -filename <path to Web Part Package> [-url <URL>] [-globalinstall]
you can also download the GIU from this link


When you will drop the web part on a web page, it will be hidden automatically. I have also attached the complete web part project.

Didn't find what you were looking for? Find more on Sending Windows authtication username in link? Or get search suggestion and latest updates.