3

I have a project that needs to associate custom New and Edit forms with list instances. The lists are not part of the project, but the custom forms are. I currently deploy them to /_layouts/ProjectFolder. I’ve tried several similar ideas to the code below, but can’t seem to find a way to get these forms into the list’s form collection. The code below fails on the list.RootFolder.Files.Add with a DirectoryNotFoundException. The idea is to create the blank form, which is what I thought the 2 SPFile form = list.RootFolder.Files.Add would do, then save the FileStream of the pages in /_layouts/ into the forms.

Anyone know how to do this?

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using(SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))
                    using (SPWeb elevatedWeb = elevatedSite.OpenWeb(SPContext.Current.Web.ID))
                    {
                        //TODO: Save default NewFormUrl & EditFormUrl to PropertyBag
                        list.RootFolder.Properties["DefaultNewFormUrl"] = list.DefaultNewFormUrl;
                        list.RootFolder.Properties["DefaultEditFormUrl"] = list.DefaultEditFormUrl;

                        FileStream newFile = File.OpenRead(SPUtility.GetGenericSetupPath(@"\TEMPLATE\LAYOUTS\ProjectFolder\") + "FormNew.aspx");
                        FileStream editFile = File.OpenRead(SPUtility.GetGenericSetupPath(@"\TEMPLATE\LAYOUTS\ProjectFolder\") + "FormEdit.aspx");
                        SPFile newForm = list.RootFolder.Files.Add(list.ParentWeb.ServerRelativeUrl + list.RootFolder.Url + "/FormNew.aspx", SPTemplateFileType.FormPage);
                        SPFile editForm = list.RootFolder.Files.Add(list.ParentWeb.ServerRelativeUrl + list.RootFolder.Url + "/FormEdit.aspx", SPTemplateFileType.FormPage);
                        list.RootFolder.Update();

                        try
                        {
                            byte[] newDocumentBytes = new byte[newFile.Length];
                            byte[] editDocumentBytes = new byte[editFile.Length];

                            elevatedWeb.ParserEnabled = false;
                            elevatedWeb.Update();
                            newForm.SaveBinary(newDocumentBytes);
                            editForm.SaveBinary(editDocumentBytes);
                            newForm.Update();
                            editForm.Update();
                            elevatedWeb.ParserEnabled = true;
                            elevatedWeb.Update();

                        }
                        catch (Exception ex) { }


                        list.DefaultNewFormUrl = list.ParentWeb.ServerRelativeUrl + list.RootFolder.Url + newForm.Url; //"/_layouts/OSD.AnonymousLists/NewForm.aspx";
                        list.DefaultEditFormUrl = list.ParentWeb.ServerRelativeUrl + list.RootFolder.Url + editForm.Url; //"/_layouts/OSD.AnonymousLists/EditForm.aspx";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.