0

I am facing some specific issue here regarding SharePoint Lists New/display/edit forms...

I explain:

I have created (from Visual studio) a SharePoint List based on DiscussionBoard ListTemplate. now i want to customize the NewForm and EditForm of that list.

I walked through several approches; for examlpe modifiying in the Schema.xml but this only works for custom Lists not for Sharepoint list (those who are based on Templates like Discussion board) because i have only the listInstanceFile and the elements.xml file i don't have the schema.xml file to make my modifications in it.

I tried then to do that programmatically, i tried the following (after creating an application page in a mapped layout folder)

string sDirectory=Microsoft.SharePoint.Utilities.SPUtility.GetCurrentGenericSetupPath(@"TEMPLATE\LAYOUTS\TetSPCustomForms");
    string newUrl = sDirectory + @"\DiscussionApplicationPage.aspx";
    var form = web.GetFile(newUrl);
    if (form != null && form.Exists)
    {
      lstTest.DefaultNewFormUrl = newUrl;
      lstTest.Update();
    }

This doesn't work; The GetFile is getting out of range...

I tried also this,(after creating an application page and putting it under the list)

string urlNew = lstTest.DefaultNewFormUrl.Replace("NewForm.aspx", "D2appPage.aspx");
lstTest.Update();

I tried this after,

var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
SPList lstTest = web.Lists["testBoard"];
string newUrl = string.Format("{0}{1}/D2appPage.aspx",web.ServerRelativeUrl,lstTest.RootFolder.Url);

newUrl is getting a wrong one ...

And lastly i tried this

SPList myList = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, "/Lists/testBoard")); //Rq var list = web.Lists.TryGetList("testBoard"); var rootFolder = list.RootFolder;

    var newFormUrl = string.Format("{0}", list.DefaultNewFormUrl);

    var newForm = web.GetFile(newFormUrl);
    if (newForm != null && newForm.Exists)
        newForm.Delete();   // delete & recreate our new form


    newForm = rootFolder.Files.Add(newFormUrl, SPTemplateFileType.FormPage);
    var wpm = newForm.GetLimitedWebPartManager(PersonalizationScope.Shared);

    var webpart = new ListFormWebPart
    {
        ListId = list.ID,
        ListName = list.ID.ToString("B").ToUpper(),
        PageType = PAGETYPE.PAGE_NEWFORM,
        Title = list.Title,
        Description = list.Description,
        CatalogIconImageUrl = list.ImageUrl,
        TitleUrl = list.DefaultViewUrl,
        //TemplateName = "SomeCustomRenderingTemplate"
    };
    wpm.AddWebPart(webpart, "Main", 0);

nothing is getting changed...

Am i missing something ?

Any help will be appreciated.

1

I also try different approaches. I achive my goal with this peace of code:

SPFolder rootFolder = list.RootFolder; //at this point you should have SPList (list) and SPWeb (web) objects           

            /* create a new CustomNewForm (copy oryginal and modyfied it) */
            string newFormUrl = string.Format("{0}/{1}/{2}", web.ServerRelativeUrl, list.RootFolder.Url, "NewFormCustom.aspx");
            SPFile newForm = SharePointLibrary.PageHelper.copyPage(web, newFormUrl, list.DefaultNewFormUrl); // this is my custom simple method uses CopyTo metchod of SPFileObject                
            list.DefaultNewFormUrl = newFormUrl;

            SPLimitedWebPartManager lwpm = newForm.GetLimitedWebPartManager(PersonalizationScope.Shared);
            foreach(Microsoft.SharePoint.WebPartPages.WebPart wp in lwpm.WebParts )
            {
                if(typeof(Microsoft.SharePoint.WebPartPages.ListFormWebPart).IsInstanceOfType(wp))
                { 
                    ListFormWebPart lfwp = wp as ListFormWebPart;
                    if (lfwp.TemplateName == "ListForm")
                    {
                        wp.Hidden = true; // just hide it. (I do not know if you can remove it, but like this, it works)
                    }
                    lwpm.SaveChanges(lfwp);
                }
            }
            myWebPart webpart = new myWebPart();
            lwpm.AddWebPart(webpart, "Main", 0);

I try other options but in most cases

list.DefaultNewFormUrl = newFormUrl;

in this line I saw error.

Here is a bit information about ListForm

Hope it helps.

Your Answer

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

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