Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer
    
Thanks. In the case of session, since it shared across web application, the session object is not available in sandbox solution? –  Mac Nov 26 '11 at 13:26
    
No, it has nothing to do with that. Read the linked article - it's the marshalling between processes and the special sandboxed objects that creates this limitation. –  Wictor Wilen MCA MCM MVP Nov 26 '11 at 13:44
    
@Wictor-SharePointMVP,We can read cookie in Sandbox Solutions. –  Amit Kumawat Nov 28 '11 at 10:57
    
@Amit - great, need to update my post on that –  Wictor Wilen MCA MCM MVP Nov 28 '11 at 15:37
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.