Skip to main content

Cloud

Custom SharePoint List Forms. The C# way…. ( Part 2 )

Accessing the values of the controls is actually very easy. All the controls that you render using the FormField class are collected and stored in a collection. This collection resides in the current FormContext.

To access the collection use the following statement: SPContext.Current.FormContext.FieldControlCollection

This collection has all the fields that were rendered using the FormField class. The FormField class when instantiated registers itself in the FieldControl collection.

Toolbar and Attachments:

To make the UI look as close to the Out-Of-The-Box (OOTB) form, you might want to use the FormToolBar class. This will render the form tool bar.

To use the OOTB attachments functionality, there are few things that you need to do.

1. ID of your Field control table should be “part1”

2. Write a small javascript which will hide the element with the id “idAttachmentsTable” on page load.

blog1

The javascript should hide the following component which is displayed when the user clicks “Attach File”.

blog2

To complete the functionality we have to implement two actions:

1. Adding a new Attachment to the item.

2. Deleting attachments from the item.

The posted files collection is stored in the Request.Files collection. Here is a code snapshot that could be used to add the posted files to the attachments collection of an item.

blog3

To delete the attachments from the server, we have to get hold of all the GUIDS of files which the user have marked for deletion. They are stored in a form variable called “attachmentsToBeRemovedFromServer”.

This variable has the list of UniqueId’s seperated by semicolon. The question now is how to delete an attachment based on just its guid. The trick is to get hold of the sub folder which contains all the attachments.

if (_currentList.RootFolder.SubFolders.Count > 0)
{
if (_currentList.RootFolder.SubFolders[“Attachments”] != null)
{
SPFolder itemFolder = _currentList.RootFolder.SubFolders[“Attachments”];
if (itemFolder.SubFolders[_currentItem.ID.ToString()] != null)
return itemFolder.SubFolders[_currentItem.ID.ToString()].Files;
}
}

Once you get the file collection just iterate over and compare the UniqueID property with the ID’s that you get from the form variable.

If there is a match, just call Delete on the file object.

*Delete the attachments after you have called update on the item to save the other field values, else you will get a save conflict error.

The next part will explain some workarounds for attachment errors that you might see and how to access values for fields like RichHtml.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Amol Ajgaonkar

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram