1
我试图在现有的XlstListviewwebpart上显示添加新项目链接。但下面的代码不起作用。我发现这个在这里:http://suryapulipati.blogspot.be/2011/08/sharepoint-2010-show-add-new-item-or.html以编程方式在XsltListViewWebPart上添加新项目
但toolbarnode总是空
private void ShowAddOrganisationLinkOnHomePage(string webUrl)
{
Logger.LogDebug("NLSponsoringSiteConfigSponsoringCentralEventReceiver", "ConfigureOrganisationDashboard(SPWeb currentWeb)", "BEGIN");
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPFile pageOrganisation = null;
using (SPWeb oHomeWeb = new SPSite(webUrl).OpenWeb())
{
try
{
pageOrganisation = oHomeWeb.GetFile("default.aspx");
pageOrganisation.CheckOut();
string idWebPartRootOrganisation = "ID_RootOrganisation";
oHomeWeb.AllowUnsafeUpdates = true;
using (SPLimitedWebPartManager mgrPageManager = pageOrganisation.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
SPList organisations = oHomeWeb.GetSafeListByName(SponsoringCommon.Constants.LISTNAMES_ORGANISATIONS2);
XsltListViewWebPart lvwpOrganisation = mgrPageManager.WebParts[idWebPartRootOrganisation] as XsltListViewWebPart;
SPView oCustomView = organisations.Views[SponsoringCommon.Constants.VIEWS_ORGANISATION_DASHBOARD_NAME];
PropertyInfo nodeProp = oCustomView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
XmlNode node = nodeProp.GetValue(oCustomView, null) as XmlNode;
XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
if (toolbarNode != null)
{
if (!toolbarNode.Attributes["Type"].Value.Equals("Freeform"))
{
toolbarNode.Attributes["Type"].Value = "Freeform";
}
}
oCustomView.Update();
mgrPageManager.SaveChanges(lvwpOrganisation);
}
}
catch (Exception)
{
oHomeWeb.AllowUnsafeUpdates = false;
if (pageOrganisation != null) pageOrganisation.UndoCheckOut();
throw;
}
finally
{
pageOrganisation.CheckIn(string.Empty);
oHomeWeb.AllowUnsafeUpdates = false;
}
}
});
}
这个作品真的很好的SP2013!我没有必须使用webpart管理器来更改该工具栏,只需要SetToolBarType()方法。 – SAV