I have a web part and I dynamically add a List field iterator to it:

//contentType is the content type ID

SPList list = web.Lists[listName];

SPContentType spct = web.AvailableContentTypes[contentTypeName];
Iterator = new ListFieldIterator()
     ControlMode = SPControlMode.Display,
     TemplateName = spct.NewFormTemplateName,
     ListId = list.ID
Iterator.ListItem["ContentTypeId"] = contentType;
Iterator.EnableViewState = true;

var currentMode = SPControlMode.New;
Iterator.ControlMode = currentMode;
PlaceHolder p1 = (PlaceHolder)this.FindControl("PlaceHolder1");

I have 2 questions:

  1. How I can hide the buttons that appear with the list field iterator?

  2. I want to add a method that create the item (Library list item with attachment) by code, by reading the item of list field iterator and create it, how I can do it?

1 Answer 1

  1. If you would like the buttons not to appear with the list field iterator, the simplest way is to customize your custom rendering template (.ascx) associated to list field iterator (spct.NewFormTemplateName in your case). If you would like only "hide" them, you can achieve it via jQuery as well.
  2. I blogged about how to perform special actions in our custom list field iterators (see here, here or here). The most important things to know:
    • You should handle the save event in your own method assigned to the OnSaveHandler of the form context.
    • In this step you can optionally performe validations if you need.
    • In case of SP2013, if your list field iterator would be injected onto your webpart via an ListFormWebPart instance, and not directly, you should set the CSRRenderMode to ServerRender, otherwise your server side code (like your OnSaveHandler) won't be invoked on postback. This is not relevant to your case, as far as I understand your code.
    • The item properties are availabe in the form only as ASP.NET controls, but not like item["YourProperty"] for example. You should find the controls and parse out the values.

You find a few helper methods in this post, as well in the sample project associated to it.

  • Hi pholpar, thank you for your reply, please note that i have a ddl contains Library content types, changing content type from ddl will bind the new content type form, so using ListFormWebPart how i can add the item to sharepoint dynamically based on the content type selected ?
    – ProDevx
    Jan 30, 2015 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.