Sunday, April 20, 2014

SharePoint 2013 - Document Library and Custom Field Type

We created a very simple custom field control to filter some custom data from SQL database, and the code behind is only the necessary constructors and one overload. We created a custom content type that inherits from Folder, and associate our site column/field type to the new content type. We then add that content type to a Document Library, and when we attempt to create the new 'folder' (our content type), it has only two columns: Name and My Custom Field. When we try to save it, Name blanks out and doesn't give us any information, validation error or exception. It just won't allow us to save. Weird !!!!

After some googling and search I've found that great blog Custom field types and rendering templates correlation with the new “Server Render” property of the ListFormWebPart .

In this article you will find that the problem is that my custom field type is using Server Rendering template, While SharePoint 2013 document library ListFormWebPart is using CSR [Client-Side Rendering] .

Then you have a solution, by following the Sridhar's article by editing the New & Edit form for your library changing the ListFormWebPart , find the “CSR Render Mode” option under “Miscellaneous” section.  Just choose “Server Render (ServerRender)” for the “CSR Render Mode” option .

But what about automating this process, In  my scenario I had a SharePoint Project including the field type, content type, Document library Template and List definition. All connected together

I had a web scoped  feature to provision this document library on containing this Field type.

So what I've to do to make all this work, Is to specify  Sridhar's solution in the feature activated event receiver, The below code snippet is what you have to do to get that done:




private void ChangeSentDocumentListFormWebPart(SPList SentDocLibList)
        {
            SPDocumentLibrary SentDocLib = (SPDocumentLibrary)SentDocLibList;
 
                        // Update forms
            foreach (SPForm spForm in SentDocLib.Forms)
            {
                if (spForm.Url.Contains("DispForm.aspx") || spForm.Url.Contains("EditForm.aspx") || spForm.Url.Contains("Upload.aspx"))
                {
                    string fileURL = SentDocLib.ParentWeb.Url + "/" + spForm.Url;
                    SPFile page = SentDocLib.ParentWeb.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.CSRRenderMode = CSRRenderMode.ServerRender;
                                    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.AllowUnsafeUpdates = false;
 
                                lwpm.Web.Dispose(); // SPLimitedWebPartManager.Web object Dispose() called manually
                            }
                        }
                    }
                }
            }
        }

1 comment: