4

In SharePoint 2010 Sandbox solution, whether cookie is supported?

When I'm trying to set cookie by

HttpCookie myCookie = new HttpCookie("Test","Hello"); HttpContext.Current.Response.Cookies.Add(myCookie);

The value is not passed to browser and the Request.Cookies["Test"] is null on next request.

The same code works fine in farm based solution. Please confirm.

Mac
  • 178
  • 7

2 Answers2

7

No you cannot set cookies in the sandbox, the data are not transferred back between the sandbox and the IIS. The Sandbox lives in it's own process and the HttpContext.Current is different from the HttpContext.Current in ASP.NET (that lives in IIS and the w3wp.exe process)

For more limitations see: http://www.wictorwilen.se/Post/Understanding-the-SharePoint-2010-Sandbox-limitations.aspx

3

Regarding cookie support in Sandbox Solutions, let me break it into two parts:

Write a cookie : Not Supported via HTTPContext(Unless you set and use cookie in same sandbox webpart and in same request- which will obviouly not be the case :-))

Read a cookie : You are free to consume cookies in Sandbox Solutions! Assume, you have a cookie which is set already, the below code will work :

string cookieValue= HttpContext.Current.Request.Cookies["Test"].Value;

Another solution which don't need server-side deployment is the JQuery plugin: http://plugins.jquery.com/project/cookie

Amit Kumawat
  • 6,689
  • 2
  • 22
  • 33