System.NullReferenceException to infopath VSTA RRS feed

  • Question

  • Hi all,

    i am submiting data from infopath to Sharepoint 2007 list using custom code with VSTA.
     i am getting this error when clicking on submit button:

    The Web application at http://sharepointqa/global/Testing/ could not be found.
    Verify that you have typed the URL correctly. If the URL should be serving existing content,
    the system administrator may need to add a new request URL mapping to the intended application.

       at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
       at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
       at TemplateTesting.FormCode.BTN_Submit_Clicked(Object sender, ClickedEventArgs e)
       at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

    this is my code :
       XPathNavigator domNav = MainDataSource.CreateNavigator();
                XPathNavigator myfield = domNav.SelectSingleNode("/my:myFields/my:field", NamespaceManager);
              using (SPSite oSiteCollection = new SPSite("http://sharepointSerrver/test/"))
                  using (SPWeb oWebsiteRoot = oSiteCollection.OpenWeb())//("http://webcollectionname"))
                        SPList oList = oWebsiteRoot.Lists["EUVendorAccountRequest"];

                        SPListItem oListItem = oList.Items.Add();
                        oListItem["Title"] = myfield.ToString();


    pls back to me if have any idea about this.


    • Edited by D.Tiwary Friday, December 9, 2011 8:22 AM
    Monday, December 5, 2011 8:53 AM

