none
Create a cookie in a SP web part RRS feed

  • Question

  • Hi,

    I am creating a web part in Visual Studio 08 using the SP extensions for VS.

    The web part I'm creating needs to create a cookie for the current user. I don't need to store anything in the cookie.

    I have found several examples where cookies have been created but these all seem to be 'code-behind' for aspx pages whereas I am coding a web part.

    Can anyone provide me with a simple code example of a cookie created in a web part for SharePoint?

    Tuesday, April 20, 2010 2:25 PM

Answers

  • Perhaps you could create cookies via javascript in your web aprt

     

    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }

    function eraseCookie(name) {
    createCookie(name,"",-1);
    }

    • Marked as answer by nab89 Tuesday, April 20, 2010 2:44 PM
    Tuesday, April 20, 2010 2:38 PM
  • Q1: What are you trying to store in the cookie?

    Q2: Why cant you use similar code in your webpart code?

    Even if you are not using aspx pages, you can still write code to create cookie maybe in the constructor of your webpart class.

    HttpCookie cookie = new HttpCookie(cookieName);
    cookie.Value = "true";
    cookie.Expires = DateTime.Now.AddDays(cookieExpires);
    HttpContext.Current.Response.Cookies.Add(cookie);


    BR, PM
    • Marked as answer by nab89 Tuesday, April 20, 2010 2:44 PM
    Tuesday, April 20, 2010 2:40 PM
  • Almost... Instead of the Response object, you need to read an incoming cookie using the Request object

    HttpCookie cookie = HttpContext.Current.Request.Cookies["MyCookie"];
    if (cookie != null)
    {
      string val = cookie.Value;
    }
    


    Paul.

     

    • Marked as answer by nab89 Tuesday, April 20, 2010 4:37 PM
    Tuesday, April 20, 2010 3:29 PM

All replies

  • Perhaps you could create cookies via javascript in your web aprt

     

    function createCookie(name,value,days) {
    if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
    }

    function eraseCookie(name) {
    createCookie(name,"",-1);
    }

    • Marked as answer by nab89 Tuesday, April 20, 2010 2:44 PM
    Tuesday, April 20, 2010 2:38 PM
  • Q1: What are you trying to store in the cookie?

    Q2: Why cant you use similar code in your webpart code?

    Even if you are not using aspx pages, you can still write code to create cookie maybe in the constructor of your webpart class.

    HttpCookie cookie = new HttpCookie(cookieName);
    cookie.Value = "true";
    cookie.Expires = DateTime.Now.AddDays(cookieExpires);
    HttpContext.Current.Response.Cookies.Add(cookie);


    BR, PM
    • Marked as answer by nab89 Tuesday, April 20, 2010 2:44 PM
    Tuesday, April 20, 2010 2:40 PM
  • Hi,

    The code for a cookie in a web part should be no different than that for an aspx page. For example, to set a cookie

     HttpCookie cookie = new HttpCookie("MyWebPartCookie");
     cookie.Expires = DateTime.Now.AddDays(1);
     HttpContext.Current.Response.Cookies.Add(cookie);
    


    Hope that helps

    Paul.

     

     

    • Marked as answer by nab89 Tuesday, April 20, 2010 2:44 PM
    • Unmarked as answer by Mike Walsh FIN Tuesday, April 20, 2010 3:52 PM
    • Edited by Mike Walsh FIN Tuesday, April 20, 2010 3:54 PM Moderator Note: Unmarked as answer because identical code to the post made marginally earlier.
    Tuesday, April 20, 2010 2:41 PM
  • Thank you for your answers.

    I wasn't aware that the code would be the same.

    Just one more thing, to read the cookie now I assume I do this:

    Cookie myCookie = HttpContext.Current.Response.Cookies["My Cookie"];
    string myValue = myCookie.Value;

     

     

    Tuesday, April 20, 2010 2:49 PM
  • Almost... Instead of the Response object, you need to read an incoming cookie using the Request object

    HttpCookie cookie = HttpContext.Current.Request.Cookies["MyCookie"];
    if (cookie != null)
    {
      string val = cookie.Value;
    }
    


    Paul.

     

    • Marked as answer by nab89 Tuesday, April 20, 2010 4:37 PM
    Tuesday, April 20, 2010 3:29 PM