Cannot set cookie value when path is set

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?
DPCTAsked:
Who is Participating?
 
DPCTAuthor Commented:
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
 
leakim971PluritechnicianCommented:
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
 
DPCTAuthor Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
leakim971PluritechnicianCommented:
Same hostname?
0
 
Michel PlungjanIT ExpertCommented:
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
 
DPCTAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.