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);
}
}
}