get current user and pass it to a url in a page viewer webpart or content editor webpart

Mar 18, 2010 at 12:48 AM

I'm not sure if this is possible but can i get a currentuser and pass it to a url in page viewer webpart or content editor webpart
e.g
http://myserver/ask?user=loginuser

Thanks in Advance

Coordinator
Mar 18, 2010 at 3:05 AM

Sure! Use the $().SPServices.SPGetCurrentUser function.

M.

Mar 18, 2010 at 5:24 AM

Can you advice how this can be done using Content Editor WebPart or the Page Viewer Webpart.
I tried viewing the logon user using the below:

<script type="text/javascript" language="javascript" src="/jQuery%20Libraries/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript" src="/jQuery%20Libraries/jquery.SPServices-0.4.5.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
    $().SPServices.SPGetCurrentUser();
    });
</script>

But i don't get anything.
but if i would like to add it to a url like this :http://myserver/ask?user=loginuser

How to.
Thanks in Advance

 

Coordinator
Mar 18, 2010 at 9:09 PM

naijacoder:

Check the docs for the function.  You need to assign the value returned from the $().SPServices.SPGetCurrentUser to something to work with it.

Something like this:

<script type="text/javascript" language="javascript" src="/jQuery%20Libraries/jquery-1.3.2.min.js"></script>
<script type="text/javascript" language="javascript" src="/jQuery%20Libraries/jquery.SPServices-0.4.5.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
      var thisUser = $().SPServices.SPGetCurrentUser();
      document.location = "http://myserver/ask?user=" + thisUser;
    });
</script>

By the way, you're using old versions of both the jQuery library and this library. You should update to the most recent versions.

M.

Mar 18, 2010 at 11:18 PM

Marc sorry for being a pain but quite new to jquery
I tried the below (As you can see below the js are in the layouts folder)

    <script src="/sites/Retail//_layouts/jquery/jquery.SPServices-0.5.2.js" type="text/javascript"></script>
                <script src="/sites/Retail/_layouts/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
      var thisUser = $().SPServices.SPGetCurrentUser();
      
  document.write = "http://myserver/ask?user=" + thisUser;

    });
</script>

But getting error "SPServices is null or not an object"

And do you know how i can put the url in an iframe e.g something like this

<iframe src="http://myserver/ask?user=" + thisUser;/>

Thanks in Advance

Coordinator
Mar 19, 2010 at 12:03 AM

It looks like you've got an extra slash in the reference to the SPServices library. You should also call it after the jQuery library, so:

<script src="/sites/Retail/_layouts/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/sites/Retail/_layouts/jquery/jquery.SPServices-0.5.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
      var thisUser = $().SPServices.SPGetCurrentUser();
       
  document.write = "http://myserver/ask?user=" + thisUser;
    });
</script>

The references also point to the _layouts folder, which means that you've got the files installed on the server, correct?

I'm not sure on exactly how to do the iframe piece without knowing more, but I'm sure it's possible.

M.

Mar 19, 2010 at 1:01 AM

Oops  didn't notice that and thanks for the reply
But i tried that i get to see no data.
I'm putting the snippet code straight inside a Content Editor WebPart like so

<script src="/sites/Retail/_layouts/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/sites/Retail/_layouts/jquery/jquery.SPServices-0.5.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
      var thisUser = $().SPServices.SPGetCurrentUser();
      
  document.write = "http://myserver/ask?user=" + thisUser;
    });
</script>

I also tested it my  ref it in the masterpage of the site like below
I mean the masterpage of the rep site.
See below:

<BODY scroll="yes" onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();">
<script src="_layouts/ToolTipScripts/wz_tooltip.js" type="text/javascript"></script>
    
                <script src="/sites/Retail/Reps/_layouts/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>

<script src="/sites/Retail/Reps/_layouts/jquery/jquery.SPServices-0.5.2.js" type="text/javascript"></script>

Still no luck.

Could the wz_tooltip.js be the problem.
I have no idea.
I don't get the error SPServices is null or not an object anymore.
But the webpart is blank.

THanks alot Marc

Coordinator
Mar 19, 2010 at 4:50 PM

I'm guessing that the script file references still aren't right. Two things you can try:

  • In SharePoint Designer, Ctrl-Click on the src and the .js file should open in SPD
  • Add an alert insode the $(document).ready. If the references are right, you'll see the alert.

Finally, you're doing a document.write, but you aren't specifying a location.

M.

Mar 24, 2010 at 1:20 AM

Hi Marc,
     When i try the below  i can see the alert.

<script src="/sites/Retail/_layouts/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/sites/Retail/_layouts/jquery/jquery.SPServices-0.5.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var country = "Fiji";
    var city = "Suva";
    alert('The city of ' + city + ' is located in ' + country + '.');   
   });
</script>

No idea why
var thisUser = $().SPServices.SPGetCurrentUser();
       
  document.write = "http://myserver/ask?user=" + thisUser;

Doesn't work :(

Thanks

Mar 25, 2010 at 12:20 AM

Hi Marc,
     I got it working

<script src="/sites/Retail/_layouts/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/sites/Retail/_layouts/jquery/jquery.SPServices-0.5.2.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var thisUser = $().SPServices.SPGetCurrentUser();
alert('http://myserver/ask?user=' + thisUser);
         });
</script>

Nice one.I need to get use to your jqUERIES

Coordinator
Mar 25, 2010 at 2:19 AM

Glad you got it working!

M.

Oct 3, 2010 at 8:53 AM
Edited Oct 3, 2010 at 10:14 AM

Hi there,

Thank u for your code,

i put alert('hello'); inside function and it showed but still page has javascript error "SPServices Is NULL Or not an object"

i use jquery-1.4.2.js and jquery.SPServices-0.5.6.min.js , in a Content Editor webpart.

:(

thanks

 

Coordinator
Oct 3, 2010 at 12:06 PM

aria_cop:

Sounds like your reference to the jQuery file is right, but the one to SPServices is not.

M.

Sep 27, 2011 at 6:05 PM

In my case it only works for administrators , the regular user it doesn´t , any advice !??

 

Sep 28, 2011 at 10:17 AM

peps, I replied again to your other post. A small suggestion would be to create a new post if you are still having issues. This way you keep one post with all the replies together. I hope my answer works for you!