1

I have some code which simply gets a list, creates a new item with the appropriate fields, and adds the item to the list. The code worked perfect in the dev and UAT environments but since we deployed it to production it has stopped working.

The overview of how it was set up is a user fills out a form on the page and an ajax call sends the information to a code behind. Then the code behind creates a new item and adds that to the list.

This is the template i am using for the code:

try
{
    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists["List Name"];
            SPListItem itemToAdd = list.Items.Add();
            itemToAdd["Title"] = "Some Value";
            itemToAdd.Update();
            web.AllowUnsafeUpdates = false;
         }
    }
}

The dev servers are 100% on an intranet. The prod server is in the intranet, but has alternate access mappings to an extranet. The code does not execute when attempting from either though.

Does anyone have an idea for this situation?

Thanks

Solved it,

The list i was referencing in production had been altered by someone while i was working in dev. One of the column names was changed and i did not see it because there were 20 or so fields.

Thanks Dave Wise for the advice on the rootweb/openroot. I was not aware of the differences so you have saved me many future headaches.

Thanks Thantos for recommending the logging to the ULS logs, that was how i was able to discover the error.

7
  • So the code simply does not execute? May 2, 2013 at 15:41
  • It is hard to say, on success it returns true and the ajax call displays a success message which is being displayed after button click. If the code fails it should hit the catch and return false, displaying an error message. But without attaching to a debugging process i cannot be 100% positive that it is reaching the code
    – heilch
    May 2, 2013 at 15:47
  • Do you have any kind of logging in the server side function where you could see if that is failing? May 2, 2013 at 15:51
  • I'll try that quick and post the relevant log entries
    – heilch
    May 2, 2013 at 15:54
  • One bug is your using() block on the 'web' variable but are getting its value value via site.RootWeb instead of the .OpenWeb() (which is when a using() is required) RootWeb merely returns an handle to an existing object and by using it in this manner you are destroying the .RootWeb object before page execution completes which could cause any number of issues for downstream code
    – Dave Wise
    May 2, 2013 at 16:02

1 Answer 1

0

Could it be a permission issue? Since you usually have admin rights on the dev environment, that's not the case for normal users and some of them might not have permission the specific web or list.

This can be fixed by using a SPSecurity.RunWithElevatedPrivileges block around the outer using block.

1
  • If i am a farm admin on both servers would i still require this? So the tricky thing here is that i came in to this project after a previous developer got 85% done and quit. The page was made from scratch and deploys to _layouts and i was not given the option of recreating the page/site properly. So the code is not in a web part or sharepoint page at all, it just executes from the _layouts folder. That being said, I can try that to see if it works. It will be interesting to see what happens.
    – heilch
    May 2, 2013 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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