<p>I have a custom SharePoint application page deployed to the _layouts folder.  It's a custom "new form" for a custom content type.  During my interactions with this page, I will need to add an item to my list.  When the page first loads, I can use SPContext.Current.List to see the current list I'm working with.  But after I fill in my form and the form posts back onto itself and IsPostBack is true, then SPContext.Current.List is null so I can't find the list that I need to add my stuff into.</p>

<p>Is this expected?</p>

<p>How should I retain some info about my context list across the postback?  Should I just populate some asp:hidden control with my list's guid and then just pull it back from that on the postback?  That seems safe, I guess.</p>

<p>FWIW, this is the MOSS 2007 Standard version.</p>

[{'answer_id': 30166, 'author': 'Nathan DeWitt', 'author_id': 1753, 'author_profile': 'https://Stackoverflow.com/users/1753', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not using a custom "new form", so this might not apply.  I added an event receiver to my custom content type and then do my custom code in the ItemAdded or ItemAdding events.  This code fires when the event is added to a list.  You can use the event receiver properties to get to the parent List, Web, and Site.</p>\n'}, {'answer_id': 30691, 'author': 'Daniel McPherson', 'author_id': 897, 'author_profile': 'https://Stackoverflow.com/users/897', 'pm_score': 3, 'selected': True, 'text': '<p>Generally speaking I try and copy whatever approach the product group has taken when looking to add functionality of my own. In this case they add their own edit/view/add pages via the list definition itself.</p>\n\n<p>I built a solution that also needed its own custom "New" form, not open source unfortunately, though if you are interested you can download it, its called "Tagged Links" (Social Bookmarking for SharePoint) and you can find some links on my blog.</p>\n\n<p>To give you a few hints and tips, the following should set you off in the right direction:</p>\n\n<ol>\n<li>Created a new list definition.</li>\n<li>Created a new Content Type In the content type you can define your own "FormTemplates" that references a Rendering Template which determine what gets displayed in the "Middle" bit of those forms.</li>\n<li>Copied the standard Rendering Template, but then made the changes to it that I\nneeded.  </li>\n<li>Wrapped it all up in a solution, and deployed.</li>\n</ol>\n\n<p>My Rendering Template actually included an overridden "Save" Button where I did a lot of the extra work I needed to do during the save.</p>\n\n<p>Anyway, it is a little too much work in my opinion but, I think, it most closely matches the standard approach taken by the product developers. Let me know if you need more detail and I will see if I can put together a step-by-step blog post, but hopefully this gets you off on the right direction.</p>\n'}, {'answer_id': 30724, 'author': 'Chris Farmer', 'author_id': 404, 'author_profile': 'https://Stackoverflow.com/users/404', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d like to think my issue is "special" here, since I am using a custom form.  I chose to use a custom form rather than a custom FormTemplate simply because I\'m doing a lot of stuff that\'s not very SharePoint list-like (making ajax calls to get info from a third-party app then generating some dynamic form elements based on that ajax result, then subsequent processing of that data on postback).  I thought it\'d be a nightmare to try this within the usual custom rendering template mechanism.</p>\n\n<p>I also don\'t think I can supply the custom form declarations in the list definition itself, because I have multiple content types associated with this list, and each content type has its own custom form (the other type is thankfully much simpler).</p>\n\n<p>Actually, my simple way of keeping the list guid in my hidden field was a very low impact way to address this specific problem.  My main concern is that I\'m not sure why the SPContext just loses all its usefulness when I postback here, which makes me think I\'m doing something wrong.</p>\n'}, {'answer_id': 30828, 'author': 'Daniel McPherson', 'author_id': 897, 'author_profile': 'https://Stackoverflow.com/users/897', 'pm_score': 2, 'selected': False, 'text': '<p>I would be surprised if you could do something in a _Layouts file that you can\'t do in a forms template. You have pretty much the same technologies at your disposal. </p>\n\n<p>Looking at the way SharePoint works with ListItems and Layouts pages (for example "Manage Permissions" on a list item), I can see that they pass some variables in via querystrings:\n?obj={76113B3A-FABA-4389-BC85-4BB2CC5AB423},6,LISTITEM&amp;List={76113B3A-FABA-4389-BC85-4BB2CC5AB423}</p>\n\n<p>Perhaps they grab the context back each time programmatically using these values.</p>\n'}]
['https://Stackoverflow.com/questions/29030', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/404/']