Monday, 16 July 2012

JQUERY: SharePoint 2010 Get Current User for web application


JQUERY: SharePoint 2010 Get Current User for web application

·         Download the latest jquery.js from SPServices Jquery.
·         Most releases of the library include both a minified and a normal version of the release. If you would like to understand the workings of the library, look at the normal version, but use the minified version for any production use.
·         Upload the minnified js file to the style library of your site.

The library can be implemented by adding a reference to it into a single page, a page layout, a content query web part or a master page, depending upon your desired scope of use

I recommend storing the jQuery library and SPServices in a Document Library in your Site Collection and referencing it as needed, like this:

<script language="javascript" type="text/javascript" src="/jQueryLibrary/ jquery.SPServices-0.7.1a.min.js"></script>

<script language="javascript" type="text/javascript" src="/jQueryLibrary/ jquery.SPServices-0.7.1a.min.js"></script>

Obviously, the src attributes should point to wherever you've put the .js files.

·         Add the following code
$().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false });

This is a quick script to check if everything is working OK:
<script type="text/javascript">
alert ("js on.");
$(document).ready(function() {
alert ('jQuery on.');
var theuser= $().SPServices.SPGetCurrentUser();
alert ('Current user for web application is : ' + theuser);
});
</script>

Cheers :)

No comments:

Post a Comment