Question

I am working on a project that requires code behind in the dispform.aspx as well as the edit and new form. The reson being, I am synching to a web service, on view of an item, I want to make a sall to the web service to get updated data on the record in SharePoint.

I've looked a list definitions, using delegate controls (I was able to link delegate control but unable to find controls in the display form, seems they are in a table without ids etc)

I have created an application page in an empty SharePoint project in Visual Studio 2010, added a listviewwebpart (rather use instead of dataview because the lookup values are displayed as hyperlinks). This page resides in the _Layouts folder.

The question is, how do I add code behind to the OOB dispform.aspx page without having to use list definitions, etc. If I can use the form I created in the layouts folder, can I use this for my list display? (SharePoint 2010).

Was it helpful?

Solution

As you already discovered, one way is to use the DelegateControl approach (with PlaceholderAdditionalPagehead) and that's in fact what I've been using for a couple of years now.

The key is how to access the built-in editform.aspx/dispform.aspx pages with the embedded WebPart showing the SPListItem. That's done by hooking your WebControl into OnPreRender/OnLoad to check SPContext.Current.FormContext.FormMode if you are on a SharePoint form page.

Then you need to find the ListFormWebPart and get your hands on the fields you are looking for.

Basically something like the pseudo-code below (though it is much simplified for brevity):

First, get the ASP.NET controls of the current page:

this.Page.Controls

Then find the ListFormWebPart (either loop recursively or use another smart way to find it deep down the controls collection):

if (typeof(ListFormWebPart) == childControl.GetType())
ListFormWebPart listFormWebPart = (ListFormWebPart)childControl;

Finally, dive into the controls collection of the ListFormWebPart to find the ListFieldIterator control (again, either loop recursively or use another smart way to find it deep down the controls collection) which itself contains a collection of the actual fields of the SPListItem. E.g. for a text field:

if (typeof(TextField) == formFieldChild.GetType())
{
  TextField textField = (TextField)formFieldChild;
  if (textField.FieldName == "YourFieldNameGoesHere")
  {
    // Get the field value and do with it whatever you want
    string myFieldValue = textField.Text;
  }
}

Another way would be to use client-side JavaScript and the CSOM to pull data from the selected item (e.g. by getting the SPListItem ID from the Url of Dispform.aspx and query the SharePoint Webservice via REST/SOAP) and push it via XmlHttpRequest to your custom WebService.

Finally, the last thing that comes into my mind is to develop a custom ControlAdapter to hook into the ListFormWebPart and read its fields etc., though I haven't tried this myself because I'm quite happy with the DelegateControl approach mentioned above.

Hope that helps.

EDIT: Added a few lines to clarify things a little bit more.

OTHER TIPS

Here are two possibilities for getting custom code on a list page:

EDIT

You could also use InfoPath. In SharePoint 2010 you can customize the list form to use InfoPath for new/edit/display forms. And in the InfoPath form, you can use code behind or the built-in functionality of InfoPath to call your web services.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top