How To Reset the Default EditFormUrl, NewFormUrl and DispFormUrl of a SharePoint ContentType
When you want to change the EditFormUrl, NewFormUrl and DispFormUrl of a SharePoint ContentType, you can do this by including a section in your ContentType Xml Declaration or do it in code (recommended). It is also possible to reset those Urls so that the default SharePoint form is loaded when editing, creating or displaying a listitem of that content type.
If you want to change the EditForm, NewForm and DispForm Urls of a content type, you could do it as follows:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!--Some defined fields-->
<!-- Example: Parent ContentType: Item (0x01) -->
<ContentType ID="0x0100..." Name="ProductContentType" Group="Custom Content Types" Description="My Content Type" Inherits="false" Version="0">
<FieldRefs>
<!--Some references to the fields used in your content type-->
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
<FormUrls xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url">
<Display>_layouts/MyProjectName/Product.aspx?mode=display</Display>
<Edit>_layouts/MyProjectName/Product.aspx?mode=edit</Edit>
<New>_layouts/MyProjectName/Product.aspx?mode=new</New>
</FormUrls>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>
However after reading the JOPX Blog post about this, it is better to do this in code and to make an extra feature for this. If you want to clean up (reset) your Custom Urls to the default ones used by SharePoint when de-activating that feature, also that code is included:
private void changeFormUrls(SPSite currentSite, string offerteContentTypeName, bool reset)
{
foreach (SPWeb currentWeb in currentSite.AllWebs)
{
foreach (SPList currentList in currentWeb.Lists)
{
foreach (SPContentType currentCT in currentList.ContentTypes)
{
if (currentCT.Name.Equals(offerteContentTypeName, StringComparison.CurrentCultureIgnoreCase))
{
try
{
currentWeb.AllowUnsafeUpdates = true;
//Change the Form Urls
if (reset)
{
currentCT.NewFormUrl = "NewForm.aspx";
currentCT.EditFormUrl = "EditForm.aspx";
currentCT.DisplayFormUrl = "DispForm.aspx";
}
else
{
currentCT.NewFormUrl = @"_layouts/MyProjectName/Product.aspx?mode=new";
currentCT.EditFormUrl = @"_layouts/MyProjectName/Product.aspx?mode=edit";
currentCT.DisplayFormUrl = @"_layouts/MyProjectName/Product.aspx?mode=display";
}
currentCT.Update();
currentList.Update();
}
catch (Exception ex)
{
//ToDo: Add some logging...
}
finally
{
currentWeb.AllowUnsafeUpdates = false;
}
}
}
}
}
}
Off course you’ll have to pass the name of the content type to this function.
Happy coding
No Comments »
RSS feed for comments on this post.