Saturday, October 27, 2007

Creating a new form in a list by copying an existing form (in code)

To copy an existing form in a WSS list, and to add a new content editor web part to that list, use the SPFile class (not the SPForm collection of the list):

//Copy the NewForm.aspx to NewForm_new.aspx
string formPath = "Lists/ClientTest/NewForm.aspx";
string newformPath = formPath.Replace(".aspx", "_new.aspx");
SPFile form = web.GetFile(formPath);
form.CopyTo(newformPath, false);

//Get a reference to the new form, and create a new content editor web part containing onload JavaScript
SPFile copiedForm = web.GetFile(newformPath);
SPLimitedWebPartManager wpManager = copiedForm.GetLimitedWebPartManager(PersonalizationScope.Shared);

//Create the new web part
ContentEditorWebPart wpContent = new ContentEditorWebPart();
XmlDocument xmlContent = new XmlDocument();
XmlElement el = xmlContent.CreateElement("El");
el.InnerText = "";
wpContent.Content = el;
wpManager.AddWebPart(wpContent, "Main", 0);

The JavaScript adds an onload handler to the page, allowing for behaviours to modify the content of the standard list form for example.

Thursday, October 18, 2007

Error in Custom Document Library View

When creating custom document library definitions using the view files output by the SharePoint Solution Generator, you may see the following error on the custom view pages in an instance of the libary:

An error occurred during the processing of ....... Only Content controls are allowed in a content page that contains Content controls.

This is caused by invalid text in the ASPX file - the solution generator adds a comment XML node to the top of each view file, and this needs to be deleted before deploying that view.

Checkin Page Shows Error For Document Library

Have been very busy creating lots of solutions to define a raft of lists and document libraries. One technique I have experimented with is as follows:
  • Manually define the site containing the lists
  • Use the SharePoint Solution Generator to create schemas for the lists
  • Run my own tool across these schemas to generate site column and content type definitions. The tool also removes some of the invlaid attributes in the schema field definitions that are created by the solution generator (I'll publish the tool here sometime soon)
  • Create solutions and features in Visual Studio using these XML definitions.
This has been saving me lots of manual effort in creating all the necessary XML. My reason for not using the VSeWSS projects for list definitions is I prefer control over the feature and manifest files, so that I can add other elements where necessary.

I came across an issue with a document library created using one of the custom list definitions. Everything seemd to work in the document library except for checking in a document - the checkin.aspx page simply displayed an error stating

"Value does not fall within the expected range"

Don't ya love that one!! Lots of investigation uncovered the cause - a malformed content type. The document library used one custom content type, and one of the field elements in the field section of the document library schema.xml file had a mispelt name attribute. Amazing that I could still add a document, and could edit the metadata values!