In Sharepoint 2010 I'm trying to do a custom google search based on the currently logged in user's username. For example if a user named john clicked on the link he would be directed to http://www.google.com/search?q=john

In order to get a user's username from the AD and display it in a page, I'm using this property:

<SPSWC:ProfilePropertyValue PropertyName="PreferredName" runat="server" id="ProfilePropertyValue6"/>

So far I have come across this javascript snippet which allows me to concatenate a variable to the end of google's search url:

<script type="text/javascript">
var url = "john";
<a href="http://www.google.com/search?q=" onclick="location.href=this.href+url;return false;">Link</a> 

By assigning john to the url variable I am able to concatenate the username to the end of the url.

However, what I want to do is assign the SPSWC:ProfilePropertyValue... to the variable url so that each individual user's username is concatenated to the end of google's url.

So far I have tried assigning SPSWC:ProfilePropertyValue... to the url variable:

var url = <SPSWC:ProfilePropertyValue PropertyName="PreferredName" runat="server" id="ProfilePropertyValue6"/>;

This didn't work because I can't seem to call the property value inside of the script tags, so it will have no effect.

So what is the proper way of modifying the url with javascript?


try this instead:

var url = {  UserName: ' <SPSWC:ProfilePropertyValue PropertyName="PreferredName" runat="server" id="ProfilePropertyValue6"/>'};  

check to make sure above works first ;) once it gets the name you can then do:

var name = {  UserName: ' <SPSWC:ProfilePropertyValue PropertyName="PreferredName" runat="server" id="ProfilePropertyValue6"/>'};  

var url = "http://www.google.comsearch?q="+name;

also like to note that i dont know if the property name your using would work and also make sure your id is correct? so you should try:

var name = {  UserName: ' <SPSWC:ProfilePropertyValue PropertyName="FirstName" TitleMode="true" runat="server" />'}; 
var url = "http://www.google.comsearch?q="+name;

shouldnt the preffered prop look like this?

  <SPSWC:ProfilePropertyValue PropertyName="PreferredName" ApplyFormatting="false" id="userPreferredName" runat="server"/>

as a personal opinion I wouldnt be using this method! you should be using the javascript object mode to get the username! thats what its there for ;)

someone is nice enough todo most of the work!

   var spUser;

   // Ensure SP objects have been loaded before executing our code    

   function getSpUser() {
       var clientContext = new SP.ClientContext.get_current();
       var spWeb = clientContext.get_web();
       spUser = spWeb.get_currentUser();
       clientContext.executeQueryAsync(getSpUserSuccess, getSpUserException);

   function getSpUserSuccess(sender, args) {
       var curUsername = spUser.get_title();
       var url = "http://www.google.comsearch?q="+curUsername;
        window.location = url;

   function getSpUserException(sender, args) {
       // Do any necessary error handling.

iv ammended the answer slightly to suit your needs :)


| improve this answer | |
  • Hi, I've used the first line which you suggested, but now every time I click the link google redirects me to this link instead of concatenating the username: google.com/search?q=[object%20Object] – user18548 Jan 7 '14 at 5:31
  • 1
    its returning the object because its a control that is run on the server. The code that your trying to use is ment to be within the masterpage or aspx page that runs on the server so that is why its returning the object itself rather than any data! Your best method would be to use my last example as that is the correct way! – Ali Jafer Jan 7 '14 at 8:44
  • My knowledge of javascript at the time being is not sufficient enough, so I'm going to read a javascript book and then read your post again. Thank you for your help. – user18548 Jan 7 '14 at 9:46
  • no problem happy to help! – Ali Jafer Jan 7 '14 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy