Wednesday, October 29, 2008

How to populate a list with subdirectories of a SharePoint Library

Introduction

When creating subdirectories in SharePoint Library, you can’t submit directly to one the sub directories. Also when users add or change the subdirectories, how to handle this in InfoPath.


Compatibility

InfoPath 2003 and 2007: Full Support
Form Serices:  No Support

Designing the Form Template

  1. Create the Receiving Data Connection
    1. Go to Menu Tools ->Data Connections
    2. Click the Add button
    3. Select Receive data
    4. Select SharePoint library or list
    5. Enter the path to your SharePoint Site
    6. Select the Library
    7. Select Item and Content Type

clip_image002

Figure 1: Select Item and Content Type

    1. Set the name to SubDirectories
  1. Add the Dropdown list to your form
    1. Add a dropdown list box to the form
    2. Double-Click the newly added list box, to open the Properties Dialog Box
    3. Select ‘Look up values in a data connection ,….’ In the list box entries section
    4. Select SubDirectories in the Data Connection Dropdown listbox
    5. Click on the ‘Select xPath’ button clip_image003 next to the Entries textbox
    6. Select the repeating node from the list (= SharePoint Library Name)
    7. Click on the Filter Data button
    8. Select Content_Type in the first dropdown list box
    9. Enter the text Folder in the third dropdown list box

clip_image005

Figure 2: Filter Data settings

    1. Click 3x the Ok button
    2. Set Value and Display Name to @Item
    3. Click the Ok button
  1. Test the form

Submitting the form to the selected sub directory

  1. Create a Submit Connection to the main SharePoint Directory
    1. Go to Menu Tools ->Data Connections
    2. Click the Add button
    3. Select Submit data
    4. Select To a SharePoint library
    5. Enter the Path of the Main SharePoint Library
    6. Set the file name
    7. Click the Finish button
  1. Set the submitting options
    1. Go to Menu Tools ->Submitting forms
    2. Select Enable Submit Commands and buttons
    3. Select Custom Submit using form Code in the Submit to dropdown list box
    4. Select Edit Form Code
    5. Click the Ok button

clip_image006 Note:

The Microsoft script editor will open

Point f : is the code using InfoPath 2003

Poit g : is the code using InfoPath 2007

    1. Add the following code to the XDocument::OnSubmitRequest function

Code for InfoPath 2003

//-- Create string objects for each field we will use to modify the FolderUrl –

var xnLocation = XDocument.DOM.selectSingleNode("my:myFields/my:DynamicInfo/my:xnLocation").text;

//-- Get a reference to the submit data connection –

var fc = XDocument.DataAdapters["Submit"];

//-- Modify the Submit connection URL we want to submit to by concatenating the //-- xnLocation and xnFolderName values –

fc.FolderURL = fc.FolderURL + "/" + xnLocation;

//-- Execute the submit connection –

try

{

fc.Submit();

eventObj.ReturnStatus = true;

}

catch(ex)

{

eventObj.ReturnStatus = false;

}

    1. Add the following code to the FormEvents_Submit function

Code for InfoPath 2007

try

{

// Get a reference to the form's XmlNamespaceManager object.

XmlNamespaceManager ns = this.NamespaceManager;

// Create an XPathNavigator object for the form's main data

// source.

XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();

XPathNavigator xnCountry =

xnDoc.SelectSingleNode("/my:myFields/my:country", ns);

if (xnCountry != null)

{

FileSubmitConnection dc =

(FileSubmitConnection)this.DataConnections["Main submit"];

if (dc != null)

{

dc.FolderUrl = dc.FolderUrl + "/" + xnCountry.Value;

dc.Execute();

e.CancelableArgs.Cancel = false;

}

}

}

catch (Exception ex)

{

e.CancelableArgs.Message = "There was a problem submitting the

form:\n\n" + ex.Message;

e.CancelableArgs.Cancel = true;

}

    1. Save and Close the Microsoft Script Editor
    2. Finish the InfoPath 2003 form with the other business requirements
    3. Publish the Form Template to the SharePoint document library or document libraries

1 comment:

Anonymous said...
This comment has been removed by a blog administrator.