Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cannot set cookie value when path is set

Posted on 2011-05-04
9
Medium Priority
?
355 Views
Last Modified: 2013-11-19
I want to have the user click a button on a page in SharePoint that will upload a file then pre-populate a required field for that document library. I need to do all this client side. (no custom code allowed on the server). Since I can't modify the upload form, I thought I would try saving the value in a cookie from my first form, then read it from the edit form for the document library. My problem is that if I write the cookie using javascript

document.cookie = "MyField=MyValue";

it saves the cookie. If I instead use

document.cookie = "MyField=MyValue; path=/";

it fails to save the  cookie.
When I save it without the path, the form that saved the cookie can read the value back, but other forms, even in the same folder on the same site, can't see the cookie value and return null.

Any experts out there have any experiance with SP cookie madness?
0
Comment
Question by:DPCT
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 35697178
Work :
document.cookie = "MyField=MyValue";

Work :
document.cookie = "path=/";

Don't work :
document.cookie = "MyField=MyValue; path=/";

Work :



document.cookie = "MyField=MyValue"; 
document.cookie = "path=/";

Open in new window

0
 

Author Comment

by:DPCT
ID: 35697676
I tried this.  It is now setting the cookie, but I still can't read it from another page. only the page that set the cookie.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35697760
Same hostname?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35698488
Use this

<script src="cookie.js"></script>
<script>
setCookie("MyField","MyValue",expiryDate,"/");
</script>
// cookie.js file
var daysToKeep = 14; // default cookie life...
var today      = new Date(); 
var expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));


/* Cookie functions originally by Bill Dortsch */
function setCookie (name,value,expires,path,theDomain,secure) { 
   value = escape(value);
   var theCookie = name + "=" + value + 
   ((expires)    ? "; expires=" + expires.toGMTString() : "") + 
   ((path)       ? "; path="    + path   : "") + 
   ((theDomain)  ? "; domain="  + theDomain : "") + 
   ((secure)     ? "; secure"            : ""); 
   document.cookie = theCookie;
} 

function getCookie(Name) { 
   var search = Name + "=" 
   if (document.cookie.length > 0) { // if there are any cookies 
      var offset = document.cookie.indexOf(search) 
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value 
         var end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value 
         if (end == -1) end = document.cookie.length 
         return unescape(document.cookie.substring(offset, end)) 
      } 
   } 
} 
function delCookie(name,path,domain) {
   if (getCookie(name)) document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

Open in new window

0
 

Author Comment

by:DPCT
ID: 35698565
Yes. The same host name.   I can't read a cookie set by one page from another page on the same server with the same path except for the file name.

I have tried a number of these code solutions. I Including this one. I believe my issue has something to do with the site being in SharePoint.  Even with the path set to "/", I cannot read the set cookie from any other page than the one that wrote it.  Sorry if I'm not being too clear. Has anyone used cookies on a SharePoint site across pages?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 35698742
0
 

Accepted Solution

by:
DPCT earned 0 total points
ID: 35728839
I found the very obscure answer to this problem. When you set the path for the cookie it cannot be "/" or "../" or even a fully qualified path. It Must be the current folder and the paretn folder only. FOr example
If the pathe was 'myInstance.mycompany.com/allsites/thisSite/ThisSubsite" the only path that the system would accept and would work was path="/thisSIte/ThisSubsite". I have no Idea the rational behind it, but this worked
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question