Code Snippet – Programmatically Update the SharePoint Web Part Properties on the List Forms

If you ever want to update the web part properties on the out of the box SharePoint List forms programmatically, use the following code snippet. Thanks to Fransisco’s article for the inspiration. I have updated his code with better memory check.

Typically you wouldn’t want to change List form web part properties but we came across very interesting situation where we had to update these web part properties. We had several document libraries created on the several sites which were further created based on the site template (user site templates stored in the site collection solution library). Whenever we add new custom content types on the document libraries on these sites, document library list form wasn’t displaying custom metadata attached to the custom content types in the edit form. By default, document library list form web part template name property is set to “DocumentLibraryForm”. Updating the template name property to the “ListForm” would fix this issue. This is one of the curious cases of SharePoint. We have tested the same scenario on the document libraries on the sites created based on site definition (out of box site templates) and we didn’t have these issues. This issue was affecting only document libraries in the sites where sites were created using user site templates.

The scenario I have here is I wanted to modify the ListFormWebPart’s TemplateName proprety on the Upload.aspx, EditForm.aspx, and DispForm.aspx pages of all the document libraries for all the sites in given site collection. This code snippet checks out the files, updates the list form web part properties, and checks in the files.

</pre>
&nbsp;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls.WebParts;

class Program
{
    static void Main(string[] args)
    {
        SPSite spSite = new SPSite("<a href="http://sp2010vm">http://sp2010vm</a>");
        foreach (SPWeb spWeb in spSite.AllWebs)
        {
            spWeb.AllowUnsafeUpdates = true;

            SPListCollection lists = spWeb.Lists;
            for (int i = 0; i < lists.Count; i++)
            {
                if (lists[i] is SPDocumentLibrary)
                {
                    if (((SPDocumentLibrary)lists[i]).IsCatalog == false)
                    {
                        SPDocumentLibrary spList = (SPDocumentLibrary)lists[i];

                        // Update forms
                        foreach (SPForm spForm in spList.Forms)
                        {
                            if (spForm.Url.Contains("DispForm.aspx") || spForm.Url.Contains("EditForm.aspx") || spForm.Url.Contains("Upload.aspx"))
                            {
                                string fileURL = spWeb.Url + "/" + spForm.Url;
                                SPFile page = spWeb.GetFile(fileURL);

                                using (SPLimitedWebPartManager lwpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
                                {
                                    try
                                    {
                                        // Enable the Update
                                        lwpm.Web.AllowUnsafeUpdates = true;

                                        // Check out the file, if not checked out
                                        SPFile file = lwpm.Web.GetFile(fileURL);
                                        if (file.CheckOutType == SPFile.SPCheckOutType.None)
                                            file.CheckOut();

                                        // Find the ListFormWebPart and Update the Template Name Property
                                        foreach (System.Web.UI.WebControls.WebParts.WebPart wp in lwpm.WebParts)
                                        {
                                            if (wp is Microsoft.SharePoint.WebPartPages.ListFormWebPart)
                                            {
                                                Microsoft.SharePoint.WebPartPages.ListFormWebPart lfwp =
                                                    (Microsoft.SharePoint.WebPartPages.ListFormWebPart)wp.WebBrowsableObject;
                                                lfwp.TemplateName = "ListForm";
                                                lwpm.SaveChanges(lfwp);
                                            }
                                        }

                                        // Update the file
                                        file.Update();
                                        file.CheckIn("System Update");

                                        // Disable the Unsafe Update
                                        lwpm.Web.AllowUnsafeUpdates = false;
                                    }
                                    finally
                                    {
                                        if (lwpm.Web != null)
                                        {
                                            lwpm.Web.Dispose(); // SPLimitedWebPartManager.Web object Dispose() called manually
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            spWeb.AllowUnsafeUpdates = false;
            spWeb.Dispose();                  
        }

        spSite.Dispose();
    }
}

Hope it is useful.

Advertisements
This entry was posted in SP2010 Code Snippets. Bookmark the permalink.