Capturing SPGetCurrentUser to a cookie

Jul 15, 2010 at 6:30 PM

I have a tasker to captured the logged in user id and store it in a cookie which can then be used to query a web service and return values based on the user id.

I have been able to get it to show in a window.alert but could use some guidance in getting it stored in a cookie and then create a query to the web service.

<script type="text/javascript">
 $(document).ready(function() {

 var thisUserAccount = $().SPServices.SPGetCurrentUser({
 fieldName: "Name",
 
 debug: true
 });
 
 window.alert(('http://www.techweb.com/ask?user=' + thisUserAccount.split('\\')[1]));

 });
</script>

Coordinator
Jul 17, 2010 at 3:57 AM

There's no reason to save the value into a cookie just to call a Web Service with the value. Can you explain a little more about what you're trying to do?

M.

Jul 21, 2010 at 4:46 PM
Basically, the reason for the cookie is that another company owns the webservice and just want to call, reference, the cookie in their query to the webservice instead of using a query string. This way they do not have to keep making calls to the server each time they want to run a query using the logged in SharePoint user id. I can capture the user id and get it to show up in an alert and am trying to use jquery to store in a cookie that I can then call in a fuction or query. I am pretty new to jquery so any pointers would be appreciated. The point is to pull the department(s) that a employee belongs to and populate a dropdown menu.
Here is the code I have so far:
<%@ Page Language="C#" %>
<html dir="ltr">
<head runat="server">
<META name="WebPartPageExpansion" content="full">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled 1</title>
</head>








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




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

alert(('http://aftdev.stei.com?user=' + thisUserAccount.split('\\')[1]));
SetCookie('UserAccountId', thisUserAccount);
$('#Label2').text(thisUserAccount);
});
}





function GetDepartment(strUserID)
{
var webMethod = 'http://www.tech.com/webservice.asmx/GetDepartment';
var parameters = "{'strUserID':'" + strUserID + "'}";
//alert(parameters);
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
//$('
Array Length:' + msg.d.department.length + " " + msg.d.department[0].department + msg.d.ResponseStatus.Status + '
').appendTo('body');
if (msg.d.ResponseStatus.Status)
{
//clear the dropdown first
$('#Department > option').remove();
//set the the cookie to the first market on the list
SetCookie('marketid', msg.d.department[0].department);
$('#Label1').text(msg.d.department[0].department);
//now load the dropdown
for(var i in msg.d.department)
{
//alert(msg.d.department[i].department);
$('<option>' + msg.d.department[i].department + '</option>').appendTo('#department');
}
}
else
{
alert('No department found.);
}
},
error: function(e){
alert('failed');
}
});
}


function SetCookie(strCookieName, strCookieValue)
{
//function to save cookies
$.cookies.set(strCookieName,strCookieValue);
//alert($.cookies.get(strCookieName));
}

function GetWindowsUser()
{
var oNet = new ActiveXObject("WScript.Network");
return oNet.UserName;
}

function LoadDropDown()
{
//1st Get the markets
GetMarkets($('#txtName').val());
}


</script>
<body id="mbody">
<form id="form1" runat="server">
<input name="Text1" type="text" id="txtName">



<select name="Select1" style="width: 153px" id="Departments" onchange="javascript:{SetCookie('departmentid', $('#Departments').val());$('#Label1').text($.cookies.get('departmentid'));}">
<option></option>
</select> Selected Market->
<label id="Label1"></label>




<label id="Label2"></label>
<input name="btnLoad" type="button" value="Load" id="btnLoad" onclick="LoadDropDown()">
<input name="btnLoad1" type="button" value="Get User Account" id="btnLoad1" onclick="getUserAccount()">
</form>
</body>
</html>

From: sympmarc

There's no reason to save the value into a cookie just to call a Web Service with the value. Can you explain a little more about what you're trying to do?

M.

Coordinator
Jul 23, 2010 at 3:31 AM

I don't see any refences to the js files in what you posted, but I'll assume that you are making those referneces, or nothing would be working.

However, since I can't see your references, I also can;t tell what plugin you are using to set the cookie in the line:
$.cookies.set(strCookieName,strCookieValue);

I'm guessing that's where the problem lies, and you should look at the documentation for that plugin.

M.