1)创建ControlTemplates - 在项目目录中 - /ControlTemplates/Main/MainControl.ascx
2)为MainControl写了业务逻辑
3)已创建全局ControlTemplates ProjectTemplates.ascx
4)在ProjectTemplates中添加了控件MainControl:
<%@ Register TagPrefix="main" TagName="MainControl" src="~/_controltemplates/Main/MainContro.ascx" %>
<SharePoint:RenderingTemplate id="ProjMainControl" runat="server">
<Template>
<main:MainControl runat="server" />
</Template>
</SharePoint:RenderingTemplate>
5)使用实用程序制作表单 - 在应用程序外部运行并关联我们的控件:
private bool ChangeMyForms(SPWeb web, SPFile homePage, string TemplateName)
{
using (SPLimitedWebPartManager wpm = homePage.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
var webparts = wpm.WebParts.OfType<Wp>().ToList();
foreach (Wp wpt in webparts)
{
if (!(wpt is XsltListFormWebPart))
{
continue;
}
var wp = (XsltListFormWebPart)wpt;
string zoneId = wp.ZoneID;
int zoneIndex = wp.ZoneIndex;
var listName = wp.ListName;
var listId = wp.ListId;
var pageType = wp.PageType;
wpm.DeleteWebPart(wp);
var lfwp = new ListFormWebPart();
lfwp.TemplateName = TemplateName;
lfwp.ListName = listName;
lfwp.ListId = listId;
lfwp.PageType = pageType;
wpm.AddWebPart(lfwp, zoneId, zoneIndex);
}
}
return true;
}
实用工具按钮代码:
using (SPWeb web = SPContext.Current.Site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl)) // I think that my main problem here!
{
var TaskList = web.Lists.TryGetList("Tasks"); //Tasks //Lists/Tasks/EditForm.aspx
if(TaskList != null) //Задачи
{
SPFile newHomePage = web.GetFile(SPUrlUtility.CombineUrl(TaskList.RootFolder.Url, "NewForm.aspx"));
SPFile editHomePage = web.GetFile(SPUrlUtility.CombineUrl(TaskList.RootFolder.Url, "EditForm.aspx")); // Lists/Tasks/EditForm.aspx
ChangeMyForms(web, editHomePage, "ProjMainControl");//EditForm
ChangeMyForms(web, newHomePage, "ProjMainControl");//NewForm
}
}