All replies

  • Hi,

    • The user connected has got the permission to see the site?
    • The user connected has got permission to add a new item in the list?
    • Why have you got 2 SPSite in your code?

    If all is correct, please check in your VSTA the following parameter : Go to Project Properties -> Build -> Target Platform -> Make it Any CPU (default is x86).

    Hope this help.

    Visit my blog : http://rmatayron.blogspot.com/
    Monday, December 5, 2011 1:13 PM
  • onwthing i am working with SP2007.

    by mistek i have paste two SPSite in this blogs ,one SPSite have comment.

    i have full permission on sitecollection.

    and "Target Platform " don't have in my VSTA , it's in Visual studio application.

    see in below image: VSTA the following parameter : Go to Project Properties -> Build -> Target Platform -> Make it Any CPU (default is x86)."


    • Edited by D.Tiwary Monday, December 5, 2011 2:16 PM
    Monday, December 5, 2011 2:01 PM
  • Hi,

    I am using InfoPath 2010 and VSTA and I have the platform parameter on top of your screen shot (on the top of the general tab).

    Have you check into the application tab also?

    Or I have the paramater also in Visual Studio "Generate" menu and "configuration manager".


    Hope this help.


    Visit my blog : http://rmatayron.blogspot.com/
    Monday, December 5, 2011 3:59 PM
  • Hi,

    Is this Client Infopath form or browser form?

    If it is client form, then it will not work properly. It will only work if you have SharePoint dll on your development system.

    For client form you have to user SharePoint webservice List.asmx to update the list.


    Dare to promise
    Monday, December 5, 2011 8:19 PM
  • Hi,

     i am using Browser enable form.

    also upload in infopath form service but doesnt matter before i have to test in design mood.



    Wednesday, December 7, 2011 10:29 AM
  • sorry i am not getting this option anywhere in VSTA.

    is it problems in VSTA versions? .


    Wednesday, December 7, 2011 10:39 AM
  • here by default selected (platform target="Any CPU" ) in below image:

    Friday, December 9, 2011 1:06 PM
  • Hi Deepak /

    I am having same issue and trying many solution still not success from a week.. trying to run  (http://blogs.msdn.com/b/infopath/archive/2010/03/18/introduction-to-programming-in-infopath-2010.aspx)

     I am using InfoPath2010 , VSTA 2005 & SP2010

    1) I developed code on server and tried to run with Server and Farm administrator permission.

    2) Gave all neccessary permisions


    3) User Office2010 64 bit and VSTA to make sure if is there any thing wrong with Platform

    4) Tried all @ http://stackoverflow.com/questions/266255/filenotfoundexception-with-the-spsite-constructor-whats-the-problem

    can't event open SPSite obejct. Error message doesn't change a single character :(

    Please post solution, if you get success



    Tej http://tejzatms.blogspot.com/

    • Edited by Tejz Sunday, December 11, 2011 2:27 AM
    Sunday, December 11, 2011 1:44 AM
  • Hi Remi,

    I am using Infopath2010 and VSTA 2005 browser base form for SP2010. I do not have any platform option. Dont have Generate and Configuration Manager option.

    I am using following version of VSTA . not sure how to get VSTA 2008, as VS2010 does not support.

    Struggling from a week. If you provide details  might help 


    Tej http://tejzatms.blogspot.com/
    • Edited by Tejz Sunday, December 11, 2011 2:36 AM
    Sunday, December 11, 2011 2:36 AM
  • I would make sure that you are doing actual development on a box that has SharePoint installed. 

    If yes, then I would verify that you can browse to the site you are trying to connect to.

    Then, in both of the above examples:




    are not SPSite

    You should be using:

    ... = new SPSite(http://sharepointqa);

    ... = new SPWeb("global/testing");

    I would check the SPContext.Web and SPContext.Site to see what the values are.

    If this doesn't work, then you need to wrap your new SPSite(...) in elevated priveledges in order to access it.


    Monday, December 12, 2011 6:28 AM
  • yes , i am working remotely on the server.

    now getting this error with RunWithElevatedPrivileges method and try block :("System.NullReferenceException
    Object reference not set to an instance of an object.")

    without RunWithElevatedPrivileges method and try block  now i am using this code:


    XPathNavigator xnTargetSite = MainDataSource.CreateNavigator().SelectSingleNode("//my:field2", NamespaceManager);


    MessageBox.Show("Hello!!!");//only this event is working








    using (SPSite site = new SPSite("http://sharepointqa/global/project"))//not working

    using (SPSite site = new SPSite("http://sharepointqa))//not working

    using (SPSite site = new SPSite(http://sharepointqa/))//not working

    //http://sharepointqa/global/Project/TestNew/Forms/AllItems.aspx //not working



    using (SPWeb web = site.OpenWeb("global/project"))//not working

    using (SPWebweb = site.OpenWeb())//not working


    web.AllowUnsafeUpdates =




    SPList taskList = web.Lists["TestNew"];


    SPListItem taskItem = taskList.Items.Add();


    "Title"] = MainDataSource.CreateNavigator().SelectSingleNode("//my:xnTargetSite", NamespaceManager).Value;


    web.AllowUnsafeUpdates =







    catch (Exception ex)



    throw ex;


    Monday, December 12, 2011 8:57 AM
  • I am working on server, it's single server farm. I am having same issues as Deepak, same code runing and tried with elivated permission also. Run under Office 64 bit VSTA. my spsite object

     SPSite site = new SPSite("http://sharepointqa/sites/project");   --- not working

    I have added my account to

    1) SQL Server  sa db owner for SharePoint databases.


    3) I am admin of the box & for SharePoint Farm & my Site colletion owner.

    Same I tried on my SharePoint VHD & runs as charm also runs using Console app ... I think some permission issue but Dont know where something missing.

    Tej http://tejzatms.blogspot.com/
    • Edited by Tejz Monday, December 12, 2011 4:02 PM
    Monday, December 12, 2011 3:59 PM
  • did you try this ?

    To target x86 in the express editions:

    Tools --> Options --> Projects and Solutions-->General  Check "Show advanced build configurations"
    If "Configuration Manager" doesn't show on the Buid menu, add it and click it.
    Active Solution Platform --> New --> Type or select the new platform x86

    Tuesday, December 13, 2011 7:39 AM
  • Finally, Issue is resolved, make sure all condition + user account must be part of Application Pool Identity & has access of sharepoint database. also user office 2010 64 bit VSTA.
    Tej http://tejzatms.blogspot.com/
    Tuesday, December 13, 2011 9:50 PM
  • how did you set this :

    user account must be part of Application Pool Identity & has access of sharepoint database.

    Wednesday, December 14, 2011 9:53 AM
  • My server is configured with a service account(e.g Domain\User) which setup as Application Pool identity account. I have to use same account to run & debug my code. I haven't tried to with Network / Local service account but It should.

    Make sure  whaterever account you logged  in, has  access of SQL server database & it must have db-owner role.

    Tej http://tejzatms.blogspot.com/
    Wednesday, December 14, 2011 4:16 PM
  • thanks for reply.

    i done this thing but not working.

    i am working on Server remotely from my local computer.

    Friday, December 16, 2011 10:33 AM
  • Please walk through your code with the debugger and provide the SPContext.Web and SPContext.Site values so that we can begin to narrow down the cause of the issue.


    Sunday, December 18, 2011 12:49 AM
  • Have you installed the SharePoint in correct way.

    If not please refer the document "SharePoint2010_selfstudy_RTM_Install.pdf".

    It could be an authentication issue.

    It was solved many of my problems like VSTA custom code, SSRS and etc.


    Thursday, September 20, 2012 9:37 AM
  • Give a try by adding "NT AUTHORITY\IUSR" as dbowner in sql server ( Server=>Security=>Logins), Let your code be in SPSecurity.RunWithElevatedPrivileges Method.

    If your list is on same site where InfoPath has deployed. use code like, 


                       SPWeb mySite = SPContext.Current.Web;

                       SPListItemCollection listItems = mySite.Lists["TestNew"].Items;

                       SPListItem item = listItems.Add();

                       item["Title"] = "your values";


    Friday, September 21, 2012 6:32 AM