Custom NewForm , Redirect to a custom page using Powershell
-
25-12-2020 - |
سؤال
I could achieve this i.e. creating a list's NewForm named CustomNewForm.aspx setting it as Default NewForm and editing it in advanced mode to add a script code which redirects to CustomForm.aspx Page using code
window.location.href = "/sites/<Site Collection Name>/SitePages/CustomForm.aspx"
Using SharePoint Designer but as per deployment is concerned I need to achieve this using Powershell only. Is there a way to do the above two steps using Powershell or is there a better solution to achieve the task of mainly to redirect to CustomForm Page (The CustomForm.aspx page has custom UI and consist of jsom code to create a new ListItem) for creating new item in the list and making it available on the click of New item button.
المحلول
Use the following CSOM code to update the NewFormUrl
property of the list item content type.
ClientContext ctx = new ClientContext("http://spm:5002/sites/test");
Web web = ctx.Web;
List test = web.Lists.GetByTitle("TestList");
ContentTypeCollection ct = test.ContentTypes;
ctx.Load(ct);
ctx.ExecuteQuery();
ContentType item = ct.Where(x => x.Name.Equals("Item")).FirstOrDefault();
Console.WriteLine(item.Name + ", " + item.Id + ", " + item.NewFormUrl);
item.NewFormUrl = "SitePages/asd.aspx";
item.Update(false);
ctx.ExecuteQuery();
Now whenever you click on New Item
it will open the desired page. Also note that item.Update(false);
statement is going to update the content type. The false
as function parameter used to not update the derived content type.