3

I want to replace the New and Edit forms for a specific list. I have a SharePoint Add-in which has two forms (one for New and one for Edit) and now I want to make sure that when a user click New or Edit my custom forms are used.

This should cover both when users create items using the ribbon button New Item and when they click the +new item link on the AllItems.aspx page.

I don't want to do this using server-side features, I want to use CSOM to do this!

There is a good Dev PnP sample on how to add new Ribbon buttons using CSOM that I'm using to add buttons with custom commands in the Display form. My first thought was to do something similar to this but perhaps this is the wrong path.

Clarification

I have a SP Add-in and the web-project part of the add-in is an MVC application that has two forms, one for new and one for edit. Perhaps being an Add-in is not necessary and a normal ASP.NET project is just fine. But regardless of which type of project the form is located in I don't see how I can upload the forms to the Forms Library, as they are dependent on c# code in the DLL to execute.

UPDATE

We ended up skipping custom forms and instead used Site Columns to define which fields is shown in which form, less code and uses configuration instead. Thanks for the discussions!

  • That means your main requirement is to change the default new & edit form of a list using CSOM. Let me know if I am wrong – Atish Dipongkor - MVP Nov 27 '15 at 7:37
  • Yes, that is what I'm trying to achieve. – Mattias Lindberg Nov 27 '15 at 9:54
  • Now try my solution and let me know whether it works or not – Atish Dipongkor - MVP Nov 27 '15 at 9:59
5

If I am not wrong, you need to change the default New & Edit form of your specific list using CSOM.

Following is using C#

Get your list an Content Types.

var specificList = oWebsite.Lists.GetByTitle("SpecificListName");
var specificListContentTypes = specificList.ContentTypes;

specificListContentTypes holds all the content type those are being using in your SpecificList. So you have to find your desired content type. You can try GetById.

var myDesiredContentType = specificListContentTypes.GetById("Id of myDesiredContentType");

Now change the URL and Update()

myDesiredContentType.EditFormUrl = "Your URL";
myDesiredContentType.NewFormUrl = "Your URL";
myDesiredContentType.Update(true);
specificList.Update();
  • Thanks, this updates the URL. But now I'm getting 'A potentially dangerous Request.Path value was detected from the client' in the ULS log. How would the URL be specified? – Mattias Lindberg Nov 27 '15 at 10:02
  • Are you sure that above solution is causing the issue? – Atish Dipongkor - MVP Nov 27 '15 at 10:07
  • The code above execute ok. But when I click on the New Item link in the list I get error, didn't get that before I changed the URL. I set the URL to localhost:53561/WebForm1.aspx – Mattias Lindberg Nov 27 '15 at 10:16
  • oooopps.. Your form should be uploaded into the form library I guess. Your are trying with a local url. Upload your form in SP site and then update the Form URL – Atish Dipongkor - MVP Nov 27 '15 at 10:25
  • Great code Atish !!!, why csom and not sharepoint designer to achieve this. – Azam Khan Nov 27 '15 at 11:02

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.