How to use cookie in entire site collection and among n number of sub sites?

Hi,

Can anybody tell me how to use cookie in site collection among n number of subsite?

I have a requirement to store a value and want to use in entire site collection.

Just I created a cookie using javascript and assigned some value but when I try to access in another 

sub site in same site collection it does not return anything and if try to get a value in same 

site where I had assigned a value to that cookie it gives properly.

Can anybody tell me is there any way to maintain an information at client side and use it 

across the site collection?

Looking forward to hear from you.

Thanks in advance.

Kalpesh

June 18th, 2013 4:51pm

Hi,

Usually if you set cookie at the domain (i.e., abc.com) then you should be able to access the cookie in that domain only. To set cookie in a domain and access it from subdomain please take a look at the link. You need to set domain in a specific way to access cookie from subdomain.

http://stackoverflow.com/questions/5671451/creating-a-javascript-cookie-on-a-domain-and-reading-it-across-sub-domains

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2013 4:20am

Hi Sohel,

Thank you for replying me.

I have tried according to suggested URL (http://stackoverflow.com/questions/5671451/creating-a-javascript-cookie-on-a-domain-and-reading-it-across-sub-domains) .

But still it does not work properly.

In my SharePoint solution I have added code like :-

document.cookie=c_name + "=" + c_value + ";domain=.testSite;path=/;";

My SharePoint Foundation site url suppose like http://testSite/ .

The above code does not work.

Please help to get rid of the above issue.

Many Thanks



  • Edited by Kalpesh4 Wednesday, June 19, 2013 11:42 AM TYPO
June 19th, 2013 11:41am

Can you please some full domain naming format - like testsite.com? Instead of http://sitename you can use http://sitename.com
Free Windows Admin Tool Kit Click here and download it now
June 20th, 2013 1:19am

>>how to use cookie in site collection among n number of subsite

All sites in the same site collection share the same web domain.

>>Can anybody tell me is there any way to maintain an information at client side and use it across the site collection

All site collections (except for host header site collection) in the same sharepoint web application share the same web domain of the web application zone by default.


June 20th, 2013 8:06am

Hi,

You can use local storage or session storage feature of HTML 5.

Since you can store the data at client side and can use within the entire site collection.

Also it is safe and more faster than cookies.

This web storage can be use in any A grade browsers and ahead of IE8 version. 

 You can use sessionStorage as following : 

 To set values :

 window.sessionStorage.setItem("key", 'value');

To get values :

 window.sessionStorage.setItem("key", 'value');

Free Windows Admin Tool Kit Click here and download it now
July 11th, 2013 9:45am

Hi,

This JAVA code does work and I have tested on a V4.master running in the footer between the </body> and the </html> tag.  It hits the sites and sub-sites of the entire farm for example, example.com.  The cookie will remain active until Dec 31 on the machine it was accessed on.  The only thing that is necessary is this modification must exist in all default master page on each sub-subsite or site collection on the entire site.  The path =/ will allow every site access to read the existing cookie from the root.

<script type="text/javascript" language="javascript">
    var agreement = GetCookie();

    // checks for cookie and displays disclaimer alert if new user
    if (agreement == "") {
		//Author: Tony Di Leonardo
        var decision = confirm("DISCLAIMER: GOES HERE \n\nClick Ok if you agree to the disclaimer or click Cancel to close this window. \n");
        

        if (decision == true) {
            // writes a cookie
			var oneMinute = 60 * 1000
			var oneHour = oneMinute * 60
			var oneDay = oneHour * 24
			var today = new Date()
			var nextXmas = new Date()
			nextXmas.setMonth(11)
			nextXmas.setDate(31)
			if (today.getMonth() == 11 && today.getDate() > 31) {
				nextXmas.setFullYear(nextXmas.getFullYear() + 1)
			}
			var expiredays = nextXmas.getTime() - today.getTime()
			expiredays = Math.floor(expiredays/oneDay)
            var exdate = new Date()
            exdate.setDate(exdate.getDate() + expiredays)
            document.cookie = "PartnerAgreement" + "=" + escape("Agree To Disclaimer") +
            ((expiredays == null) ? "" : ";path=/; expires=" + exdate.toGMTString())
        }
        else {
            // redirect
            window.location = "/_layouts/signout.aspx";

            // or close the browser window
            //window.opener='x';
            //window.close();
        }
    }

    // gets the Cookie if it exists
    function GetCookie() {
        if (document.cookie.length > 0) {
            c_name = "PartnerAgreement";
            c_start = document.cookie.indexOf(c_name + "=")
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1
                c_end = document.cookie.indexOf(";", c_start)
                if (c_end == -1) c_end = document.cookie.length
                return agreement = unescape(document.cookie.substring(c_start, c_end))
            }
        }
        return "";
    }

</script>




May 15th, 2015 1:04pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics