Wednesday, November 13, 2013

Saving InfoPath form back to subfolder in SharePoint library


Recently I needed to save an InfoPath 2007 ( :-P ) form back to its original location. The original location was on a subfolder inside a SharePoint form library, and the subfolder was created dynamically during form creation time, named based on some data that was not available on the form itself. So there was no way (at least I didn’t see one) I could create a form submit connection that enabled me to save the form back to where it was.

I’ve Googled for a solution and the only one that looked ok was to include the form URL as a field on the form itself, so I could retrieve it later when saving the form back to the SharePoint library. But since the form is running inside SharePoint, and it is an item in a list, I thought I might give it a try to the SharePoint Server API inside the form code behind, and it worked! I used SPWeb.GetListItem() to retrieve the item in SharePoint corresponding to the form, and then used the item’s properties to retrieve its location:

     public void FormEvents_Submit(object sender, SubmitEventArgs e)
     {
         // Retrieves reference to the form in the SharePoint library based on its URL
         SPListItem item = SPContext.Current.Web.GetListItem(this.Uri);
         // Saves the form. There must be a connection on the form named “SaveFile”, pointing to the
         // SharePoint library. In the following code the connection generates the file name, but if the file
         // name must be generated at run-time, it can be set through FileSubmitConnection.FileName
         FileSubmitConnection spConnection = (FileSubmitConnection)this.DataConnections["SaveFile"];
         spConnection.FolderUrl = item.Web.Url + item.File.ParentFolder.ServerRelativeUrl;
         spConnection.Execute();
         e.CancelableArgs.Cancel = false; // Tells InfoPath that form submit was ok
     }

1 comment:

  1. The information that you have posted helped me a lot as I got to know so many new and useful facts about this concept.

    ReplyDelete