Can I get the current user info ?

Mar 8, 2010 at 8:14 AM

Sorry, I'm new to this and I'm wondering whether I could retrive the current user data ?

Coordinator
Mar 8, 2010 at 11:44 AM

The answer is undoubtedly yes, but the specifics depend on what user data you want to get at.  Can you be more specific?

M.

Mar 21, 2010 at 11:37 AM
Edited Mar 21, 2010 at 11:43 AM

I am doing something similar. I would like to get the current user display name. I would like to populate a variable (var userDisplayName = ......)

I have just found a thread that answers exactly what i was trying to do, get user info into a javascript variable, maybe it will help you out too kissmevn ??

http://spservices.codeplex.com/Thread/View.aspx?ThreadId=83048

Coordinator
Mar 21, 2010 at 1:52 PM
Edited Mar 21, 2010 at 5:22 PM

Derek:

I'm guessing that there's more to your question, but here's what you'd do:

var userDisplayName = $(this).attr("Name");

Keep in mind that a variable which you define within the loop will be scoped to the loop.

M.

Mar 21, 2010 at 4:20 PM
Edited Mar 21, 2010 at 4:32 PM

Thanks !!

I am creating a silverlight control where I pass in the current url encoded user display name via the silverlight initparams. This seems to be working quite nicely now, thanks again for your quick reply. I was thinking of moving everything I to a java script file and I would call one or two methods within this file. What are you thoughts ? I would like to have some sort of version control, not sure what is the best way. Again any thoughts\suggestions are appreciated. 

heres what I ended up with, in a content editor webpart:

<div id="bgtSL3Host">
    <script type="text/javascript" src="http://sharepoint.com/sites/SilverLight%20Applications/SilverLight%20Application%20Resources/Silverlight.js"> </script>
    <script type="text/javascript" src="http://sharepoint.com/sites/JQuery/jquery-1.4.2.min.js"></script> 
    <script type="text/javascript" src="http://sharepoint.com/sites/JQuery/jquery.SPServices-0.5.2.min.js"> </script>
    <script type="text/javascript" src="http://sharepoint.com/sites/javascripts/UserPreferencesLibrary.js"> </script>

<script language="javascript"  type="text/javascript">

   var loginName = $().SPServices.SPGetCurrentUser();
   var getSilverlightMethodCall = "javascript:Silverlight.getSilverlight(\"3.0.40624.0\");"
   var installImageUrl = "http://go.microsoft.com/fwlink/?LinkId=108181";
   var imageAltText = "Get Microsoft Silverlight";
   var altHtml =
            "<a href='{1}' style='text-decoration: none;'>" +
            "<img src='{2}' alt='{3}' " +
            "style='border-style: none'/></a>";
        altHtml = altHtml.replace('{1}', getSilverlightMethodCall);
        altHtml = altHtml.replace('{2}', installImageUrl);
        altHtml = altHtml.replace('{3}', imageAltText);
   var userDisplayName = encodeURIComponent(captureUserDisplayName(loginName));
   //alert(userDisplayName);

  </script>

<script type="text/javascript">

        var getSilverlightMethodCall =
            "javascript:Silverlight.getSilverlight(\"3.0.40624.0\");"
        var installImageUrl =
            "http://go.microsoft.com/fwlink/?LinkId=108181";
        var imageAltText = "Get Microsoft Silverlight";
        var altHtml =
            "<a href='{1}' style='text-decoration: none;'>" +
            "<img src='{2}' alt='{3}' " +
            "style='border-style: none'/></a>";
        altHtml = altHtml.replace('{1}', getSilverlightMethodCall);
        altHtml = altHtml.replace('{2}', installImageUrl);
        altHtml = altHtml.replace('{3}', imageAltText);


        Silverlight.createObject(
            "http://sharepoint.com/sites/SilverLight%20Applications/SilverlightApplication3.xap", 
            bgtSL3Host, "slPlugin",
            {
                width: "439", height: "350",
                background: "white", alt: altHtml,
                version: "3.0.40624.0"
            },
            { },
            "debug=true,username="+userDisplayName+",configFileName=bgtConfig.xml", "row3");
    </script>
</div>

 

in the "UserPreferencesLibrary.js" javascript file I have one method

 

function captureUserDisplayName(usrName){
            var userDisplayname;
     $().SPServices({
        operation:'GetUserInfo',
                  async: false,
      userLoginName:usrName,
      completefunc: function (xData,Status){
       $(xData.responseXML).find('User').each(function(){
        userDisplayname = $(this).attr("Name");
        //alert("Status=" + Status + "XML=" + xData.responseXML.xml); 
       });
      }
     });
     return userDisplayname;
    }

 

Coordinator
Mar 21, 2010 at 4:58 PM

Nice stuff.  I'm not a Silverlight guy (yet), but it's cool to see you using SPServices with Silverlight.

On the versioning question, I like to store my .js files in a Document Library at the root of the Site Collection.  This means that you can treat the .js files like any other content and take advantage of versioning, approvals, workflows, or any other Document Library functionality that's useful.  No, it's not a true code repository, per se, but if you want to be able to fall back to previous versions and maintain control, it works extremely well.

M.