Answered Implement Cookie in Sahrepoint

  • 10 Juli 2012 8:11
     
      Memiliki Kode

    Hi,

    I have requirement of implementin cookie in my web part.I have used the following code.


                

    HttpCookie objPollingCookie = HttpContext.Current.Response.Cookies[strListURL + "|" + iListItemID + "|" + SPContext.Current.Web.CurrentUser.ID]; if ((objPollingCookie != null) && (objPollingCookie.Value != null)) { //HttpCookie objPollingCookie = this.Page.Response.Cookies[strListURL + "|" + iListItemID + "|" + SPContext.Current.Web.CurrentUser.ID]; Utils objDecrypt=new Utils(); string strVoteStatus = objDecrypt.DecryptString(objPollingCookie.Value); if (strVoteStatus == "Yes") { return true; } else { return false; }

    }


    The cookie never returns false cookie is present their with some default values with date as 0001-01-01 ,ie why I have addded the conditon for cookie.value==null. The cookie I am writing are also not geting saved .Is there anything I am missing for implementing the cookie in sharepoint.COde for writing cookie is given below

    HttpCookie objPollingCookie = new HttpCookie(strListURL+"|"+iListItemID+"|"+SPContext.Current.Web.CurrentUser.ID);
    
                objPollingCookie.Value = objEncrypt.EncryptData("Yes");
                objPollingCookie.Expires = DateTime.Now.AddMonths(1);
                HttpContext.Current.Response.Cookies.Add(objPollingCookie);

Semua Balasan

  • 10 Juli 2012 8:30
     
     Jawab Memiliki Kode

    Hello

    I have used the following line of code in my example

    it is working fine for me.

    private void writeCookie(string strSurvey,string itemID)
    		{
    			try
    			{
    				string cookieName = strSurvey + itemID;
    				string redirectUrl = string.Empty;			
    				HttpCookie cookie = Request.Cookies[cookieName];
    				if (cookie == null)
    				{
    					cookie = new HttpCookie(cookieName);
    				}
    				cookie["ID"] = itemID;
    				cookie.Expires = DateTime.Now.AddYears(1);
    				Response.Cookies.Add(cookie);
    			}
    			catch (Exception ex)
    			{
    				throw ex
    			}
    		}

    Hope this will help


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    • Disarankan sebagai Jawaban oleh Prateesh Nair 11 Juli 2012 7:53
    • Ditandai sebagai Jawaban oleh naveen m sasi 11 Juli 2012 7:54
    •  
  • 11 Juli 2012 7:12
     
     

    Thanks Hiren,

    I made a mistake.While getting cookie I have used "Resonse" instead of "REquest" .When I made that change it started working .