I'm adding an ListViewWebPart programmatically to my custom page, so far so good, the problem is that the link "Add document" is missing(Summary Toolbar), and the simple behavior of open the edit web part panel, don't change a thing, and then press OK resolves the prob. (and also adds Ajax Options and Misc categories)
Any idea why is missing or how to force it to appear?
spregards
code:
public static void AddListViewWebPart(SPWeb spWeb, SPFile oFile, SPList oSPList, string webPartTitle, string viewName, string webPartZone)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (var site = new SPSite(spWeb.Site.ID))
{
site.AllowUnsafeUpdates = true;
using (SPWeb oWeb = site.OpenWeb(spWeb.ID))
{
oWeb.AllowUnsafeUpdates = true;
//Create the object SPLimitedWebPart Manager
SPLimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
//Create ListViewWebPart
SPView spView = oSPList.Views[viewName];
var oListViewWP = new ListViewWebPart
{
ChromeType = PartChromeType.None,
Title = webPartTitle,
ListName = oSPList.ID.ToString("B").ToUpper(),
ViewGuid = spView.ID.ToString("B").ToUpperInvariant(),
AllowConnect = false,
AllowClose = false,
AllowEdit = false,
AllowHide = false,
AllowMinimize = false,
AllowZoneChange = false,
ListViewXml = spView.HtmlSchemaXml,
};
//Define the zone in which webparts need to be added
limitedWebPartManager.AddWebPart(oListViewWP, webPartZone, 1);
SetToolbarTypeFreeForm(oWeb, oFile.Url, oListViewWP.StorageKey);
limitedWebPartManager.SaveChanges(oListViewWP);
limitedWebPartManager.Dispose();
}
}
});
}
catch (Exception ex)
{
SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("WebPartsHelper.AddListViewWebPart() Error", TraceSeverity.Medium, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
SPUtility.TransferToErrorPage(String.Format("An error occurred while attempting to add the List View WebPart to the default.aspx with the following error message: {0}", ex.Message));
}
}
Solution:
using (SPLimitedWebPartManager wpMngr = web.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
//// Create web part and set properties
XsltListViewWebPart lvwp = new XsltListViewWebPart
{
ListName = oList.ID.ToString("B").ToUpper(),
Title = oList.Title,
ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly,
ViewGuid = spView.ID.ToString(),
ListId = oList.ID,
XmlDefinition = spView.GetViewXml()
};
wpMngr.AddWebPart(lvwp, "Left", 1);
}