How to Use SPForm and SPFormCollection

When writing WebParts that are going to heavily interact with lists, you will end up working with the associated form pages that are offered when massaging list items, so, you will end up working with the SPForm and SPFormCollection class. An SPForm The SPForm and SPFormCollection class derives from the Microsoft.SharePoint namespace, and while the SPForm object simply represents one of the forms associated with the list, the SPFormCollection is just a collection of those same SPForm objects.

So, let’s firstly look at some of the basic code required to interact with these pieces. What I want to show you how to do right now is iterate through a set of form objects contain in a form collection. While iterating through the SPForm objects in SPFormsCollection, we can then use the Microsoft.SharePoint.PAGETYPE enumeration which contains the types of available forms, and the Type property of the SPForm object in order to build a simple operator comparison. In the last part, you can see after the test that we are assigning the url strings (which will be consumed in a minute) with the site url (which is just passed in via a SPSite site = new SPSite(this.myUrl); and then using the OpenWeb() method), and then appending the secondary portion using the url property of the SPForm object.

public SPFormCollection formsCollection;

public string viewItemUrl = “”;

public string newItemUrl = “”;

public string editItemUrl = “”;

foreach (SPForm form in this.formsCollection)
this.newItemUrl = this.myUrl + “/” + form.Url;
this.editItemUrl = this.myUrl + “/” + form.Url;
this.viewItemUrl = this.myUrl + “/” + form.Url;


So, now you have a test, which is great. But what can I do with it? Well, the most common use for this type of loop is to make your own item links for hooking up to items in a SharePoint list. Let’s use the viewItemUrl string as an example.

Now that you have the viewItemUrl string populated, you can use the ID property of a SharePoint List object (which you could collect all of them in an SPListItemCollection doing something like foreach (SPListItem item in SPListItemCollection )), and create fun links to create list interactivity in your WebParts.



It’s a pretty fun way to create all sorts of embedded list interactivity, and can help you when creating WebParts that are using SharePoint as the native data store.


4 thoughts on “How to Use SPForm and SPFormCollection”

  1. I have a form library and from that I can get each SPForm from the SPFormCollection. But how do i get the values in each field (I require some method like GetItems in SPListItem). Is there some way for me to convert the SPForm to SPListItem to get the data in the fields?

  2. > I have a form library and from that I can get each SPForm from the SPFormCollection. But how do i get the values in >each field (I require some method like GetItems in SPListItem). Is there some way for me to convert the SPForm to >SPListItem to get the data in the fields?

    I would certainly like to do that!

    I designed a bulk editor webpart for the fields of a customer’s document library. Problem I found, when I looked more closely, the customer’s document library happens to be a Form Library. When I noticed the discrepancy and pointed my webpart away from my development Document Library to a dev Form Library I created, only stock columns such as Modified and Modified By got edited.

    Is this something I should be using SPform and SPformCollection for? So far the documentation I’ve found seems bare-bones. I can’t figure out how to relate these to the list that contains the metadata I’d like to edit. Can anyboy point me to the right resources ?

  3. Regarding my issue, I looked high, I looked low…then talking with the user I refreshed my memory of InfoPath, which I think likely revealed the answer: In the publishing-window sequence of InfoPath at the bottom of the columns-promotion window is a checkbox, asking: Do you want the column to be editable from the Sharepoint UI?

    If you check yes, it proceeds to warn you the UI input won’t get the benefit of any validation or other form-embedded scripting that would run were the user entering data via your form.

    Checking that box seems to have hit the mark, giving my Visual-Studio-built webpart the access it needed.

Leave a Reply

Your email address will not be published. Required fields are marked *