I am adding XsltListViewWebPart to application page from behind code. I want to have fully functional ribbon there for this list view. I have also custom ribbon tab action and custom context menu actions registered to content type that is showed by this view.
Web part is added successfully and list items are showed. My custom ribbon tab is visible and it works fine. All context menu actions including custom one are working.
But I have two issues: 1. Toolbar with "New | Action | Settings" is showed in list view web part. 2. All buttons in List Tools ribbon tab are disabled.
To resolve first issue I am hiding toolbar using reflection from behind code:
MethodInfo ensureViewMethod = XsltListViewWebPart_Results.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
object[] ensureViewParams = { };
ensureViewMethod.Invoke(XsltListViewWebPart_Results, ensureViewParams);
FieldInfo viewFieldInfo = XsltListViewWebPart_Results.GetType().GetField("view", BindingFlags.NonPublic | BindingFlags.Instance);
SPView spView = viewFieldInfo.GetValue(XsltListViewWebPart_Results) as SPView;
Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
MethodInfo setToolbarTypeMethod = spView.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
object[] setToolbarParam = { "None" };
setToolbarTypeMethod.Invoke(spView, setToolbarParam);
And it does its magic - Toolbar disappear AND it also resolves second issue - Buttons in List Tools tab in ribbon are active. BUT my custom ribbon tab is no longer there, my custom context menu actions are still in place but tab is not visible. I was trying enable ribbon tab using SPRibbon.MakeTabAvailable method but it did not work. Hidding List view toolbar is not an issue - I can do it using JS or CSS but I need working ribbon tabs.
To set up XsltListViewWebPart I am using following code:
XsltListViewWebPart_Results.ViewGuid = spList.DefaultView.ID.ToString("B");
XsltListViewWebPart_Results.ListId = listId;
XsltListViewWebPart_Results.ViewContentTypeId = spList.ContentTypes[0].Id.ToString();
XsltListViewWebPart_Results.ShowToolbarWithRibbon = true;
XsltListViewWebPart_Results.WebId = SPContext.Current.Web.ID;
XsltListViewWebPart_Results.ListName = spList.ID.ToString("B");
Does anybody have an idea what might be the reason why List Tool buttons are disabled or why my custom tab in ribbon disappear? Or what can be done to make this work properly?
Thanks

0 commentaires:
Enregistrer un commentaire