Friday, April 1, 2011

Logged-In/Current user's email address populated from list in custom list form

Scenario: Recently I had a requirement wherein the business wanted to maintain the email addresses of users in a list. And in other forms where users are suppose to enter data, the email address should automatically populated from this list. The Site was going to be hosted with hosting company and they wanted the email address in a list so that every user can easily maintain the email address and other information.

Solution: One custom list for user where at least email address, User ID and the person's NT ID (filed type-person) is stored. Then in the record list where users are making entries (for example: Sales Order Main) there will be a field for email address of the user (for example: Sales Executive) and in the new form, it would automatically populated form the other user list using SPServices GetListItems method supplying the currentUser as parameter.

Example execution:

1. Create a list for users:

Note: The title column has been renamed to NTUserID. You may as well customize the new list form for this lsit so that users dont have to enter the NTUserID manually and by validating the NTUserName, it inserts the NTUserID in the text field. and you can make the NTUserID hidden. 

2. In SPD, create a custom list form for new items and and edit that form in advance mode and locate the placeholder PlaceHolderAdditionalPageHead

3. Put reference to the Script Library (I assume that you would have a DocLib for script libraries that will have all scripting libraries uploaded).


<script src="/ScriptLib/jquery-1.5.2.js"></script>
<script language="javascript" type="text/javascript" src="/ScriptLib/jquery.SPServices-0.6.0/jquery.SPServices-0.6.0.min.js"></script>

4. Now you have to get the email address from the Users list for currently logged in user.

$(document).ready(function() {
var thisUserAccount = $().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: false
});

Note: You should alway use the Name field as working with Title is easy but then there could be more than one person with same Title person in AD however there cannot be two person with same account name.

5. Before you can use the thisUserAccount , you should replace the single slash with double slash so that it works with CAML.

var userID=String(thisUserAccount);
userID.replace("\\","\\\\")

5. Now get the element ID for the email address text box. (If the element ID in your form is ff4 then it will be rendered with a big ID with _ff41_ as part of the ID text. You can just use this rather then using complete ID to get hold of the text box and assign value of the email address you got from previous JQuery call.


   $().SPServices({
   operation: "GetListItems",
   async: false,
   listName: "Users",
   CAMLViewFields: "<ViewFields><FieldRef Name='EMailAddress' /></ViewFields>",
   CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + userID + "</Value></Eq></Where></Query>",
   completefunc: function (xData, Status) {
     $(xData.responseXML).find("[nodeName='z:row']").each(function() {
       $("#[id*='_ff41_']").val($(this).attr("ows_EMailAddress"));
     });
   }
 });
});

Note: The expression $("#[id*='_ff41_']").val() will locate any element that has _ff41_ in the ID. Since we are using both underscores, it would surely get that text box and another good thing (may be) about this is that even if you move this list to other web apps/site collection on any different level, or upgrade SharePoint  in future, the full ID may change but (most probably) it will have the _ff41_ text in it.

So now if the list has the email address for logged in user, the new form will have that in the email address text box on form load. 


1 comment:

  1. Good one.

    Another alternative approach, instead of using ID (which may vary in different environment), We can use input[title='Display name of sharepoint field'] to assign the value

    ReplyDelete