HI all,
I have created an InfoPath Form that has a File Attachment controller. This attachment controller upload attachments to a Folder in a SharePoint document library . This folder is created using VSTA in Office 2010. The Document Library is "Scanned Docs". The new folder will be created using several fields form the InfoPath Form (TourName, Today, TravelAgent).
The Source Code is at the below.
{ /**** Code snippet for uploading the attachment
to a folder within a document library. ***/
/** Declaring the SharePoint Site Collection. **/
SPSite mySPSite = new SPSite("http://thekingsbury/");
{
/** Decalaring the SharePoint Site. **/
SPWeb myWeb = mySPSite.OpenWeb();
{
// Code to retrieve the new Document library name.
XPathNavigator xLibraryName = MainDataSource.CreateNavigator();
String NewLibraryName = xLibraryName.SelectSingleNode("/my:myFields/my:LibraryName", NamespaceManager).Value;
// Code to retrieve the new folder name.
XPathNavigator xFolderName = MainDataSource.CreateNavigator();
String NewFolderName = xFolderName.SelectSingleNode("/my:myFields/my:FolderName", NamespaceManager).Value;
/*
// Code to create the new library.
myWeb.Lists.Add(NewLibraryName, "Custom Library !", SPListTemplateType.DocumentLibrary);
myWeb.Update();
*
* */
// Code to create the new folder within the new library.
SPDocumentLibrary newDocLibrary = (SPDocumentLibrary)myWeb.Lists[NewLibraryName];
SPFolderCollection newFolders = myWeb.Folders;
newFolders.Add("http://thekingsbury/Scanned Docs/" + NewFolderName + "/");
newDocLibrary.Update();
/** Code snippet for uploading the attachment to the newly created folder. **/
// Retrieve the value of the attachment in the InfoPath form
XPathNavigator ipFormNav = MainDataSource.CreateNavigator();
XPathNavigator nodeNav = ipFormNav.SelectSingleNode("/my:myFields/my:AgentCopy", NamespaceManager);
String attachmentValue = String.Empty;
if (nodeNav != null && !String.IsNullOrEmpty(nodeNav.Value))
{
attachmentValue = nodeNav.Value;
// Decode the InfoPath file attachment
InfoPathAttachmentDecoder dec = new InfoPathAttachmentDecoder(attachmentValue);
String fileName = dec.Filename;
byte[] data = dec.DecodedAttachment;
/** // Code to retrieve the new folder name.
XPathNavigator xFolderName = MainDataSource.CreateNavigator();
String NewFolderName = xFolderName.SelectSingleNode("/my:myFields/my:FolderName", NamespaceManager).Value;
**/
// Add the file to a document library
using (SPSite site = new SPSite("http://thekingsbury"))
{
using (SPWeb web = site.OpenWeb())
{
String tempFolder = "Scanned Docs" + "/" + NewFolderName + "/";
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.Folders[tempFolder];
docLib.Files.Add(fileName, data);
web.AllowUnsafeUpdates = false;
web.Close();
}
site.Close();
}
}
}
}
}But I've got an error in the
SPFolder docLib = web.Folders[tempFolder];
statement saying "Value does not fall within the expected range.". This statement is at the
// Add the file to a document library
using (SPSite site = new SPSite("http://thekingsbury"))
{
using (SPWeb web = site.OpenWeb())
{
String tempFolder = "Scanned Docs" + "/" + NewFolderName + "/";
web.AllowUnsafeUpdates = true;
SPFolder docLib = web.Folders[tempFolder];
docLib.Files.Add(fileName, data);
web.AllowUnsafeUpdates = false;
web.Close();
}
site.Close();
}code block.
I cannot understand what this error is for. I have stuck in here. Could someone help me to solve this error.
Thank You,
Regards,
Chiranthaka