Cannot connect to a SharePoint Site Collection in code. - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Cannot connect to a SharePoint Site Collection in code.

Last post 01-08-2009 01:05 PM by toast32. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 01-07-2009 07:15 AM

    Cannot connect to a SharePoint Site Collection in code.

    What I want to do is create a SharePoint document library folder based on values in an InfoPath form.
    I have a console app that copies a folder from a template folder. When I add this code to the submit event, I recieve the following error:

    The Web application at http://moss64 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.

     I just learned as well that working with SharePoint it appears that one should use web services and not the object model. Any comments on this would be appreciated.

    Based on a post, I added the line that is commentted out in my sample code.  If this is enabled, I recieve the following error.


    System.InvalidOperationException
    This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to the server farm, use the SharePoint Products and Technologies Configuration Wizard, located on the Start menu in Administrative Tools.
       at Microsoft.SharePoint.Administration.SPWebApplication.Lookup(Uri requestUri)
       at codeBehindFormTemplate.FormCode.FormEvents_Submit(Object sender, SubmitEventArgs e)
       at Microsoft.Office.InfoPath.Internal.FormEventsHost.OnSubmit(DocReturnEvent pEvent)
       at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnSubmitRequest(DocReturnEvent pEvent)

    Any ideas would be appreciated.  Thanks.

    Below is the code:

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Text;
    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.Xml;
    using System.Xml.XPath;
    using mshtml;

    namespace codeBehindFormTemplate
    {
        public partial class FormCode
        {
            // Member variables are not supported in browser-enabled forms.
            // Instead, write and read these values from the FormState
            // dictionary using code such as the following:
            //
            // private object _memberVariable
            // {
            //     get
            //     {
            //         return FormState["_memberVariable"];
            //     }
            //     set
            //     {
            //         FormState["_memberVariable"] = value;
            //     }
            // }

            // NOTE: The following procedure is required by Microsoft Office InfoPath.
            // It can be modified using Microsoft Office InfoPath.
            public void InternalStartup()
            {
                EventManager.FormEvents.Submit += new SubmitEventHandler(FormEvents_Submit);
            }

            public void FormEvents_Submit(object sender, SubmitEventArgs e)
            {
                // If the submit operation is successful, set
                // e.CancelableArgs.Cancel = false;
                // Write your code here.
                MessageBox.Show("Hello " + this.Application.User.UserName);
               
                //******* The following two lines cause the errors.
               // SPWebApplication wa = SPWebApplication.Lookup(new Uri("http://moss64"));
               
                SPSite site = new SPSite("http://moss64"); //Reference Site
                SPWeb web = site.AllWebs["InfoPath"];  //Refencence web site
                SPList docLibList = web.Lists["InfoPath Forms Library"]; //References LIst
                //Set variables
                string newEmployeeName = "Joe Strummer";
                string urlEmployeeFolder = "InfoPath Forms Library/" + newEmployeeName;
                // Check if Folder already exists.  Exit if it already exists.
                if (web.GetFolder(urlEmployeeFolder).Exists)
                //if (web.GetFolder("InfoPath Forms Library/Joe Strummer").Exists)
                {
                    //Don't do anything if it exists
                }
                else
                {
                    // Create Copy of template folder
                    if (web.GetFolder("InfoPath Forms Library/Employee Folder").Exists)
                    {
                        SPFolder orgFolder = web.GetFolder("InfoPath Forms Library/Employee Folder");
                        orgFolder.CopyTo(urlEmployeeFolder);
                    }

                }
            }

  • 01-08-2009 01:05 PM In reply to

    Re: Cannot connect to a SharePoint Site Collection in code.

    I have been able to progress in solving this issue.

    My guess is that one cannot use the object model in a code behind page in InfoPath.  It is now obvious, (I think) that an InfoPath form with a code behind page using the object model would not be much use to any remote users. 

     Utilizing web services I have been able to create folders.  Now I just need to figure out how to populate the folder with document templates.

Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.